1 | package org.openstreetmap.josm.tools;
|
---|
2 |
|
---|
3 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
4 |
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.regex.Pattern;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Utility class for creating {@link Predicate}s.
|
---|
10 | */
|
---|
11 | public final class Predicates {
|
---|
12 |
|
---|
13 | private Predicates() {
|
---|
14 | }
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Returns the negation of {@code predicate}.
|
---|
18 | */
|
---|
19 | public static <T> Predicate<T> not(final Predicate<T> predicate) {
|
---|
20 | return new Predicate<T>() {
|
---|
21 | @Override
|
---|
22 | public boolean evaluate(T obj) {
|
---|
23 | return !predicate.evaluate(obj);
|
---|
24 | }
|
---|
25 | };
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Returns a {@link Predicate} executing {@link Utils#equal}.
|
---|
30 | */
|
---|
31 | public static <T> Predicate<T> equalTo(final T ref) {
|
---|
32 | return new Predicate<T>() {
|
---|
33 | @Override
|
---|
34 | public boolean evaluate(T obj) {
|
---|
35 | return Utils.equal(obj, ref);
|
---|
36 | }
|
---|
37 | };
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
|
---|
42 | */
|
---|
43 | public static Predicate<String> stringMatchesPattern(final Pattern pattern) {
|
---|
44 | return new Predicate<String>() {
|
---|
45 | @Override
|
---|
46 | public boolean evaluate(String string) {
|
---|
47 | return pattern.matcher(string).matches();
|
---|
48 | }
|
---|
49 | };
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}.
|
---|
54 | */
|
---|
55 | public static Predicate<String> stringContainsPattern(final Pattern pattern) {
|
---|
56 | return new Predicate<String>() {
|
---|
57 | @Override
|
---|
58 | public boolean evaluate(String string) {
|
---|
59 | return pattern.matcher(string).find();
|
---|
60 | }
|
---|
61 | };
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Returns a {@link Predicate} executing {@link String#contains(CharSequence)}.
|
---|
66 | */
|
---|
67 | public static Predicate<String> stringContains(final String pattern) {
|
---|
68 | return new Predicate<String>() {
|
---|
69 | @Override
|
---|
70 | public boolean evaluate(String string) {
|
---|
71 | return string.contains(pattern);
|
---|
72 | }
|
---|
73 | };
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
|
---|
78 | */
|
---|
79 | public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) {
|
---|
80 | return new Predicate<OsmPrimitive>() {
|
---|
81 | @Override
|
---|
82 | public boolean evaluate(OsmPrimitive p) {
|
---|
83 | return p.hasTag(key, values);
|
---|
84 | }
|
---|
85 | };
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
|
---|
90 | */
|
---|
91 | public static <T> Predicate<T> inCollection(final Collection<? extends T> target) {
|
---|
92 | return new Predicate<T>() {
|
---|
93 | @Override
|
---|
94 | public boolean evaluate(T object) {
|
---|
95 | return target.contains(object);
|
---|
96 | }
|
---|
97 | };
|
---|
98 | }
|
---|
99 | }
|
---|