Ticket #12908: patch-java-8-predicates.patch

File patch-java-8-predicates.patch, 12.6 KB (added by michael2402, 10 years ago)
  • src/org/openstreetmap/josm/tools/Predicate.java

    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;  
    66 *
    77 * @param <T> The objects type
    88 * @since 3177
     9 * @deprecated Use {@link java.util.function.Predicate} instead.
    910 */
    10 public interface Predicate<T> {
     11@Deprecated
     12@FunctionalInterface
     13public interface Predicate<T> extends java.util.function.Predicate<T> {
    1114
    1215    /**
    1316     * Determines whether the object passes the test or not
    public interface Predicate<T> {  
    1518     * @return {@code true} if the object passes the test, {@code false} otherwise
    1619     */
    1720    boolean evaluate(T object);
     21
     22    @Override
     23    default boolean test(T t) {
     24        return evaluate(t);
     25    }
    1826}
  • src/org/openstreetmap/josm/tools/Predicates.java

    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 {  
    2222     * @since 10040
    2323     */
    2424    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;
    3126    }
    3227
    3328    /**
    public final class Predicates {  
    3732     * @since 10040
    3833     */
    3934    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;
    4636    }
    4737
    4838    /**
    public final class Predicates {  
    5040     * @param <T> type of items
    5141     * @param predicate the predicate to negate
    5242     * @return the negation of {@code predicate}
     43     * @deprecated Use {@link java.util.function.Predicate#negate()}
    5344     */
     45    @Deprecated
    5446    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);
    6148    }
    6249
    6350    /**
    public final class Predicates {  
    6754     * @return a {@link Predicate} executing {@link Objects#equals}
    6855     */
    6956    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);
    7658    }
    7759
    7860    /**
    public final class Predicates {  
    8466     */
    8567    public static <T> Predicate<T> isOfClass(final Class<? extends T> clazz) {
    8668        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;
    9370    }
    9471
    9572    /**
    public final class Predicates {  
    10279     */
    10380    public static <T> Predicate<T> isInstanceOf(final Class<? extends T> clazz) {
    10481        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);
    11183    }
    11284
    11385    /**
    public final class Predicates {  
    11688     * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#matches}
    11789     */
    11890    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();
    12592    }
    12693
    12794    /**
    public final class Predicates {  
    13097     * @return a {@link Predicate} executing {@link Pattern#matcher(CharSequence)} and {@link java.util.regex.Matcher#find}
    13198     */
    13299    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();
    139101    }
    140102
    141103    /**
    public final class Predicates {  
    144106     * @return a {@link Predicate} executing {@link String#contains(CharSequence)}
    145107     */
    146108    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);
    153110    }
    154111
    155112    /**
    public final class Predicates {  
    159116     * @return a {@link Predicate} executing {@link OsmPrimitive#hasTag(String, String...)}
    160117     */
    161118    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);
    168120    }
    169121
    170122    /**
    public final class Predicates {  
    173125     * @return a {@link Predicate} executing {@link OsmPrimitive#hasKey(String)}
    174126     */
    175127    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);
    182129    }
    183130
    184131    /**
    public final class Predicates {  
    188135     * @return a {@link Predicate} executing {@link Collection#contains(Object)}
    189136     */
    190137    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);
    197139    }
    198140
    199141    /**
    public final class Predicates {  
    202144     * @return a {@link Predicate} testing whether objects are {@code null}
    203145     */
    204146    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;
    211148    }
    212149
    213150}
  • src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java

    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;  
    1111 * (read-only collection, but elements can be changed, of course)
    1212 * Lets you iterate through those elements of a given collection that satisfy a
    1313 * certain condition (imposed by a predicate).
     14 * <p>
     15 * The behaviour of this class is undefined if the underlying collection is changed.
    1416 * @param <S> element type of the underlying collection
    1517 * @param <T> element type of filtered collection (and subclass of S). The predicate
    1618 *      must accept only objects of type T.
    import java.util.NoSuchElementException;  
    1921public class SubclassFilteredCollection<S, T extends S> extends AbstractCollection<T> {
    2022
    2123    private final Collection<? extends S> collection;
    22     private final Predicate<? super S> predicate;
     24    private final java.util.function.Predicate<? super S> predicate;
    2325    private int size = -1;
    2426
    2527    private class FilterIterator implements Iterator<T> {
    public class SubclassFilteredCollection<S, T extends S> extends AbstractCollecti  
    3537            if (current == null) {
    3638                while (iterator.hasNext()) {
    3739                    current = iterator.next();
    38                     if (predicate.evaluate(current))
     40                    if (predicate.test(current))
    3941                        return;
    4042                }
    4143                current = null;
    public class SubclassFilteredCollection<S, T extends S> extends AbstractCollecti  
    6971     * Constructs a new {@code SubclassFilteredCollection}.
    7072     * @param collection The base collection to filter
    7173     * @param predicate The predicate to use as filter
     74     * @deprecated Use java predicates instead.
    7275     */
     76    @Deprecated
    7377    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) {
    7488        this.collection = collection;
    7589        this.predicate = predicate;
    7690    }
    public class SubclassFilteredCollection<S, T extends S> extends AbstractCollecti  
    97111    public boolean isEmpty() {
    98112        return !iterator().hasNext();
    99113    }
     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    }
    100125}
  • src/org/openstreetmap/josm/tools/Utils.java

    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;  
    5757import java.util.concurrent.atomic.AtomicLong;
    5858import java.util.regex.Matcher;
    5959import java.util.regex.Pattern;
     60import java.util.stream.Stream;
    6061import java.util.zip.GZIPInputStream;
    6162import java.util.zip.ZipEntry;
    6263import java.util.zip.ZipFile;
    public final class Utils {  
    101102
    102103    /**
    103104     * 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.
    104107     * @param <T> type of items
    105108     * @param collection the collection
    106109     * @param predicate the predicate
    public final class Utils {  
    117120
    118121    /**
    119122     * 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.
    120125     * @param <T> type of items
    121126     * @param collection the collection
    122127     * @param predicate the predicate
    public final class Utils {  
    171176     * @param collection The collection to filter.
    172177     * @param predicate The predicate to filter for.
    173178     * @return The new {@link FilteredCollection}
     179     * @deprecated Use java predicates and {@link SubclassFilteredCollection#filter(Collection, java.util.function.Predicate)} instead.
    174180     */
     181    @Deprecated
    175182    @SuppressWarnings("unused")
    176183    public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) {
    177184        // Diamond operator does not work with Java 9 here
    public final class Utils {  
    780787     * returns objects of {@code B}.
    781788     * @param <A> class of input objects
    782789     * @param <B> class of transformed objects
     790     *
     791     * @deprecated Use java.util.function.Function instead.
    783792     */
     793    @Deprecated
    784794    public interface Function<A, B> {
    785795
    786796        /**