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.List;
16 import java.util.function.BiConsumer;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.deutschebahn.internal.timetable.dto.JaxbEntity;
22 import org.openhab.core.types.State;
25 * Accessor for attribute value of a DTO-Object.
27 * @author Sönke Küper - Initial contribution.
29 * @param <DTO_TYPE> type of value in Bean.
30 * @param <VALUE_TYPE> type of value in Bean.
31 * @param <STATE_TYPE> type of state.
34 public abstract class AbstractDtoAttributeSelector<DTO_TYPE extends JaxbEntity, @Nullable VALUE_TYPE, STATE_TYPE extends State> {
36 private final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter;
37 private final BiConsumer<DTO_TYPE, VALUE_TYPE> setter;
38 private final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState;
39 private final String channelTypeName;
40 private final Class<STATE_TYPE> stateType;
41 private final Function<VALUE_TYPE, List<String>> valueToList;
44 * Creates a new {@link EventAttribute}.
46 * @param getter Function to get the raw value.
47 * @param setter Function to set the raw value.
48 * @param getState Function to get the Value as {@link State}.
50 protected AbstractDtoAttributeSelector(final String channelTypeName, //
51 final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter, //
52 final BiConsumer<DTO_TYPE, VALUE_TYPE> setter, //
53 final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
54 final Function<VALUE_TYPE, List<String>> valueToList, //
55 final Class<STATE_TYPE> stateType) {
56 this.channelTypeName = channelTypeName;
59 this.getState = getState;
60 this.valueToList = valueToList;
61 this.stateType = stateType;
65 * Returns the type of the state value.
67 public final Class<STATE_TYPE> getStateType() {
68 return this.stateType;
72 * Returns the name of the corresponding channel-type.
74 public final String getChannelTypeName() {
75 return this.channelTypeName;
79 * Returns the {@link State} for the selected attribute from the given DTO object
80 * Returns <code>null</code> if the value is <code>null</code>.
83 public final STATE_TYPE getState(final DTO_TYPE object) {
84 final VALUE_TYPE value = this.getValue(object);
88 return this.getState.apply(value);
92 * Returns the value for the selected attribute from the given DTO object.
95 public final VALUE_TYPE getValue(final DTO_TYPE object) {
96 return this.getter.apply(object);
100 * Returns a list of values as string list.
101 * Returns empty list if value is not present, singleton list if attribute is not single-valued.
103 public final List<String> getStringValues(DTO_TYPE object) {
104 return this.valueToList.apply(getValue(object));
108 * Sets the value for the selected attribute in the given DTO object
110 public final void setValue(final DTO_TYPE event, final VALUE_TYPE object) {
111 this.setter.accept(event, object);