| 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 | * Creates a predicate that returns true every time.
|
|---|
| 20 | * @param <T> The type of the predicate.
|
|---|
| 21 | * @return A predicate returning <code>true</code>
|
|---|
| 22 | * @since 10040
|
|---|
| 23 | */
|
|---|
| 24 | public static <T> Predicate<T> alwaysTrue() {
|
|---|
| 25 | return o -> true;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Creates a predicate that returns false every time.
|
|---|
| 30 | * @param <T> The type of the predicate.
|
|---|
| 31 | * @return A predicate returning <code>false</code>
|
|---|
| 32 | * @since 10040
|
|---|
| 33 | */
|
|---|
| 34 | public static <T> Predicate<T> alwaysFalse() {
|
|---|
| 35 | return o -> false;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Returns the negation of {@code predicate}.
|
|---|
| 40 | * @param <T> type of items
|
|---|
| 41 | * @param predicate the predicate to negate
|
|---|
| 42 | * @return the negation of {@code predicate}
|
|---|
| 43 | * @deprecated Use {@link java.util.function.Predicate#negate()}
|
|---|
| 44 | */
|
|---|
| 45 | @Deprecated
|
|---|
| 46 | public static <T> Predicate<T> not(final Predicate<T> predicate) {
|
|---|
| 47 | return obj -> !predicate.evaluate(obj);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Returns a {@link Predicate} executing {@link Objects#equals}.
|
|---|
| 52 | * @param <T> type of items
|
|---|
| 53 | * @param ref the reference object
|
|---|
| 54 | * @return a {@link Predicate} executing {@link Objects#equals}
|
|---|
| 55 | */
|
|---|
| 56 | public static <T> Predicate<T> equalTo(final T ref) {
|
|---|
| 57 | return obj -> Objects.equals(obj, ref);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Creates a new predicate that checks if elements are exactly of that class.
|
|---|
| 62 | * @param <T> The predicate type.
|
|---|
| 63 | * @param clazz The class the elements must have.
|
|---|
| 64 | * @return The new predicate.
|
|---|
| 65 | * @throws IllegalArgumentException if clazz is null
|
|---|
| 66 | */
|
|---|
| 67 | public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) {
|
|---|
| 68 | CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
|
|---|
| 69 | return obj -> obj != null && obj.getClass() == clazz;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Creates a new predicate that checks if the object is of a given class.
|
|---|
| 74 | * @param <T> The predicate type.
|
|---|
| 75 | * @param clazz The class objects need to be of.
|
|---|
| 76 | * @return The new predicate.
|
|---|
| 77 | * @throws IllegalArgumentException if clazz is null
|
|---|
| 78 | * @since 10286
|
|---|
| 79 | */
|
|---|
| 80 | public static <T> Predicate<T> isInstanceOf(final Class<? extends T> clazz) {
|
|---|
| 81 | CheckParameterUtil.ensureParameterNotNull(clazz, "clazz");
|
|---|
| 82 | return clazz::isInstance;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}.
|
|---|
| 87 | * @param pattern the pattern
|
|---|
| 88 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}
|
|---|
| 89 | */
|
|---|
| 90 | public static Predicate<String> stringMatchesPattern(final Pattern pattern) {
|
|---|
| 91 | return string -> pattern.matcher(string).matches();
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Returns a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}.
|
|---|
| 96 | * @param pattern the pattern
|
|---|
| 97 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}
|
|---|
| 98 | */
|
|---|
| 99 | public static Predicate<String> stringContainsPattern(final Pattern pattern) {
|
|---|
| 100 | return string -> pattern.matcher(string).find();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Returns a {@link Predicate} executing {@link String#contains(CharSequence)}.
|
|---|
| 105 | * @param pattern the pattern
|
|---|
| 106 | * @return a {@link Predicate} executing {@link String#contains(CharSequence)}
|
|---|
| 107 | */
|
|---|
| 108 | public static Predicate<String> stringContains(final String pattern) {
|
|---|
| 109 | return string -> string.contains(pattern);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}.
|
|---|
| 114 | * @param key the key forming the tag
|
|---|
| 115 | * @param values one or many values forming the tag
|
|---|
| 116 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}
|
|---|
| 117 | */
|
|---|
| 118 | public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) {
|
|---|
| 119 | return p -> p.hasTag(key, values);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Returns a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}.
|
|---|
| 124 | * @param key the key
|
|---|
| 125 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}
|
|---|
| 126 | */
|
|---|
| 127 | public static Predicate<OsmPrimitive> hasKey(final String key) {
|
|---|
| 128 | return p -> p.hasKey(key);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Returns a {@link Predicate} executing {@link Collection#contains(Object)}.
|
|---|
| 133 | * @param <T> type of items
|
|---|
| 134 | * @param target collection
|
|---|
| 135 | * @return a {@link Predicate} executing {@link Collection#contains(Object)}
|
|---|
| 136 | */
|
|---|
| 137 | public static <T> Predicate<T> inCollection(final Collection<? extends T> target) {
|
|---|
| 138 | return target::contains;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * Returns a {@link Predicate} testing whether objects are {@code null}.
|
|---|
| 143 | * @param <T> type of items
|
|---|
| 144 | * @return a {@link Predicate} testing whether objects are {@code null}
|
|---|
| 145 | */
|
|---|
| 146 | public static <T> Predicate<T> isNull() {
|
|---|
| 147 | return object -> object == null;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | }
|
|---|