1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Objects;
|
---|
6 | import java.util.regex.Pattern;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Utility class for creating {@link Predicate}s.
|
---|
12 | */
|
---|
13 | public final class Predicates {
|
---|
14 |
|
---|
15 | private Predicates() {
|
---|
16 | }
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Returns the negation of {@code predicate}.
|
---|
20 | * @param predicate the predicate to negate
|
---|
21 | * @return the negation of {@code predicate}
|
---|
22 | */
|
---|
23 | public static <T> Predicate<T> not(final Predicate<T> predicate) {
|
---|
24 | return new Predicate<T>() {
|
---|
25 | @Override
|
---|
26 | public boolean evaluate(T obj) {
|
---|
27 | return !predicate.evaluate(obj);
|
---|
28 | }
|
---|
29 | };
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Returns a {@link Predicate} executing {@link Objects#equals}.
|
---|
34 | * @param ref the reference object
|
---|
35 | * @return a {@link Predicate} executing {@link Objects#equals}
|
---|
36 | */
|
---|
37 | public static <T> Predicate<T> equalTo(final T ref) {
|
---|
38 | return new Predicate<T>() {
|
---|
39 | @Override
|
---|
40 | public boolean evaluate(T obj) {
|
---|
41 | return Objects.equals(obj, ref);
|
---|
42 | }
|
---|
43 | };
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
|
---|
48 | * @param pattern the pattern
|
---|
49 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}
|
---|
50 | */
|
---|
51 | public static Predicate<String> stringMatchesPattern(final Pattern pattern) {
|
---|
52 | return new Predicate<String>() {
|
---|
53 | @Override
|
---|
54 | public boolean evaluate(String string) {
|
---|
55 | return pattern.matcher(string).matches();
|
---|
56 | }
|
---|
57 | };
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}.
|
---|
62 | * @param pattern the pattern
|
---|
63 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}
|
---|
64 | */
|
---|
65 | public static Predicate<String> stringContainsPattern(final Pattern pattern) {
|
---|
66 | return new Predicate<String>() {
|
---|
67 | @Override
|
---|
68 | public boolean evaluate(String string) {
|
---|
69 | return pattern.matcher(string).find();
|
---|
70 | }
|
---|
71 | };
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Returns a {@link Predicate} executing {@link String#contains(CharSequence)}.
|
---|
76 | * @param pattern the pattern
|
---|
77 | * @return a {@link Predicate} executing {@link String#contains(CharSequence)}
|
---|
78 | */
|
---|
79 | public static Predicate<String> stringContains(final String pattern) {
|
---|
80 | return new Predicate<String>() {
|
---|
81 | @Override
|
---|
82 | public boolean evaluate(String string) {
|
---|
83 | return string.contains(pattern);
|
---|
84 | }
|
---|
85 | };
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
|
---|
90 | * @param key the key forming the tag
|
---|
91 | * @param values one or many values forming the tag
|
---|
92 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}
|
---|
93 | */
|
---|
94 | public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) {
|
---|
95 | return new Predicate<OsmPrimitive>() {
|
---|
96 | @Override
|
---|
97 | public boolean evaluate(OsmPrimitive p) {
|
---|
98 | return p.hasTag(key, values);
|
---|
99 | }
|
---|
100 | };
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}.
|
---|
105 | * @param key the key
|
---|
106 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}
|
---|
107 | */
|
---|
108 | public static Predicate<OsmPrimitive> hasKey(final String key) {
|
---|
109 | return new Predicate<OsmPrimitive>() {
|
---|
110 | @Override
|
---|
111 | public boolean evaluate(OsmPrimitive p) {
|
---|
112 | return p.hasKey(key);
|
---|
113 | }
|
---|
114 | };
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
|
---|
119 | * @param target collection
|
---|
120 | * @return a {@link Predicate} executing {@link Collection#contains(Object)}
|
---|
121 | */
|
---|
122 | public static <T> Predicate<T> inCollection(final Collection<? extends T> target) {
|
---|
123 | return new Predicate<T>() {
|
---|
124 | @Override
|
---|
125 | public boolean evaluate(T object) {
|
---|
126 | return target.contains(object);
|
---|
127 | }
|
---|
128 | };
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Returns a {@link Predicate} testing whether objects are {@code null}.
|
---|
133 | * @return a {@link Predicate} testing whether objects are {@code null}
|
---|
134 | */
|
---|
135 | public static <T> Predicate<T> isNull() {
|
---|
136 | return new Predicate<T>() {
|
---|
137 | @Override
|
---|
138 | public boolean evaluate(T object) {
|
---|
139 | return object == null;
|
---|
140 | }
|
---|
141 | };
|
---|
142 | }
|
---|
143 | }
|
---|