]> git.basschouten.com Git - openhab-addons.git/blob
ab16a1f370bc907d530dc707738d870ad301d96d
[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.filter;
14
15 import java.util.regex.Pattern;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.deutschebahn.internal.AttributeSelection;
19 import org.openhab.binding.deutschebahn.internal.EventAttribute;
20 import org.openhab.binding.deutschebahn.internal.EventAttributeSelection;
21 import org.openhab.binding.deutschebahn.internal.EventType;
22 import org.openhab.binding.deutschebahn.internal.TripLabelAttribute;
23
24 /**
25  * Token representing an attribute filter.
26  * 
27  * @author Sönke Küper - initial contribution.
28  */
29 @NonNullByDefault
30 public final class ChannelNameEquals extends FilterToken {
31
32     private final String channelName;
33     private final Pattern filterValue;
34     private final String channelGroup;
35
36     /**
37      * Creates a new {@link ChannelNameEquals}.
38      */
39     public ChannelNameEquals(int position, String channelGroup, String channelName, Pattern filterPattern) {
40         super(position);
41         this.channelGroup = channelGroup;
42         this.channelName = channelName;
43         this.filterValue = filterPattern;
44     }
45
46     /**
47      * Returns the channel group.
48      */
49     public String getChannelGroup() {
50         return channelGroup;
51     }
52
53     /**
54      * Returns the channel name.
55      */
56     public String getChannelName() {
57         return channelName;
58     }
59
60     /**
61      * Returns the filter value.
62      */
63     public Pattern getFilterValue() {
64         return filterValue;
65     }
66
67     @Override
68     public String toString() {
69         return this.channelGroup + "#" + channelName + "=\"" + this.filterValue + "\"";
70     }
71
72     @Override
73     public <R> R accept(FilterTokenVisitor<R> visitor) throws FilterParserException {
74         return visitor.handle(this);
75     }
76
77     /**
78      * Maps this into a {@link TimetableStopByStringEventAttributeFilter}.
79      */
80     public TimetableStopByStringEventAttributeFilter mapToPredicate() throws FilterParserException {
81         return new TimetableStopByStringEventAttributeFilter(mapAttributeSelection(), filterValue);
82     }
83
84     private AttributeSelection mapAttributeSelection() throws FilterParserException {
85         switch (this.channelGroup) {
86             case "trip":
87                 final TripLabelAttribute<?, ?> tripAttribute = TripLabelAttribute.getByChannelName(this.channelName);
88                 if (tripAttribute == null) {
89                     throw new FilterParserException("Invalid trip channel: " + channelName);
90                 }
91                 return tripAttribute;
92
93             case "departure":
94                 final EventType eventTypeDeparture = EventType.DEPARTURE;
95                 final EventAttribute<?, ?> departureAttribute = EventAttribute.getByChannelName(this.channelName,
96                         eventTypeDeparture);
97                 if (departureAttribute == null) {
98                     throw new FilterParserException("Invalid departure channel: " + channelName);
99                 }
100                 return new EventAttributeSelection(eventTypeDeparture, departureAttribute);
101
102             case "arrival":
103                 final EventType eventTypeArrival = EventType.ARRIVAL;
104                 final EventAttribute<?, ?> arrivalAttribute = EventAttribute.getByChannelName(this.channelName,
105                         eventTypeArrival);
106                 if (arrivalAttribute == null) {
107                     throw new FilterParserException("Invalid arrival channel: " + channelName);
108                 }
109                 return new EventAttributeSelection(eventTypeArrival, arrivalAttribute);
110             default:
111                 throw new FilterParserException("Unknown channel group: " + channelGroup);
112         }
113     }
114 }