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.function.Predicate;
|
---|
7 | import java.util.regex.Pattern;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Utility class for creating {@link Predicate}s.
|
---|
13 | */
|
---|
14 | public final class Predicates {
|
---|
15 |
|
---|
16 | private Predicates() {
|
---|
17 | }
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Creates a predicate that returns true every time.
|
---|
21 | * @param <T> The type of the predicate.
|
---|
22 | * @return A predicate returning <code>true</code>
|
---|
23 | * @since 10040
|
---|
24 | */
|
---|
25 | public static <T> Predicate<T> alwaysTrue() {
|
---|
26 | return o -> true;
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Creates a predicate that returns false every time.
|
---|
31 | * @param <T> The type of the predicate.
|
---|
32 | * @return A predicate returning <code>false</code>
|
---|
33 | * @since 10040
|
---|
34 | */
|
---|
35 | public static <T> Predicate<T> alwaysFalse() {
|
---|
36 | return o -> false;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Returns a {@link Predicate} executing {@link Objects#equals}.
|
---|
41 | * @param <T> type of items
|
---|
42 | * @param ref the reference object
|
---|
43 | * @return a {@link Predicate} executing {@link Objects#equals}
|
---|
44 | */
|
---|
45 | public static <T> Predicate<T> equalTo(final T ref) {
|
---|
46 | return obj -> Objects.equals(obj, ref);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a new predicate that checks if elements are exactly of that class.
|
---|
51 | * @param <T> The predicate type.
|
---|
52 | * @param clazz The class the elements must have.
|
---|
53 | * @return The new predicate.
|
---|
54 | * @throws IllegalArgumentException if clazz is null
|
---|
55 | */
|
---|
56 | public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) {
|
---|
57 | CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
|
---|
58 | return obj -> obj != null && obj.getClass() == clazz;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Creates a new predicate that checks if the object is of a given class.
|
---|
63 | * @param <T> The predicate type.
|
---|
64 | * @param clazz The class objects need to be of.
|
---|
65 | * @return The new predicate.
|
---|
66 | * @throws IllegalArgumentException if clazz is null
|
---|
67 | * @since 10286
|
---|
68 | */
|
---|
69 | public static <T> Predicate<T> isInstanceOf(final Class<? extends T> clazz) {
|
---|
70 | CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
|
---|
71 | return clazz::isInstance;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
|
---|
76 | * @param pattern the pattern
|
---|
77 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}
|
---|
78 | */
|
---|
79 | public static Predicate<String> stringMatchesPattern(final Pattern pattern) {
|
---|
80 | return string -> pattern.matcher(string).matches();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}.
|
---|
85 | * @param pattern the pattern
|
---|
86 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}
|
---|
87 | */
|
---|
88 | public static Predicate<String> stringContainsPattern(final Pattern pattern) {
|
---|
89 | return string -> pattern.matcher(string).find();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns a {@link Predicate} executing {@link String#contains(CharSequence)}.
|
---|
94 | * @param pattern the pattern
|
---|
95 | * @return a {@link Predicate} executing {@link String#contains(CharSequence)}
|
---|
96 | */
|
---|
97 | public static Predicate<String> stringContains(final String pattern) {
|
---|
98 | return string -> string.contains(pattern);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
|
---|
103 | * @param key the key forming the tag
|
---|
104 | * @param values one or many values forming the tag
|
---|
105 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}
|
---|
106 | */
|
---|
107 | public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) {
|
---|
108 | return p -> p.hasTag(key, values);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}.
|
---|
113 | * @param key the key
|
---|
114 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}
|
---|
115 | */
|
---|
116 | public static Predicate<OsmPrimitive> hasKey(final String key) {
|
---|
117 | return p -> p.hasKey(key);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
|
---|
122 | * @param <T> type of items
|
---|
123 | * @param target collection
|
---|
124 | * @return a {@link Predicate} executing {@link Collection#contains(Object)}
|
---|
125 | */
|
---|
126 | public static <T> Predicate<T> inCollection(final Collection<? extends T> target) {
|
---|
127 | return target::contains;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Returns a {@link Predicate} testing whether objects are {@code null}.
|
---|
132 | * @param <T> type of items
|
---|
133 | * @return a {@link Predicate} testing whether objects are {@code null}
|
---|
134 | */
|
---|
135 | public static <T> Predicate<T> isNull() {
|
---|
136 | return object -> object == null;
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|