]> git.basschouten.com Git - openhab-addons.git/blob
3b0750bd0a8a2bd566d83d6b55ee2228cb838086
[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.Collections;
16 import java.util.List;
17 import java.util.function.BiConsumer;
18 import java.util.function.Function;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
23 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripLabel;
24 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28
29 /**
30  * Selection that returns the value of an {@link TripLabel}.
31  * 
32  * chapter "1.2.7 TripLabel" in Technical Interface Description for external Developers
33  *
34  * @see https://developer.deutschebahn.com/store/apis/info?name=Timetables&version=v1&provider=DBOpenData&#tab1
35  * 
36  * @author Sönke Küper - Initial contribution.
37  * 
38  * @param <VALUE_TYPE> type of value in Bean.
39  * @param <STATE_TYPE> type of state.
40  */
41 @NonNullByDefault
42 public final class TripLabelAttribute<VALUE_TYPE, STATE_TYPE extends State> extends
43         AbstractDtoAttributeSelector<TripLabel, @Nullable VALUE_TYPE, STATE_TYPE> implements AttributeSelection {
44
45     /**
46      * Trip category.
47      */
48     public static final TripLabelAttribute<String, StringType> C = new TripLabelAttribute<>("category", TripLabel::getC,
49             TripLabel::setC, StringType::new, TripLabelAttribute::singletonList, StringType.class);
50
51     /**
52      * Number.
53      */
54     public static final TripLabelAttribute<String, StringType> N = new TripLabelAttribute<>("number", TripLabel::getN,
55             TripLabel::setN, StringType::new, TripLabelAttribute::singletonList, StringType.class);
56
57     /**
58      * Filter flags.
59      */
60     public static final TripLabelAttribute<String, StringType> F = new TripLabelAttribute<>("filter-flags",
61             TripLabel::getF, TripLabel::setF, StringType::new, TripLabelAttribute::singletonList, StringType.class);
62     /**
63      * Trip Type.
64      */
65     public static final TripLabelAttribute<TripType, StringType> T = new TripLabelAttribute<>("trip-type",
66             TripLabel::getT, TripLabel::setT, TripLabelAttribute::fromTripType, TripLabelAttribute::listFromTripType,
67             StringType.class);
68     /**
69      * Owner.
70      */
71     public static final TripLabelAttribute<String, StringType> O = new TripLabelAttribute<>("owner", TripLabel::getO,
72             TripLabel::setO, StringType::new, TripLabelAttribute::singletonList, StringType.class);
73
74     /**
75      * Creates an new {@link TripLabelAttribute}.
76      *
77      * @param getter Function to get the raw value.
78      * @param setter Function to set the raw value.
79      * @param getState Function to get the Value as {@link State}.
80      */
81     private TripLabelAttribute(final String channelTypeName, //
82             final Function<TripLabel, @Nullable VALUE_TYPE> getter, //
83             final BiConsumer<TripLabel, VALUE_TYPE> setter, //
84             final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
85             final Function<VALUE_TYPE, List<String>> valueToList, //
86             final Class<STATE_TYPE> stateType) {
87         super(channelTypeName, getter, setter, getState, valueToList, stateType);
88     }
89
90     @Nullable
91     @Override
92     public State getState(TimetableStop stop) {
93         if (stop.getTl() == null) {
94             return UnDefType.UNDEF;
95         }
96         return super.getState(stop.getTl());
97     }
98
99     @Override
100     public @Nullable Object getValue(TimetableStop stop) {
101         if (stop.getTl() == null) {
102             return UnDefType.UNDEF;
103         }
104         return super.getValue(stop.getTl());
105     }
106
107     @Override
108     public List<String> getStringValues(TimetableStop stop) {
109         if (stop.getTl() == null) {
110             return Collections.emptyList();
111         }
112         return this.getStringValues(stop.getTl());
113     }
114
115     private static StringType fromTripType(final TripType value) {
116         return new StringType(value.value());
117     }
118
119     private static List<String> listFromTripType(@Nullable final TripType value) {
120         if (value == null) {
121             return Collections.emptyList();
122         } else {
123             return Collections.singletonList(value.value());
124         }
125     }
126
127     /**
128      * Returns a list containing only the given value or empty list if value is <code>null</code>.
129      */
130     private static List<String> singletonList(@Nullable String value) {
131         return value == null ? Collections.emptyList() : Collections.singletonList(value);
132     }
133
134     /**
135      * Returns an {@link TripLabelAttribute} for the given channel-name.
136      */
137     @Nullable
138     public static TripLabelAttribute<?, ?> getByChannelName(final String channelName) {
139         switch (channelName) {
140             case "category":
141                 return C;
142             case "number":
143                 return N;
144             case "filter-flags":
145                 return F;
146             case "trip-type":
147                 return T;
148             case "owner":
149                 return O;
150             default:
151                 return null;
152         }
153     }
154 }