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