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