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