]> git.basschouten.com Git - openhab-addons.git/blob
e5c844d15d5321620028736c1d01ed949bd18c25
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.List;
16 import java.util.function.BiConsumer;
17 import java.util.function.Function;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.deutschebahn.internal.timetable.dto.JaxbEntity;
22 import org.openhab.core.types.State;
23
24 /**
25  * Accessor for attribute value of a DTO-Object.
26  * 
27  * @author Sönke Küper - Initial contribution.
28  *
29  * @param <DTO_TYPE> type of value in Bean.
30  * @param <VALUE_TYPE> type of value in Bean.
31  * @param <STATE_TYPE> type of state.
32  */
33 @NonNullByDefault
34 public abstract class AbstractDtoAttributeSelector<DTO_TYPE extends JaxbEntity, @Nullable VALUE_TYPE, STATE_TYPE extends State> {
35
36     private final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter;
37     private final BiConsumer<DTO_TYPE, VALUE_TYPE> setter;
38     private final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState;
39     private final String channelTypeName;
40     private final Class<STATE_TYPE> stateType;
41     private final Function<VALUE_TYPE, List<String>> valueToList;
42
43     /**
44      * Creates a new {@link EventAttribute}.
45      *
46      * @param getter Function to get the raw value.
47      * @param setter Function to set the raw value.
48      * @param getState Function to get the Value as {@link State}.
49      */
50     protected AbstractDtoAttributeSelector(final String channelTypeName, //
51             final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter, //
52             final BiConsumer<DTO_TYPE, VALUE_TYPE> setter, //
53             final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
54             final Function<VALUE_TYPE, List<String>> valueToList, //
55             final Class<STATE_TYPE> stateType) {
56         this.channelTypeName = channelTypeName;
57         this.getter = getter;
58         this.setter = setter;
59         this.getState = getState;
60         this.valueToList = valueToList;
61         this.stateType = stateType;
62     }
63
64     /**
65      * Returns the type of the state value.
66      */
67     public final Class<STATE_TYPE> getStateType() {
68         return this.stateType;
69     }
70
71     /**
72      * Returns the name of the corresponding channel-type.
73      */
74     public final String getChannelTypeName() {
75         return this.channelTypeName;
76     }
77
78     /**
79      * Returns the {@link State} for the selected attribute from the given DTO object
80      * Returns <code>null</code> if the value is <code>null</code>.
81      */
82     @Nullable
83     public final STATE_TYPE getState(final DTO_TYPE object) {
84         final VALUE_TYPE value = this.getValue(object);
85         if (value == null) {
86             return null;
87         }
88         return this.getState.apply(value);
89     }
90
91     /**
92      * Returns the value for the selected attribute from the given DTO object.
93      */
94     @Nullable
95     public final VALUE_TYPE getValue(final DTO_TYPE object) {
96         return this.getter.apply(object);
97     }
98
99     /**
100      * Returns a list of values as string list.
101      * Returns empty list if value is not present, singleton list if attribute is not single-valued.
102      */
103     public final List<String> getStringValues(DTO_TYPE object) {
104         return this.valueToList.apply(getValue(object));
105     }
106
107     /**
108      * Sets the value for the selected attribute in the given DTO object
109      */
110     public final void setValue(final DTO_TYPE event, final VALUE_TYPE object) {
111         this.setter.accept(event, object);
112     }
113 }