]> git.basschouten.com Git - openhab-addons.git/blob
2acbaeaab5e40d3cc73a5c07aba3395bf25d12e6
[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.TimetableStop;
21 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripLabel;
22 import org.openhab.binding.deutschebahn.internal.timetable.dto.TripType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26
27 /**
28  * Selection that returns the value of an {@link TripLabel}.
29  * 
30  * chapter "1.2.7 TripLabel" in Technical Interface Description for external Developers
31  *
32  * @see https://developer.deutschebahn.com/store/apis/info?name=Timetables&version=v1&provider=DBOpenData&#tab1
33  * 
34  * @author Sönke Küper - Initial contribution.
35  * 
36  * @param <VALUE_TYPE> type of value in Bean.
37  * @param <STATE_TYPE> type of state.
38  */
39 @NonNullByDefault
40 public final class TripLabelAttribute<VALUE_TYPE, STATE_TYPE extends State> extends
41         AbstractDtoAttributeSelector<TripLabel, @Nullable VALUE_TYPE, STATE_TYPE> implements AttributeSelection {
42
43     /**
44      * Trip category.
45      */
46     public static final TripLabelAttribute<String, StringType> C = new TripLabelAttribute<>("category", TripLabel::getC,
47             TripLabel::setC, StringType::new, StringType.class);
48
49     /**
50      * Number.
51      */
52     public static final TripLabelAttribute<String, StringType> N = new TripLabelAttribute<>("number", TripLabel::getN,
53             TripLabel::setN, StringType::new, StringType.class);
54
55     /**
56      * Filter flags.
57      */
58     public static final TripLabelAttribute<String, StringType> F = new TripLabelAttribute<>("filter-flags",
59             TripLabel::getF, TripLabel::setF, StringType::new, StringType.class);
60     /**
61      * Trip Type.
62      */
63     public static final TripLabelAttribute<TripType, StringType> T = new TripLabelAttribute<>("trip-type",
64             TripLabel::getT, TripLabel::setT, TripLabelAttribute::fromTripType, StringType.class);
65     /**
66      * Owner.
67      */
68     public static final TripLabelAttribute<String, StringType> O = new TripLabelAttribute<>("owner", TripLabel::getO,
69             TripLabel::setO, StringType::new, StringType.class);
70
71     /**
72      * Creates an new {@link TripLabelAttribute}.
73      *
74      * @param getter Function to get the raw value.
75      * @param setter Function to set the raw value.
76      * @param getState Function to get the Value as {@link State}.
77      */
78     private TripLabelAttribute(final String channelTypeName, //
79             final Function<TripLabel, @Nullable VALUE_TYPE> getter, //
80             final BiConsumer<TripLabel, VALUE_TYPE> setter, //
81             final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
82             final Class<STATE_TYPE> stateType) {
83         super(channelTypeName, getter, setter, getState, stateType);
84     }
85
86     @Nullable
87     @Override
88     public State getState(TimetableStop stop) {
89         if (stop.getTl() == null) {
90             return UnDefType.UNDEF;
91         }
92         return super.getState(stop.getTl());
93     }
94
95     private static StringType fromTripType(final TripType value) {
96         return new StringType(value.value());
97     }
98
99     /**
100      * Returns an {@link TripLabelAttribute} for the given channel-name.
101      */
102     @Nullable
103     public static TripLabelAttribute<?, ?> getByChannelName(final String channelName) {
104         switch (channelName) {
105             case "category":
106                 return C;
107             case "number":
108                 return N;
109             case "filter-flags":
110                 return F;
111             case "trip-type":
112                 return T;
113             case "owner":
114                 return O;
115             default:
116                 return null;
117         }
118     }
119 }