2 * Copyright (c) 2010-2021 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.function.BiConsumer;
16 import java.util.function.Function;
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;
28 * Selection that returns the value of an {@link TripLabel}.
30 * chapter "1.2.7 TripLabel" in Technical Interface Description for external Developers
32 * @see https://developer.deutschebahn.com/store/apis/info?name=Timetables&version=v1&provider=DBOpenData&#tab1
34 * @author Sönke Küper - Initial contribution.
36 * @param <VALUE_TYPE> type of value in Bean.
37 * @param <STATE_TYPE> type of state.
40 public final class TripLabelAttribute<VALUE_TYPE, STATE_TYPE extends State> extends
41 AbstractDtoAttributeSelector<TripLabel, @Nullable VALUE_TYPE, STATE_TYPE> implements AttributeSelection {
46 public static final TripLabelAttribute<String, StringType> C = new TripLabelAttribute<>("category", TripLabel::getC,
47 TripLabel::setC, StringType::new, StringType.class);
52 public static final TripLabelAttribute<String, StringType> N = new TripLabelAttribute<>("number", TripLabel::getN,
53 TripLabel::setN, StringType::new, StringType.class);
58 public static final TripLabelAttribute<String, StringType> F = new TripLabelAttribute<>("filter-flags",
59 TripLabel::getF, TripLabel::setF, StringType::new, StringType.class);
63 public static final TripLabelAttribute<TripType, StringType> T = new TripLabelAttribute<>("trip-type",
64 TripLabel::getT, TripLabel::setT, TripLabelAttribute::fromTripType, StringType.class);
68 public static final TripLabelAttribute<String, StringType> O = new TripLabelAttribute<>("owner", TripLabel::getO,
69 TripLabel::setO, StringType::new, StringType.class);
72 * Creates an new {@link TripLabelAttribute}.
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}.
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);
88 public State getState(TimetableStop stop) {
89 if (stop.getTl() == null) {
90 return UnDefType.UNDEF;
92 return super.getState(stop.getTl());
95 private static StringType fromTripType(final TripType value) {
96 return new StringType(value.value());
100 * Returns an {@link TripLabelAttribute} for the given channel-name.
103 public static TripLabelAttribute<?, ?> getByChannelName(final String channelName) {
104 switch (channelName) {