Changeset 10582 in josm for trunk/src/org


Ignore:
Timestamp:
2016-07-21T02:15:38+02:00 (8 years ago)
Author:
Don-vip
Message:

see #11390, fix #12908 - Java 8: Move to java Predicates (patch by michael2402) - gsoc-core

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Predicate.java

    r8512 r10582  
    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    /**
     
    1619     */
    1720    boolean evaluate(T object);
     21
     22    @Override
     23    default boolean test(T t) {
     24        return evaluate(t);
     25    }
    1826}
  • trunk/src/org/openstreetmap/josm/tools/Predicates.java

    r10286 r10582  
    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
     
    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
     
    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
     
    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
     
    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
     
    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
     
    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
     
    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
     
    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
     
    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 p -> p.hasTag(key, values);
    168120    }
    169121
     
    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
     
    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
     
    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
  • trunk/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java

    r9854 r10582  
    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
     
    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
     
    3638                while (iterator.hasNext()) {
    3739                    current = iterator.next();
    38                     if (predicate.evaluate(current))
     40                    if (predicate.test(current))
    3941                        return;
    4042                }
     
    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;
     
    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}
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r10570 r10582  
    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;
     
    105106    /**
    106107     * Tests whether {@code predicate} applies to at least one element from {@code collection}.
     108     * <p>
     109     * Note: you can use {@link Stream#anyMatch(java.util.function.Predicate)} instead.
    107110     * @param <T> type of items
    108111     * @param collection the collection
     
    121124    /**
    122125     * Tests whether {@code predicate} applies to all elements from {@code collection}.
     126     * <p>
     127     * Note: you can use {@link Stream#allMatch(java.util.function.Predicate)} instead.
    123128     * @param <T> type of items
    124129     * @param collection the collection
     
    175180     * @param predicate The predicate to filter for.
    176181     * @return The new {@link FilteredCollection}
    177      */
     182     * @deprecated Use java predicates and {@link SubclassFilteredCollection#filter(Collection, java.util.function.Predicate)} instead.
     183     */
     184    @Deprecated
    178185    @SuppressWarnings("unused")
    179186    public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) {
     
    798805     * @param <A> class of input objects
    799806     * @param <B> class of transformed objects
    800      */
     807     *
     808     * @deprecated Use java.util.function.Function instead.
     809     */
     810    @Deprecated
    801811    public interface Function<A, B> {
    802812
Note: See TracChangeset for help on using the changeset viewer.