Changeset 10691 in josm for trunk


Ignore:
Timestamp:
2016-07-31T17:58:31+02:00 (8 years ago)
Author:
Don-vip
Message:

see #11390, fix #12890 - finish transition to Java 8 predicates/functions

Location:
trunk
Files:
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r10608 r10691  
    2121import java.util.concurrent.locks.ReadWriteLock;
    2222import java.util.concurrent.locks.ReentrantReadWriteLock;
     23import java.util.function.Predicate;
    2324
    2425import org.openstreetmap.josm.Main;
     
    319320     * @since 10590
    320321     */
    321     public <T extends OsmPrimitive> Collection<T> getPrimitives(java.util.function.Predicate<? super OsmPrimitive> predicate) {
     322    public <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<? super OsmPrimitive> predicate) {
    322323        return new SubclassFilteredCollection<>(allPrimitives, predicate);
    323324    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r10680 r10691  
    896896                positionString = Utils.getPositionListString(position);
    897897                // if not all objects from the selection are member of this relation
    898                 if (selection.stream().anyMatch(Predicates.not(Predicates.inCollection(members)))) {
     898                if (selection.stream().anyMatch(Predicates.inCollection(members).negate())) {
    899899                    positionString += ",\u2717";
    900900                }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r10689 r10691  
    11351135        public Float aggregateList(List<?> lst) {
    11361136            final List<Float> floats = Utils.transform(lst, (Function<Object, Float>) x -> Cascade.convertTo(x, float.class));
    1137             final Collection<Float> nonNullList = SubclassFilteredCollection.filter(floats, Predicates.not(Predicates.isNull()));
     1137            final Collection<Float> nonNullList = SubclassFilteredCollection.filter(floats, Predicates.<Float>isNull().negate());
    11381138            return nonNullList.isEmpty() ? (Float) Float.NaN : computeMax ? Collections.max(nonNullList) : Collections.min(nonNullList);
    11391139        }
  • trunk/src/org/openstreetmap/josm/tools/FilteredCollection.java

    r10657 r10691  
    33
    44import java.util.Collection;
     5import java.util.function.Predicate;
    56
    67/**
     
    1718     * @param predicate The predicate to use as filter
    1819     */
    19     public FilteredCollection(Collection<? extends T> collection, java.util.function.Predicate<? super T> predicate) {
     20    public FilteredCollection(Collection<? extends T> collection, Predicate<? super T> predicate) {
    2021        super(collection, predicate);
    2122    }
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r10684 r10691  
    1616import java.util.List;
    1717import java.util.Set;
     18import java.util.function.Predicate;
    1819
    1920import org.openstreetmap.josm.Main;
     
    887888     * @return {@code true} if the node is inside the multipolygon
    888889     */
    889     public static boolean isNodeInsideMultiPolygon(Node node, Relation multiPolygon, java.util.function.Predicate<Way> isOuterWayAMatch) {
     890    public static boolean isNodeInsideMultiPolygon(Node node, Relation multiPolygon, Predicate<Way> isOuterWayAMatch) {
    890891        return isPolygonInsideMultiPolygon(Collections.singletonList(node), multiPolygon, isOuterWayAMatch);
    891892    }
     
    901902     * @return {@code true} if the polygon formed by nodes is inside the multipolygon
    902903     */
    903     public static boolean isPolygonInsideMultiPolygon(List<Node> nodes, Relation multiPolygon,
    904             java.util.function.Predicate<Way> isOuterWayAMatch) {
     904    public static boolean isPolygonInsideMultiPolygon(List<Node> nodes, Relation multiPolygon, Predicate<Way> isOuterWayAMatch) {
    905905        // Extract outer/inner members from multipolygon
    906906        final MultiPolygonMembers mpm = new MultiPolygonMembers(multiPolygon);
  • trunk/src/org/openstreetmap/josm/tools/Predicates.java

    r10594 r10691  
    44import java.util.Collection;
    55import java.util.Objects;
     6import java.util.function.Predicate;
    67import java.util.regex.Pattern;
    78
     
    3435    public static <T> Predicate<T> alwaysFalse() {
    3536        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);
    4837    }
    4938
  • trunk/src/org/openstreetmap/josm/tools/SubclassFilteredCollection.java

    r10657 r10691  
    66import java.util.Iterator;
    77import java.util.NoSuchElementException;
     8import java.util.function.Predicate;
    89
    910/**
     
    2223
    2324    private final Collection<? extends S> collection;
    24     private final java.util.function.Predicate<? super S> predicate;
     25    private final Predicate<? super S> predicate;
    2526    private int size = -1;
    2627
     
    7273     * @param collection The base collection to filter
    7374     * @param predicate The predicate to use as filter
    74      * @deprecated Use java predicates instead.
     75     * @see #filter(Collection, Predicate) for an alternative way to construct this.
    7576     */
    76     @Deprecated
    7777    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) {
    8878        this.collection = collection;
    8979        this.predicate = predicate;
     
    120110     * @return The filtered collection. It is a {@code Collection<T>}.
    121111     */
    122     public static <T> SubclassFilteredCollection<T, T> filter(Collection<? extends T> collection, java.util.function.Predicate<T> predicate) {
     112    public static <T> SubclassFilteredCollection<T, T> filter(Collection<? extends T> collection, Predicate<T> predicate) {
    123113        return new SubclassFilteredCollection<>(collection, predicate);
    124114    }
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r10689 r10691  
    5050import java.util.concurrent.ThreadFactory;
    5151import java.util.concurrent.atomic.AtomicLong;
     52import java.util.function.Predicate;
    5253import java.util.regex.Matcher;
    5354import java.util.regex.Pattern;
     
    106107     * @param predicate the predicate
    107108     * @return {@code true} if {@code predicate} applies to at least one element from {@code collection}
    108      * @deprecated use {@link Stream#anyMatch(java.util.function.Predicate)} instead.
     109     * @deprecated use {@link Stream#anyMatch(Predicate)} instead.
    109110     */
    110111    @Deprecated
    111112    public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    112113        for (T item : collection) {
    113             if (predicate.evaluate(item)) {
     114            if (predicate.test(item)) {
    114115                return true;
    115116            }
     
    125126     * @param predicate the predicate
    126127     * @return {@code true} if {@code predicate} applies to all elements from {@code collection}
    127      * @deprecated use {@link Stream#allMatch(java.util.function.Predicate)} instead.
     128     * @deprecated use {@link Stream#allMatch(Predicate)} instead.
    128129     */
    129130    @Deprecated
    130131    public static <T> boolean forAll(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    131         return !exists(collection, Predicates.not(predicate));
     132        return !exists(collection, predicate.negate());
    132133    }
    133134
     
    152153    public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    153154        for (T item : collection) {
    154             if (predicate.evaluate(item)) {
     155            if (predicate.test(item)) {
    155156                return item;
    156157            }
     
    169170    public static <T> T find(Iterable<? extends Object> collection, Class<? extends T> clazz) {
    170171        return (T) find(collection, Predicates.<Object>isInstanceOf(clazz));
    171     }
    172 
    173     /**
    174      * Creates a new {@link FilteredCollection}.
    175      * @param <T> The collection type.
    176      * @param collection The collection to filter.
    177      * @param predicate The predicate to filter for.
    178      * @return The new {@link FilteredCollection}
    179      * @deprecated Use java predicates and {@link SubclassFilteredCollection#filter(Collection, java.util.function.Predicate)} instead.
    180      */
    181     @Deprecated
    182     @SuppressWarnings("unused")
    183     public static <T> Collection<T> filter(Collection<? extends T> collection, Predicate<? super T> predicate) {
    184         // Diamond operator does not work with Java 9 here
    185         return new FilteredCollection<T>(collection, predicate);
    186172    }
    187173
     
    225211        int i = 0;
    226212        for (T item : collection) {
    227             if (predicate.evaluate(item))
     213            if (predicate.test(item))
    228214                return i;
    229215            i++;
     
    758744    public static String escapeReservedCharactersHTML(String s) {
    759745        return s == null ? "" : s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
    760     }
    761 
    762     /**
    763      * Represents a function that can be applied to objects of {@code A} and
    764      * returns objects of {@code B}.
    765      * @param <A> class of input objects
    766      * @param <B> class of transformed objects
    767      *
    768      * @deprecated Use java.util.function.Function instead.
    769      */
    770     @Deprecated
    771     @FunctionalInterface
    772     public interface Function<A, B> extends java.util.function.Function<A, B> {
    773 
    774         /**
    775          * Applies the function on {@code x}.
    776          * @param x an object of
    777          * @return the transformed object
    778          */
    779         @Override
    780         B apply(A x);
    781746    }
    782747
  • trunk/test/unit/org/CustomMatchers.java

    r9246 r10691  
    44import java.awt.geom.Point2D;
    55import java.util.Collection;
     6import java.util.function.Predicate;
    67
    78import org.hamcrest.CustomTypeSafeMatcher;
     
    1213import org.openstreetmap.josm.data.coor.EastNorth;
    1314import org.openstreetmap.josm.data.coor.LatLon;
    14 import org.openstreetmap.josm.tools.Predicate;
    1515
    1616/**
     
    3535            @Override
    3636            protected boolean matchesSafely(T item) {
    37                 return predicate.evaluate(item);
     37                return predicate.test(item);
    3838            }
    3939
  • trunk/test/unit/org/openstreetmap/josm/tools/PredicatesTest.java

    r10553 r10691  
    99import java.util.List;
    1010import java.util.Properties;
     11import java.util.function.Predicate;
    1112import java.util.regex.Pattern;
    1213
     
    3839    public void testAlwaysTrue() {
    3940        Predicate<Object> alwaysTrue = Predicates.alwaysTrue();
    40         assertTrue(alwaysTrue.evaluate(new Object()));
    41         assertTrue(alwaysTrue.evaluate(Boolean.TRUE));
     41        assertTrue(alwaysTrue.test(new Object()));
     42        assertTrue(alwaysTrue.test(Boolean.TRUE));
    4243    }
    4344
     
    4849    public void testAlwaysFalse() {
    4950        Predicate<Object> alwaysFalse = Predicates.alwaysFalse();
    50         assertFalse(alwaysFalse.evaluate(new Object()));
    51         assertFalse(alwaysFalse.evaluate(Boolean.TRUE));
    52     }
    53 
    54     /**
    55      * Test {@link Predicates#not(Predicate)}
    56      */
    57     @Test
    58     public void testNot() {
    59         Predicate<Boolean> not = Predicates.not(new Predicate<Boolean>() {
    60             @Override
    61             public boolean evaluate(Boolean object) {
    62                 return object;
    63             }
    64         });
    65         assertFalse(not.evaluate(Boolean.TRUE));
    66         assertTrue(not.evaluate(Boolean.FALSE));
     51        assertFalse(alwaysFalse.test(new Object()));
     52        assertFalse(alwaysFalse.test(Boolean.TRUE));
    6753    }
    6854
     
    7460        Integer testObject = Integer.valueOf(1);
    7561        Predicate<Integer> equalTo = Predicates.equalTo(testObject);
    76         assertTrue(equalTo.evaluate(testObject));
    77         assertTrue(equalTo.evaluate(Integer.valueOf(1)));
     62        assertTrue(equalTo.test(testObject));
     63        assertTrue(equalTo.test(Integer.valueOf(1)));
    7864
    79         assertFalse(equalTo.evaluate(Integer.valueOf(2)));
    80         assertFalse(equalTo.evaluate(null));
     65        assertFalse(equalTo.test(Integer.valueOf(2)));
     66        assertFalse(equalTo.test(null));
    8167    }
    8268
     
    8773    public void testIsOfClass() {
    8874        Predicate<Object> isOfClass = Predicates.<Object>isOfClass(Hashtable.class);
    89         assertFalse(isOfClass.evaluate(null));
    90         assertFalse(isOfClass.evaluate(new Object()));
    91         assertFalse(isOfClass.evaluate(new Properties()));
    92         assertTrue(isOfClass.evaluate(new Hashtable<>()));
     75        assertFalse(isOfClass.test(null));
     76        assertFalse(isOfClass.test(new Object()));
     77        assertFalse(isOfClass.test(new Properties()));
     78        assertTrue(isOfClass.test(new Hashtable<>()));
    9379    }
    9480
     
    9985    public void testIsInstanceOf() {
    10086        Predicate<Object> isInstanceOf = Predicates.<Object>isInstanceOf(Hashtable.class);
    101         assertFalse(isInstanceOf.evaluate(null));
    102         assertFalse(isInstanceOf.evaluate(new Object()));
    103         assertTrue(isInstanceOf.evaluate(new Properties()));
    104         assertTrue(isInstanceOf.evaluate(new Hashtable<>()));
     87        assertFalse(isInstanceOf.test(null));
     88        assertFalse(isInstanceOf.test(new Object()));
     89        assertTrue(isInstanceOf.test(new Properties()));
     90        assertTrue(isInstanceOf.test(new Hashtable<>()));
    10591    }
    10692
     
    11298        Pattern p = Pattern.compile("ab?c");
    11399        Predicate<String> stringMatchesPattern = Predicates.stringMatchesPattern(p);
    114         assertFalse(stringMatchesPattern.evaluate(""));
    115         assertFalse(stringMatchesPattern.evaluate("a"));
    116         assertFalse(stringMatchesPattern.evaluate("xabcx"));
    117         assertTrue(stringMatchesPattern.evaluate("ac"));
    118         assertTrue(stringMatchesPattern.evaluate("abc"));
     100        assertFalse(stringMatchesPattern.test(""));
     101        assertFalse(stringMatchesPattern.test("a"));
     102        assertFalse(stringMatchesPattern.test("xabcx"));
     103        assertTrue(stringMatchesPattern.test("ac"));
     104        assertTrue(stringMatchesPattern.test("abc"));
    119105    }
    120106
     
    126112        Pattern p = Pattern.compile("ab?c");
    127113        Predicate<String> stringContainsPattern = Predicates.stringContainsPattern(p);
    128         assertFalse(stringContainsPattern.evaluate(""));
    129         assertFalse(stringContainsPattern.evaluate("a"));
    130         assertTrue(stringContainsPattern.evaluate("xabcx"));
    131         assertTrue(stringContainsPattern.evaluate("ac"));
    132         assertTrue(stringContainsPattern.evaluate("abc"));
    133         assertTrue(stringContainsPattern.evaluate("xx\nabc\nx"));
     114        assertFalse(stringContainsPattern.test(""));
     115        assertFalse(stringContainsPattern.test("a"));
     116        assertTrue(stringContainsPattern.test("xabcx"));
     117        assertTrue(stringContainsPattern.test("ac"));
     118        assertTrue(stringContainsPattern.test("abc"));
     119        assertTrue(stringContainsPattern.test("xx\nabc\nx"));
    134120    }
    135121
     
    140126    public void testStringContains() {
    141127        Predicate<String> stringContains = Predicates.stringContains("abc");
    142         assertFalse(stringContains.evaluate(""));
    143         assertFalse(stringContains.evaluate("a"));
    144         assertTrue(stringContains.evaluate("xabcx"));
    145         assertFalse(stringContains.evaluate("ac"));
    146         assertTrue(stringContains.evaluate("abc"));
     128        assertFalse(stringContains.test(""));
     129        assertFalse(stringContains.test("a"));
     130        assertTrue(stringContains.test("xabcx"));
     131        assertFalse(stringContains.test("ac"));
     132        assertTrue(stringContains.test("abc"));
    147133    }
    148134
     
    154140        Predicate<OsmPrimitive> hasTag = Predicates.hasTag("key", "value");
    155141        Node n1 = new Node();
    156         assertFalse(hasTag.evaluate(n1));
     142        assertFalse(hasTag.test(n1));
    157143        n1.put("Key", "x");
    158         assertFalse(hasTag.evaluate(n1));
     144        assertFalse(hasTag.test(n1));
    159145        n1.put("key", "x");
    160         assertFalse(hasTag.evaluate(n1));
     146        assertFalse(hasTag.test(n1));
    161147        n1.put("key", "value");
    162         assertTrue(hasTag.evaluate(n1));
     148        assertTrue(hasTag.test(n1));
    163149    }
    164150
     
    170156        Predicate<OsmPrimitive> hasKey = Predicates.hasKey("key");
    171157        Node n1 = new Node();
    172         assertFalse(hasKey.evaluate(n1));
     158        assertFalse(hasKey.test(n1));
    173159        n1.put("Key", "x");
    174         assertFalse(hasKey.evaluate(n1));
     160        assertFalse(hasKey.test(n1));
    175161        n1.put("key", "x");
    176         assertTrue(hasKey.evaluate(n1));
     162        assertTrue(hasKey.test(n1));
    177163    }
    178164
     
    184170        List<String> list = Arrays.asList("a", "b", "c");
    185171        Predicate<String> inCollection = Predicates.inCollection(list);
    186         assertTrue(inCollection.evaluate("a"));
    187         assertTrue(inCollection.evaluate("c"));
    188         assertFalse(inCollection.evaluate("d"));
    189         assertFalse(inCollection.evaluate(null));
     172        assertTrue(inCollection.test("a"));
     173        assertTrue(inCollection.test("c"));
     174        assertFalse(inCollection.test("d"));
     175        assertFalse(inCollection.test(null));
    190176
    191177        List<String> list2 = Arrays.asList("a", "b", "c", null);
    192178        Predicate<String> inCollection2 = Predicates.inCollection(list2);
    193         assertTrue(inCollection2.evaluate("a"));
    194         assertTrue(inCollection2.evaluate("c"));
    195         assertFalse(inCollection2.evaluate("d"));
    196         assertTrue(inCollection2.evaluate(null));
     179        assertTrue(inCollection2.test("a"));
     180        assertTrue(inCollection2.test("c"));
     181        assertFalse(inCollection2.test("d"));
     182        assertTrue(inCollection2.test(null));
    197183    }
    198184
     
    203189    public void testIsNull() {
    204190        Predicate<Object> isNull = Predicates.isNull();
    205         assertTrue(isNull.evaluate(null));
    206         assertFalse(isNull.evaluate(Integer.valueOf(2)));
     191        assertTrue(isNull.test(null));
     192        assertFalse(isNull.test(Integer.valueOf(2)));
    207193    }
    208194}
Note: See TracChangeset for help on using the changeset viewer.