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.filter;
15 import java.util.regex.Pattern;
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;
25 * Token representing an attribute filter.
27 * @author Sönke Küper - initial contribution.
30 public final class ChannelNameEquals extends FilterToken {
32 private final String channelName;
33 private final Pattern filterValue;
34 private final String channelGroup;
37 * Creates a new {@link ChannelNameEquals}.
39 public ChannelNameEquals(int position, String channelGroup, String channelName, Pattern filterPattern) {
41 this.channelGroup = channelGroup;
42 this.channelName = channelName;
43 this.filterValue = filterPattern;
47 * Returns the channel group.
49 public String getChannelGroup() {
54 * Returns the channel name.
56 public String getChannelName() {
61 * Returns the filter value.
63 public Pattern getFilterValue() {
68 public String toString() {
69 return this.channelGroup + "#" + channelName + "=\"" + this.filterValue + "\"";
73 public <R> R accept(FilterTokenVisitor<R> visitor) throws FilterParserException {
74 return visitor.handle(this);
78 * Maps this into a {@link TimetableStopByStringEventAttributeFilter}.
80 public TimetableStopByStringEventAttributeFilter mapToPredicate() throws FilterParserException {
81 return new TimetableStopByStringEventAttributeFilter(mapAttributeSelection(), filterValue);
84 private AttributeSelection mapAttributeSelection() throws FilterParserException {
85 switch (this.channelGroup) {
87 final TripLabelAttribute<?, ?> tripAttribute = TripLabelAttribute.getByChannelName(this.channelName);
88 if (tripAttribute == null) {
89 throw new FilterParserException("Invalid trip channel: " + channelName);
94 final EventType eventTypeDeparture = EventType.DEPARTURE;
95 final EventAttribute<?, ?> departureAttribute = EventAttribute.getByChannelName(this.channelName,
97 if (departureAttribute == null) {
98 throw new FilterParserException("Invalid departure channel: " + channelName);
100 return new EventAttributeSelection(eventTypeDeparture, departureAttribute);
103 final EventType eventTypeArrival = EventType.ARRIVAL;
104 final EventAttribute<?, ?> arrivalAttribute = EventAttribute.getByChannelName(this.channelName,
106 if (arrivalAttribute == null) {
107 throw new FilterParserException("Invalid arrival channel: " + channelName);
109 return new EventAttributeSelection(eventTypeArrival, arrivalAttribute);
111 throw new FilterParserException("Unknown channel group: " + channelGroup);