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