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