source: josm/trunk/src/org/openstreetmap/josm/tools/Predicates.java@ 10475

Last change on this file since 10475 was 10286, checked in by Don-vip, 8 years ago

fix #12880 - Add instanceOf predicate and predicate tests (patch by michael2402)

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.Collection;
5import java.util.Objects;
6import java.util.regex.Pattern;
7
8import org.openstreetmap.josm.data.osm.OsmPrimitive;
9
10/**
11 * Utility class for creating {@link Predicate}s.
12 */
13public final class Predicates {
14
15 private Predicates() {
16 }
17
18 /**
19 * Creates a predicate that returns true every time.
20 * @param <T> The type of the predicate.
21 * @return A predicate returning <code>true</code>
22 * @since 10040
23 */
24 public static <T> Predicate<T> alwaysTrue() {
25 return new Predicate<T>() {
26 @Override
27 public boolean evaluate(T object) {
28 return true;
29 }
30 };
31 }
32
33 /**
34 * Creates a predicate that returns false every time.
35 * @param <T> The type of the predicate.
36 * @return A predicate returning <code>false</code>
37 * @since 10040
38 */
39 public static <T> Predicate<T> alwaysFalse() {
40 return new Predicate<T>() {
41 @Override
42 public boolean evaluate(T object) {
43 return false;
44 }
45 };
46 }
47
48 /**
49 * Returns the negation of {@code predicate}.
50 * @param <T> type of items
51 * @param predicate the predicate to negate
52 * @return the negation of {@code predicate}
53 */
54 public static <T> Predicate<T> not(final Predicate<T> predicate) {
55 return new Predicate<T>() {
56 @Override
57 public boolean evaluate(T obj) {
58 return !predicate.evaluate(obj);
59 }
60 };
61 }
62
63 /**
64 * Returns a {@link Predicate} executing {@link Objects#equals}.
65 * @param <T> type of items
66 * @param ref the reference object
67 * @return a {@link Predicate} executing {@link Objects#equals}
68 */
69 public static <T> Predicate<T> equalTo(final T ref) {
70 return new Predicate<T>() {
71 @Override
72 public boolean evaluate(T obj) {
73 return Objects.equals(obj, ref);
74 }
75 };
76 }
77
78 /**
79 * Creates a new predicate that checks if elements are exactly of that class.
80 * @param <T> The predicate type.
81 * @param clazz The class the elements must have.
82 * @return The new predicate.
83 * @throws IllegalArgumentException if clazz is null
84 */
85 public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) {
86 CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
87 return new Predicate<T>() {
88 @Override
89 public boolean evaluate(T obj) {
90 return obj != null && obj.getClass() == clazz;
91 }
92 };
93 }
94
95 /**
96 * Creates a new predicate that checks if the object is of a given class.
97 * @param <T> The predicate type.
98 * @param clazz The class objects need to be of.
99 * @return The new predicate.
100 * @throws IllegalArgumentException if clazz is null
101 * @since 10286
102 */
103 public static <T> Predicate<T> isInstanceOf(final Class<? extends T> clazz) {
104 CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
105 return new Predicate<T>() {
106 @Override
107 public boolean evaluate(T o) {
108 return clazz.isInstance(o);
109 }
110 };
111 }
112
113 /**
114 * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
115 * @param pattern the pattern
116 * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}
117 */
118 public static Predicate<String> stringMatchesPattern(final Pattern pattern) {
119 return new Predicate<String>() {
120 @Override
121 public boolean evaluate(String string) {
122 return pattern.matcher(string).matches();
123 }
124 };
125 }
126
127 /**
128 * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}.
129 * @param pattern the pattern
130 * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}
131 */
132 public static Predicate<String> stringContainsPattern(final Pattern pattern) {
133 return new Predicate<String>() {
134 @Override
135 public boolean evaluate(String string) {
136 return pattern.matcher(string).find();
137 }
138 };
139 }
140
141 /**
142 * Returns a {@link Predicate} executing {@link String#contains(CharSequence)}.
143 * @param pattern the pattern
144 * @return a {@link Predicate} executing {@link String#contains(CharSequence)}
145 */
146 public static Predicate<String> stringContains(final String pattern) {
147 return new Predicate<String>() {
148 @Override
149 public boolean evaluate(String string) {
150 return string.contains(pattern);
151 }
152 };
153 }
154
155 /**
156 * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
157 * @param key the key forming the tag
158 * @param values one or many values forming the tag
159 * @return a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}
160 */
161 public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) {
162 return new Predicate<OsmPrimitive>() {
163 @Override
164 public boolean evaluate(OsmPrimitive p) {
165 return p.hasTag(key, values);
166 }
167 };
168 }
169
170 /**
171 * Returns a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}.
172 * @param key the key
173 * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}
174 */
175 public static Predicate<OsmPrimitive> hasKey(final String key) {
176 return new Predicate<OsmPrimitive>() {
177 @Override
178 public boolean evaluate(OsmPrimitive p) {
179 return p.hasKey(key);
180 }
181 };
182 }
183
184 /**
185 * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
186 * @param <T> type of items
187 * @param target collection
188 * @return a {@link Predicate} executing {@link Collection#contains(Object)}
189 */
190 public static <T> Predicate<T> inCollection(final Collection<? extends T> target) {
191 return new Predicate<T>() {
192 @Override
193 public boolean evaluate(T object) {
194 return target.contains(object);
195 }
196 };
197 }
198
199 /**
200 * Returns a {@link Predicate} testing whether objects are {@code null}.
201 * @param <T> type of items
202 * @return a {@link Predicate} testing whether objects are {@code null}
203 */
204 public static <T> Predicate<T> isNull() {
205 return new Predicate<T>() {
206 @Override
207 public boolean evaluate(T object) {
208 return object == null;
209 }
210 };
211 }
212
213}
Note: See TracBrowser for help on using the repository browser.