2 * Copyright (c) 2010-2023 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 a {@link TripLabel}.
32 * chapter "1.2.7 TripLabel" in Technical Interface Description for external Developers
34 * @see @see <a href="https://developers.deutschebahn.com/db-api-marketplace/apis/product/timetables">DB API
37 * @author Sönke Küper - Initial contribution.
39 * @param <VALUE_TYPE> type of value in Bean.
40 * @param <STATE_TYPE> type of state.
43 public final class TripLabelAttribute<VALUE_TYPE, STATE_TYPE extends State> extends
44 AbstractDtoAttributeSelector<TripLabel, @Nullable VALUE_TYPE, STATE_TYPE> implements AttributeSelection {
49 public static final TripLabelAttribute<String, StringType> C = new TripLabelAttribute<>("category", TripLabel::getC,
50 TripLabel::setC, StringType::new, TripLabelAttribute::singletonList, StringType.class);
55 public static final TripLabelAttribute<String, StringType> N = new TripLabelAttribute<>("number", TripLabel::getN,
56 TripLabel::setN, StringType::new, TripLabelAttribute::singletonList, StringType.class);
61 public static final TripLabelAttribute<String, StringType> F = new TripLabelAttribute<>("filter-flags",
62 TripLabel::getF, TripLabel::setF, StringType::new, TripLabelAttribute::singletonList, StringType.class);
66 public static final TripLabelAttribute<TripType, StringType> T = new TripLabelAttribute<>("trip-type",
67 TripLabel::getT, TripLabel::setT, TripLabelAttribute::fromTripType, TripLabelAttribute::listFromTripType,
72 public static final TripLabelAttribute<String, StringType> O = new TripLabelAttribute<>("owner", TripLabel::getO,
73 TripLabel::setO, StringType::new, TripLabelAttribute::singletonList, StringType.class);
76 * Creates a new {@link TripLabelAttribute}.
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}.
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);
93 public State getState(TimetableStop stop) {
94 if (stop.getTl() == null) {
95 return UnDefType.UNDEF;
97 return super.getState(stop.getTl());
101 public @Nullable Object getValue(TimetableStop stop) {
102 if (stop.getTl() == null) {
103 return UnDefType.UNDEF;
105 return super.getValue(stop.getTl());
109 public List<String> getStringValues(TimetableStop stop) {
110 if (stop.getTl() == null) {
111 return Collections.emptyList();
113 return this.getStringValues(stop.getTl());
116 private static StringType fromTripType(final TripType value) {
117 return new StringType(value.value());
120 private static List<String> listFromTripType(@Nullable final TripType value) {
122 return Collections.emptyList();
124 return Collections.singletonList(value.value());
129 * Returns a list containing only the given value or empty list if value is <code>null</code>.
131 private static List<String> singletonList(@Nullable String value) {
132 return value == null ? Collections.emptyList() : Collections.singletonList(value);
136 * Returns a {@link TripLabelAttribute} for the given channel-name.
139 public static TripLabelAttribute<?, ?> getByChannelName(final String channelName) {
140 switch (channelName) {