[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[6547] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[6652] | 4 | import java.util.Collection;
|
---|
[7083] | 5 | import java.util.Objects;
|
---|
[6547] | 6 | import java.util.regex.Pattern;
|
---|
| 7 |
|
---|
[7083] | 8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 9 |
|
---|
[6547] | 10 | /**
|
---|
| 11 | * Utility class for creating {@link Predicate}s.
|
---|
| 12 | */
|
---|
| 13 | public final class Predicates {
|
---|
| 14 |
|
---|
| 15 | private Predicates() {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
[6592] | 19 | * Returns the negation of {@code predicate}.
|
---|
[9246] | 20 | * @param <T> type of items
|
---|
[8928] | 21 | * @param predicate the predicate to negate
|
---|
| 22 | * @return the negation of {@code predicate}
|
---|
[6592] | 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 | /**
|
---|
[7170] | 34 | * Returns a {@link Predicate} executing {@link Objects#equals}.
|
---|
[9246] | 35 | * @param <T> type of items
|
---|
[8928] | 36 | * @param ref the reference object
|
---|
| 37 | * @return a {@link Predicate} executing {@link Objects#equals}
|
---|
[6592] | 38 | */
|
---|
| 39 | public static <T> Predicate<T> equalTo(final T ref) {
|
---|
| 40 | return new Predicate<T>() {
|
---|
| 41 | @Override
|
---|
| 42 | public boolean evaluate(T obj) {
|
---|
[7083] | 43 | return Objects.equals(obj, ref);
|
---|
[6592] | 44 | }
|
---|
| 45 | };
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
[6547] | 49 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
|
---|
[8928] | 50 | * @param pattern the pattern
|
---|
| 51 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}
|
---|
[6547] | 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}.
|
---|
[8928] | 64 | * @param pattern the pattern
|
---|
| 65 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}
|
---|
[6547] | 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)}.
|
---|
[8928] | 78 | * @param pattern the pattern
|
---|
| 79 | * @return a {@link Predicate} executing {@link String#contains(CharSequence)}
|
---|
[6547] | 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 | }
|
---|
[6573] | 89 |
|
---|
| 90 | /**
|
---|
| 91 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
|
---|
[8928] | 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...)}
|
---|
[6573] | 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 | }
|
---|
[6652] | 104 |
|
---|
| 105 | /**
|
---|
[6870] | 106 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}.
|
---|
[8928] | 107 | * @param key the key
|
---|
| 108 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}
|
---|
[6870] | 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 | /**
|
---|
[6652] | 120 | * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
|
---|
[9246] | 121 | * @param <T> type of items
|
---|
[8928] | 122 | * @param target collection
|
---|
| 123 | * @return a {@link Predicate} executing {@link Collection#contains(Object)}
|
---|
[6652] | 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 | }
|
---|
[7170] | 133 |
|
---|
| 134 | /**
|
---|
| 135 | * Returns a {@link Predicate} testing whether objects are {@code null}.
|
---|
[9246] | 136 | * @param <T> type of items
|
---|
[8928] | 137 | * @return a {@link Predicate} testing whether objects are {@code null}
|
---|
[7170] | 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 | }
|
---|
[6547] | 147 | }
|
---|