]> git.basschouten.com Git - openhab-addons.git/blob
b5c6db1040b759cdc8e9879bd91aa9402b36eac3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.deutschebahn.internal;
14
15 import java.util.function.BiConsumer;
16 import java.util.function.Function;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.deutschebahn.internal.timetable.dto.JaxbEntity;
21 import org.openhab.core.types.State;
22
23 /**
24  * Accessor for attribute value of an DTO-Object.
25  * 
26  * @author Sönke Küper - Initial contribution.
27  *
28  * @param <DTO_TYPE> type of value in Bean.
29  * @param <VALUE_TYPE> type of value in Bean.
30  * @param <STATE_TYPE> type of state.
31  */
32 @NonNullByDefault
33 public abstract class AbstractDtoAttributeSelector<DTO_TYPE extends JaxbEntity, @Nullable VALUE_TYPE, STATE_TYPE extends State> {
34
35     private final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter;
36     private final BiConsumer<DTO_TYPE, VALUE_TYPE> setter;
37     private final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState;
38     private final String channelTypeName;
39     private final Class<STATE_TYPE> stateType;
40
41     /**
42      * Creates an new {@link EventAttribute}.
43      *
44      * @param getter Function to get the raw value.
45      * @param setter Function to set the raw value.
46      * @param getState Function to get the Value as {@link State}.
47      */
48     protected AbstractDtoAttributeSelector(final String channelTypeName, //
49             final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter, //
50             final BiConsumer<DTO_TYPE, VALUE_TYPE> setter, //
51             final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
52             final Class<STATE_TYPE> stateType) {
53         this.channelTypeName = channelTypeName;
54         this.getter = getter;
55         this.setter = setter;
56         this.getState = getState;
57         this.stateType = stateType;
58     }
59
60     /**
61      * Returns the type of the state value.
62      */
63     public final Class<STATE_TYPE> getStateType() {
64         return this.stateType;
65     }
66
67     /**
68      * Returns the name of the corresponding channel-type.
69      */
70     public final String getChannelTypeName() {
71         return this.channelTypeName;
72     }
73
74     /**
75      * Returns the {@link State} for the selected attribute from the given DTO object
76      * Returns <code>null</code> if the value is <code>null</code>.
77      */
78     @Nullable
79     public final STATE_TYPE getState(final DTO_TYPE object) {
80         final VALUE_TYPE value = this.getValue(object);
81         if (value == null) {
82             return null;
83         }
84         return this.getState.apply(value);
85     }
86
87     /**
88      * Returns the value for the selected attribute from the given DTO object.
89      */
90     @Nullable
91     public final VALUE_TYPE getValue(final DTO_TYPE object) {
92         return this.getter.apply(object);
93     }
94
95     /**
96      * Sets the value for the selected attribute in the given DTO object
97      */
98     public final void setValue(final DTO_TYPE event, final VALUE_TYPE object) {
99         this.setter.accept(event, object);
100     }
101 }