diff --git a/src/org/openstreetmap/josm/tools/Predicate.java b/src/org/openstreetmap/josm/tools/Predicate.java
index 969300c..7886e6e 100644
|
a
|
b
|
package org.openstreetmap.josm.tools;
|
| 6 | 6 | * |
| 7 | 7 | * @param <T> The objects type |
| 8 | 8 | * @since 3177 |
| | 9 | * @deprecated Use {@link java.util.function.Predicate} instead. |
| 9 | 10 | */ |
| 10 | | public interface Predicate<T> { |
| | 11 | @Deprecated |
| | 12 | @FunctionalInterface |
| | 13 | public interface Predicate<T> extends java.util.function.Predicate<T> { |
| 11 | 14 | |
| 12 | 15 | /** |
| 13 | 16 | * Determines whether the object passes the test or not |
| … |
… |
public interface Predicate<T> {
|
| 15 | 18 | * @return {@code true} if the object passes the test, {@code false} otherwise |
| 16 | 19 | */ |
| 17 | 20 | boolean evaluate(T object); |
| | 21 | |
| | 22 | @Override |
| | 23 | default boolean test(T t) { |
| | 24 | return evaluate(t); |
| | 25 | } |
| 18 | 26 | } |
diff --git a/src/org/openstreetmap/josm/tools/Predicates.java b/src/org/openstreetmap/josm/tools/Predicates.java
index 5d072c3..8041594 100644
|
a
|
b
|
public final class Predicates {
|
| 22 | 22 | * @since 10040 |
| 23 | 23 | */ |
| 24 | 24 | public static <T> Predicate<T> alwaysTrue() { |
| 25 | | return new Predicate<T>() { |
| 26 | | @Override |
| 27 | | public boolean evaluate(T object) { |
| 28 | | return true; |
| 29 | | } |
| 30 | | }; |
| | 25 | return o -> true; |
| 31 | 26 | } |
| 32 | 27 | |
| 33 | 28 | /** |
| … |
… |
public final class Predicates {
|
| 37 | 32 | * @since 10040 |
| 38 | 33 | */ |
| 39 | 34 | public static <T> Predicate<T> alwaysFalse() { |
| 40 | | return new Predicate<T>() { |
| 41 | | @Override |
| 42 | | public boolean evaluate(T object) { |
| 43 | | return false; |
| 44 | | } |
| 45 | | }; |
| | 35 | return o -> false; |
| 46 | 36 | } |
| 47 | 37 | |
| 48 | 38 | /** |
| … |
… |
public final class Predicates {
|
| 50 | 40 | * @param <T> type of items |
| 51 | 41 | * @param predicate the predicate to negate |
| 52 | 42 | * @return the negation of {@code predicate} |
| | 43 | * @deprecated Use {@link java.util.function.Predicate#negate()} |
| 53 | 44 | */ |
| | 45 | @Deprecated |
| 54 | 46 | public static <T> Predicate<T> not(final Predicate<T> predicate) { |
| 55 | | return new Predicate<T>() { |
| 56 | | @Override |
| 57 | | public boolean evaluate(T obj) { |
| 58 | | return !predicate.evaluate(obj); |
| 59 | | } |
| 60 | | }; |
| | 47 | return obj -> !predicate.evaluate(obj); |
| 61 | 48 | } |
| 62 | 49 | |
| 63 | 50 | /** |
| … |
… |
public final class Predicates {
|
| 67 | 54 | * @return a {@link Predicate} executing {@link Objects#equals} |
| 68 | 55 | */ |
| 69 | 56 | public static <T> Predicate<T> equalTo(final T ref) { |
| 70 | | return new Predicate<T>() { |
| 71 | | @Override |
| 72 | | public boolean evaluate(T obj) { |
| 73 | | return Objects.equals(obj, ref); |
| 74 | | } |
| 75 | | }; |
| | 57 | return obj -> Objects.equals(obj, ref); |
| 76 | 58 | } |
| 77 | 59 | |
| 78 | 60 | /** |
| … |
… |
public final class Predicates {
|
| 84 | 66 | */ |
| 85 | 67 | public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) { |
| 86 | 68 | CheckParameterUtil.ensureParameterNotNull(clazz, "clazz"); |
| 87 | | return new Predicate<T>() { |
| 88 | | @Override |
| 89 | | public boolean evaluate(T obj) { |
| 90 | | return obj != null && obj.getClass() == clazz; |
| 91 | | } |
| 92 | | }; |
| | 69 | return obj -> obj != null && obj.getClass() == clazz; |
| 93 | 70 | } |
| 94 | 71 | |
| 95 | 72 | /** |
| … |
… |
public final class Predicates {
|
| 102 | 79 | */ |
| 103 | 80 | public static <T> Predicate<T> isInstanceOf(final Class<? extends T> clazz) { |
| 104 | 81 | CheckParameterUtil.ensureParameterNotNull(clazz, "clazz"); |
| 105 | | return new Predicate<T>() { |
| 106 | | @Override |
| 107 | | public boolean evaluate(T o) { |
| 108 | | return clazz.isInstance(o); |
| 109 | | } |
| 110 | | }; |
| | 82 | return o -> clazz.isInstance(o); |
| 111 | 83 | } |
| 112 | 84 | |
| 113 | 85 | /** |
| … |
… |
public final class Predicates {
|
| 116 | 88 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches} |
| 117 | 89 | */ |
| 118 | 90 | public static Predicate<String> stringMatchesPattern(final Pattern pattern) { |
| 119 | | return new Predicate<String>() { |
| 120 | | @Override |
| 121 | | public boolean evaluate(String string) { |
| 122 | | return pattern.matcher(string).matches(); |
| 123 | | } |
| 124 | | }; |
| | 91 | return string -> pattern.matcher(string).matches(); |
| 125 | 92 | } |
| 126 | 93 | |
| 127 | 94 | /** |
| … |
… |
public final class Predicates {
|
| 130 | 97 | * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find} |
| 131 | 98 | */ |
| 132 | 99 | public static Predicate<String> stringContainsPattern(final Pattern pattern) { |
| 133 | | return new Predicate<String>() { |
| 134 | | @Override |
| 135 | | public boolean evaluate(String string) { |
| 136 | | return pattern.matcher(string).find(); |
| 137 | | } |
| 138 | | }; |
| | 100 | return string -> pattern.matcher(string).find(); |
| 139 | 101 | } |
| 140 | 102 | |
| 141 | 103 | /** |
| … |
… |
public final class Predicates {
|
| 144 | 106 | * @return a {@link Predicate} executing {@link String#contains(CharSequence)} |
| 145 | 107 | */ |
| 146 | 108 | public static Predicate<String> stringContains(final String pattern) { |
| 147 | | return new Predicate<String>() { |
| 148 | | @Override |
| 149 | | public boolean evaluate(String string) { |
| 150 | | return string.contains(pattern); |
| 151 | | } |
| 152 | | }; |
| | 109 | return string -> string.contains(pattern); |
| 153 | 110 | } |
| 154 | 111 | |
| 155 | 112 | /** |
| … |
… |
public final class Predicates {
|
| 159 | 116 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)} |
| 160 | 117 | */ |
| 161 | 118 | public static Predicate<OsmPrimitive> hasTag(final String key, final String... values) { |
| 162 | | return new Predicate<OsmPrimitive>() { |
| 163 | | @Override |
| 164 | | public boolean evaluate(OsmPrimitive p) { |
| 165 | | return p.hasTag(key, values); |
| 166 | | } |
| 167 | | }; |
| | 119 | return string -> p.hasTag(key, values); |
| 168 | 120 | } |
| 169 | 121 | |
| 170 | 122 | /** |
| … |
… |
public final class Predicates {
|
| 173 | 125 | * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)} |
| 174 | 126 | */ |
| 175 | 127 | public static Predicate<OsmPrimitive> hasKey(final String key) { |
| 176 | | return new Predicate<OsmPrimitive>() { |
| 177 | | @Override |
| 178 | | public boolean evaluate(OsmPrimitive p) { |
| 179 | | return p.hasKey(key); |
| 180 | | } |
| 181 | | }; |
| | 128 | return p -> p.hasKey(key); |
| 182 | 129 | } |
| 183 | 130 | |
| 184 | 131 | /** |
| … |
… |
public final class Predicates {
|
| 188 | 135 | * @return a {@link Predicate} executing {@link Collection#contains(Object)} |
| 189 | 136 | */ |
| 190 | 137 | public static <T> Predicate<T> inCollection(final Collection<? extends T> target) { |
| 191 | | return new Predicate<T>() { |
| 192 | | @Override |
| 193 | | public boolean evaluate(T object) { |
| 194 | | return target.contains(object); |
| 195 | | } |
| 196 | | }; |
| | 138 | return object -> target.contains(object); |
| 197 | 139 | } |
| 198 | 140 | |
| 199 | 141 | /** |
| … |
… |
public final class Predicates {
|
| 202 | 144 | * @return a {@link Predicate} testing whether objects are {@code null} |
| 203 | 145 | */ |
| 204 | 146 | public static <T> Predicate<T> isNull() { |
| 205 | | return new Predicate<T>() { |
| 206 | | @Override |
| 207 | | public boolean evaluate(T object) { |
| 208 | | return object == null; |
| 209 | | } |
| 210 | | }; |
| | 147 | return object -> object == null; |
| 211 | 148 | } |
| 212 | 149 | |
| 213 | 150 | } |
diff --git a/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java b/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java
index ed6531a..263857d 100644
|
a
|
b
|
import java.util.NoSuchElementException;
|
| 11 | 11 | * (read-only collection, but elements can be changed, of course) |
| 12 | 12 | * Lets you iterate through those elements of a given collection that satisfy a |
| 13 | 13 | * certain condition (imposed by a predicate). |
| | 14 | * <p> |
| | 15 | * The behaviour of this class is undefined if the underlying collection is changed. |
| 14 | 16 | * @param <S> element type of the underlying collection |
| 15 | 17 | * @param <T> element type of filtered collection (and subclass of S). The predicate |
| 16 | 18 | * must accept only objects of type T. |
| … |
… |
import java.util.NoSuchElementException;
|
| 19 | 21 | public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> { |
| 20 | 22 | |
| 21 | 23 | private final Collection<? extends S> collection; |
| 22 | | private final Predicate<? super S> predicate; |
| | 24 | private final java.util.function.Predicate<? super S> predicate; |
| 23 | 25 | private int size = -1; |
| 24 | 26 | |
| 25 | 27 | private class FilterIterator implements Iterator<T> { |
| … |
… |
public class SubclassFilteredCollection<S, T extends S> extends AbstractCollecti
|
| 35 | 37 | if (current == null) { |
| 36 | 38 | while (iterator.hasNext()) { |
| 37 | 39 | current = iterator.next(); |
| 38 | | if (predicate.evaluate(current)) |
| | 40 | if (predicate.test(current)) |
| 39 | 41 | return; |
| 40 | 42 | } |
| 41 | 43 | current = null; |
| … |
… |
public class SubclassFilteredCollection<S, T extends S> extends AbstractCollecti
|
| 69 | 71 | * Constructs a new {@code SubclassFilteredCollection}. |
| 70 | 72 | * @param collection The base collection to filter |
| 71 | 73 | * @param predicate The predicate to use as filter |
| | 74 | * @deprecated Use java predicates instead. |
| 72 | 75 | */ |
| | 76 | @Deprecated |
| 73 | 77 | public SubclassFilteredCollection(Collection<? extends S> collection, Predicate<? super S> predicate) { |
| | 78 | this(collection, (java.util.function.Predicate<? super S>) predicate); |
| | 79 | } |
| | 80 | |
| | 81 | /** |
| | 82 | * Constructs a new {@code SubclassFilteredCollection}. |
| | 83 | * @param collection The base collection to filter |
| | 84 | * @param predicate The predicate to use as filter |
| | 85 | * @see #filter(Collection, java.util.function.Predicate) for an alternative way to construct this. |
| | 86 | */ |
| | 87 | public SubclassFilteredCollection(Collection<? extends S> collection, java.util.function.Predicate<? super S> predicate) { |
| 74 | 88 | this.collection = collection; |
| 75 | 89 | this.predicate = predicate; |
| 76 | 90 | } |
| … |
… |
public class SubclassFilteredCollection<S, T extends S> extends AbstractCollecti
|
| 97 | 111 | public boolean isEmpty() { |
| 98 | 112 | return !iterator().hasNext(); |
| 99 | 113 | } |
| | 114 | |
| | 115 | /** |
| | 116 | * Create a new filtered collection without any constraints on the predicate type. |
| | 117 | * @param <T> The collection type. |
| | 118 | * @param collection The collection to filter. |
| | 119 | * @param predicate The predicate to filter for. |
| | 120 | * @return The filtered collection. It is a Collection<T>. |
| | 121 | */ |
| | 122 | public static <T> SubclassFilteredCollection<T, T> filter(Collection<T> collection, java.util.function.Predicate<T> predicate) { |
| | 123 | return new SubclassFilteredCollection<T, T>(collection, predicate); |
| | 124 | } |
| 100 | 125 | } |
diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java
index 10fc99e..1f76a84 100644
|
a
|
b
|
import java.util.concurrent.ThreadFactory;
|
| 57 | 57 | import java.util.concurrent.atomic.AtomicLong; |
| 58 | 58 | import java.util.regex.Matcher; |
| 59 | 59 | import java.util.regex.Pattern; |
| | 60 | import java.util.stream.Stream; |
| 60 | 61 | import java.util.zip.GZIPInputStream; |
| 61 | 62 | import java.util.zip.ZipEntry; |
| 62 | 63 | import java.util.zip.ZipFile; |
| … |
… |
public final class Utils {
|
| 101 | 102 | |
| 102 | 103 | /** |
| 103 | 104 | * Tests whether {@code predicate} applies to at least one element from {@code collection}. |
| | 105 | * <p> |
| | 106 | * Note: you can use {@link Stream#anyMatch(java.util.function.Predicate)} instead. |
| 104 | 107 | * @param <T> type of items |
| 105 | 108 | * @param collection the collection |
| 106 | 109 | * @param predicate the predicate |
| … |
… |
public final class Utils {
|
| 117 | 120 | |
| 118 | 121 | /** |
| 119 | 122 | * Tests whether {@code predicate} applies to all elements from {@code collection}. |
| | 123 | * <p> |
| | 124 | * Note: you can use {@link Stream#allMatch(java.util.function.Predicate)} instead. |
| 120 | 125 | * @param <T> type of items |
| 121 | 126 | * @param collection the collection |
| 122 | 127 | * @param predicate the predicate |
| … |
… |
public final class Utils {
|
| 171 | 176 | * @param collection The collection to filter. |
| 172 | 177 | * @param predicate The predicate to filter for. |
| 173 | 178 | * @return The new {@link FilteredCollection} |
| | 179 | * @deprecated Use java predicates and {@link SubclassFilteredCollection#filter(Collection, java.util.function.Predicate)} instead. |
| 174 | 180 | */ |
| | 181 | @Deprecated |
| 175 | 182 | @SuppressWarnings("unused") |
| 176 | 183 | public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) { |
| 177 | 184 | // Diamond operator does not work with Java 9 here |
| … |
… |
public final class Utils {
|
| 780 | 787 | * returns objects of {@code B}. |
| 781 | 788 | * @param <A> class of input objects |
| 782 | 789 | * @param <B> class of transformed objects |
| | 790 | * |
| | 791 | * @deprecated Use java.util.function.Function instead. |
| 783 | 792 | */ |
| | 793 | @Deprecated |
| 784 | 794 | public interface Function<A, B> { |
| 785 | 795 | |
| 786 | 796 | /** |