]> git.basschouten.com Git - openhab-addons.git/blob
dbd23473f570defb0fc6d0eb8e2e65b26770770e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.deutschebahn.internal;
14
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Objects;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
22 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
25
26 /**
27  * Selection that returns the value of an {@link EventAttribute}. The required {@link Event} is
28  * selected with the given {@link EventType}.
29  * 
30  * @author Sönke Küper - Initial contribution
31  */
32 @NonNullByDefault
33 public final class EventAttributeSelection implements AttributeSelection {
34
35     private final EventType eventType;
36     private final EventAttribute<?, ?> eventAttribute;
37
38     /**
39      * Creates a new {@link EventAttributeSelection}.
40      */
41     public EventAttributeSelection(EventType eventType, EventAttribute<?, ?> eventAttribute) {
42         this.eventType = eventType;
43         this.eventAttribute = eventAttribute;
44     }
45
46     @Nullable
47     @Override
48     public State getState(TimetableStop stop) {
49         final Event event = eventType.getEvent(stop);
50         if (event == null) {
51             return UnDefType.UNDEF;
52         } else {
53             return this.eventAttribute.getState(event);
54         }
55     }
56
57     @Override
58     public @Nullable Object getValue(TimetableStop stop) {
59         final Event event = eventType.getEvent(stop);
60         if (event == null) {
61             return UnDefType.UNDEF;
62         } else {
63             return this.eventAttribute.getValue(event);
64         }
65     }
66
67     @Override
68     public List<String> getStringValues(TimetableStop stop) {
69         final Event event = eventType.getEvent(stop);
70         if (event == null) {
71             return Collections.emptyList();
72         } else {
73             return this.eventAttribute.getStringValues(event);
74         }
75     }
76
77     @Override
78     public int hashCode() {
79         return Objects.hash(eventAttribute, eventType);
80     }
81
82     @Override
83     public boolean equals(@Nullable Object obj) {
84         if (!(obj instanceof EventAttributeSelection)) {
85             return false;
86         }
87         final EventAttributeSelection other = (EventAttributeSelection) obj;
88         return Objects.equals(eventAttribute, other.eventAttribute) && eventType == other.eventType;
89     }
90 }