2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.deutschebahn.internal;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.function.BiConsumer;
18 import java.util.function.Function;
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;
30 * Selection that returns the value of an {@link TripLabel}.
32 * chapter "1.2.7 TripLabel" in Technical Interface Description for external Developers
34 * @see https://developer.deutschebahn.com/store/apis/info?name=Timetables&version=v1&provider=DBOpenData&#tab1
36 * @author Sönke Küper - Initial contribution.
38 * @param <VALUE_TYPE> type of value in Bean.
39 * @param <STATE_TYPE> type of state.
42 public final class TripLabelAttribute<VALUE_TYPE, STATE_TYPE extends State> extends
43 AbstractDtoAttributeSelector<TripLabel, @Nullable VALUE_TYPE, STATE_TYPE> implements AttributeSelection {
48 public static final TripLabelAttribute<String, StringType> C = new TripLabelAttribute<>("category", TripLabel::getC,
49 TripLabel::setC, StringType::new, TripLabelAttribute::singletonList, StringType.class);
54 public static final TripLabelAttribute<String, StringType> N = new TripLabelAttribute<>("number", TripLabel::getN,
55 TripLabel::setN, StringType::new, TripLabelAttribute::singletonList, StringType.class);
60 public static final TripLabelAttribute<String, StringType> F = new TripLabelAttribute<>("filter-flags",
61 TripLabel::getF, TripLabel::setF, StringType::new, TripLabelAttribute::singletonList, StringType.class);
65 public static final TripLabelAttribute<TripType, StringType> T = new TripLabelAttribute<>("trip-type",
66 TripLabel::getT, TripLabel::setT, TripLabelAttribute::fromTripType, TripLabelAttribute::listFromTripType,
71 public static final TripLabelAttribute<String, StringType> O = new TripLabelAttribute<>("owner", TripLabel::getO,
72 TripLabel::setO, StringType::new, TripLabelAttribute::singletonList, StringType.class);
75 * Creates an new {@link TripLabelAttribute}.
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}.
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);
92 public State getState(TimetableStop stop) {
93 if (stop.getTl() == null) {
94 return UnDefType.UNDEF;
96 return super.getState(stop.getTl());
100 public @Nullable Object getValue(TimetableStop stop) {
101 if (stop.getTl() == null) {
102 return UnDefType.UNDEF;
104 return super.getValue(stop.getTl());
108 public List<String> getStringValues(TimetableStop stop) {
109 if (stop.getTl() == null) {
110 return Collections.emptyList();
112 return this.getStringValues(stop.getTl());
115 private static StringType fromTripType(final TripType value) {
116 return new StringType(value.value());
119 private static List<String> listFromTripType(@Nullable final TripType value) {
121 return Collections.emptyList();
123 return Collections.singletonList(value.value());
128 * Returns a list containing only the given value or empty list if value is <code>null</code>.
130 private static List<String> singletonList(@Nullable String value) {
131 return value == null ? Collections.emptyList() : Collections.singletonList(value);
135 * Returns an {@link TripLabelAttribute} for the given channel-name.
138 public static TripLabelAttribute<?, ?> getByChannelName(final String channelName) {
139 switch (channelName) {