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.JaxbEntity;
21 import org.openhab.core.types.State;
24 * Accessor for attribute value of an DTO-Object.
26 * @author Sönke Küper - Initial contribution.
28 * @param <DTO_TYPE> type of value in Bean.
29 * @param <VALUE_TYPE> type of value in Bean.
30 * @param <STATE_TYPE> type of state.
33 public abstract class AbstractDtoAttributeSelector<DTO_TYPE extends JaxbEntity, @Nullable VALUE_TYPE, STATE_TYPE extends State> {
35 private final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter;
36 private final BiConsumer<DTO_TYPE, VALUE_TYPE> setter;
37 private final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState;
38 private final String channelTypeName;
39 private final Class<STATE_TYPE> stateType;
42 * Creates an new {@link EventAttribute}.
44 * @param getter Function to get the raw value.
45 * @param setter Function to set the raw value.
46 * @param getState Function to get the Value as {@link State}.
48 protected AbstractDtoAttributeSelector(final String channelTypeName, //
49 final Function<DTO_TYPE, @Nullable VALUE_TYPE> getter, //
50 final BiConsumer<DTO_TYPE, VALUE_TYPE> setter, //
51 final Function<VALUE_TYPE, @Nullable STATE_TYPE> getState, //
52 final Class<STATE_TYPE> stateType) {
53 this.channelTypeName = channelTypeName;
56 this.getState = getState;
57 this.stateType = stateType;
61 * Returns the type of the state value.
63 public final Class<STATE_TYPE> getStateType() {
64 return this.stateType;
68 * Returns the name of the corresponding channel-type.
70 public final String getChannelTypeName() {
71 return this.channelTypeName;
75 * Returns the {@link State} for the selected attribute from the given DTO object
76 * Returns <code>null</code> if the value is <code>null</code>.
79 public final STATE_TYPE getState(final DTO_TYPE object) {
80 final VALUE_TYPE value = this.getValue(object);
84 return this.getState.apply(value);
88 * Returns the value for the selected attribute from the given DTO object.
91 public final VALUE_TYPE getValue(final DTO_TYPE object) {
92 return this.getter.apply(object);
96 * Sets the value for the selected attribute in the given DTO object
98 public final void setValue(final DTO_TYPE event, final VALUE_TYPE object) {
99 this.setter.accept(event, object);