]> git.basschouten.com Git - openhab-addons.git/blob
ad7cefb138d34e0b89443798571a677e5c6fb0e0
[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.List;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.deutschebahn.internal.AttributeSelection;
20 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
21
22 /**
23  * Abstract predicate that filters timetable stops by a selected attribute of a {@link TimetableStop}.
24  * 
25  * If value has multiple values (for example stations on the planned-path) the predicate will return <code>true</code>,
26  * if at least one value matches the given filter.
27  * 
28  * @author Sönke Küper - initial contribution
29  */
30 @NonNullByDefault
31 public final class TimetableStopByStringEventAttributeFilter implements TimetableStopPredicate {
32
33     private final AttributeSelection attributeSelection;
34     private final Pattern filter;
35
36     /**
37      * Creates a new {@link TimetableStopByStringEventAttributeFilter}.
38      */
39     TimetableStopByStringEventAttributeFilter(final AttributeSelection attributeSelection, final Pattern filter) {
40         this.attributeSelection = attributeSelection;
41         this.filter = filter;
42     }
43
44     @Override
45     public boolean test(TimetableStop t) {
46         final List<String> values = attributeSelection.getStringValues(t);
47
48         for (String actualValue : values) {
49             if (filter.matcher(actualValue).matches()) {
50                 return true;
51             }
52         }
53         return false;
54     }
55
56     /**
57      * Returns the {@link AttributeSelection}.
58      */
59     final AttributeSelection getAttributeSelection() {
60         return attributeSelection;
61     }
62
63     /**
64      * Returns the filter pattern.
65      */
66     final Pattern getFilter() {
67         return filter;
68     }
69 }