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 | */
|
---|
21 | public static <T> Predicate<T> not(final Predicate<T> predicate) {
|
---|
22 | return new Predicate<T>() {
|
---|
23 | @Override
|
---|
24 | public boolean evaluate(T obj) {
|
---|
25 | return !predicate.evaluate(obj);
|
---|
26 | }
|
---|
27 | };
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Returns a {@link Predicate} executing {@link Objects#equals}.
|
---|
32 | */
|
---|
33 | public static <T> Predicate<T> equalTo(final T ref) {
|
---|
34 | return new Predicate<T>() {
|
---|
35 | @Override
|
---|
36 | public boolean evaluate(T obj) {
|
---|
37 | return Objects.equals(obj, ref);
|
---|
38 | }
|
---|
39 | };
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
|
---|
44 | */
|
---|
45 | public static Predicate<String> stringMatchesPattern(final Pattern pattern) {
|
---|
46 | return new Predicate<String>() {
|
---|
47 | @Override
|
---|
48 | public boolean evaluate(String string) {
|
---|
49 | return pattern.matcher(string).matches();
|
---|
50 | }
|
---|
51 | };
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}.
|
---|
56 | */
|
---|
57 | public static Predicate<String> stringContainsPattern(final Pattern pattern) {
|
---|
58 | return new Predicate<String>() {
|
---|
59 | @Override
|
---|
60 | public boolean evaluate(String string) {
|
---|
61 | return pattern.matcher(string).find();
|
---|
62 | }
|
---|
63 | };
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Returns a {@link Predicate} executing {@link String#contains(CharSequence)}.
|
---|
68 | */
|
---|
69 | public static Predicate<String> stringContains(final String pattern) {
|
---|
70 | return new Predicate<String>() {
|
---|
71 | @Override
|
---|
72 | public boolean evaluate(String string) {
|
---|
73 | return string.contains(pattern);
|
---|
74 | }
|
---|
75 | };
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
|
---|
80 | */
|
---|
81 | public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) {
|
---|
82 | return new Predicate<OsmPrimitive>() {
|
---|
83 | @Override
|
---|
84 | public boolean evaluate(OsmPrimitive p) {
|
---|
85 | return p.hasTag(key, values);
|
---|
86 | }
|
---|
87 | };
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}.
|
---|
92 | */
|
---|
93 | public static Predicate<OsmPrimitive> hasKey(final String key) {
|
---|
94 | return new Predicate<OsmPrimitive>() {
|
---|
95 | @Override
|
---|
96 | public boolean evaluate(OsmPrimitive p) {
|
---|
97 | return p.hasKey(key);
|
---|
98 | }
|
---|
99 | };
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
|
---|
104 | */
|
---|
105 | public static <T> Predicate<T> inCollection(final Collection<? extends T> target) {
|
---|
106 | return new Predicate<T>() {
|
---|
107 | @Override
|
---|
108 | public boolean evaluate(T object) {
|
---|
109 | return target.contains(object);
|
---|
110 | }
|
---|
111 | };
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Returns a {@link Predicate} testing whether objects are {@code null}.
|
---|
116 | */
|
---|
117 | public static <T> Predicate<T> isNull() {
|
---|
118 | return new Predicate<T>() {
|
---|
119 | @Override
|
---|
120 | public boolean evaluate(T object) {
|
---|
121 | return object == null;
|
---|
122 | }
|
---|
123 | };
|
---|
124 | }
|
---|
125 | }
|
---|