Ticket #11390: java8-utils-v2.patch

File java8-utils-v2.patch, 9.1 KB (added by simon04, 9 years ago)
  • src/org/openstreetmap/josm/tools/StreamUtils.java

    diff --git a/src/org/openstreetmap/josm/tools/StreamUtils.java b/src/org/openstreetmap/josm/tools/StreamUtils.java
    index 8f39c48..782bd9c 100644
    a b  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools;
    33
    4 import java.util.Iterator;
    5 import java.util.Spliterator;
    6 import java.util.Spliterators;
     4import java.util.Collection;
    75import java.util.stream.Stream;
    86import java.util.stream.StreamSupport;
    97
     
    2018    private StreamUtils() {}
    2119
    2220    /**
    23      * Convert an iterator to a stream.
    24      * @param <T> The element type to iterate over
    25      * @param iterator The iterator
    26      * @return The stream of for that iterator.
     21     * Returns a sequential {@code Stream} with the iterable as its source.
     22     *
     23     * @param <T>      The element type to iterate over
     24     * @param iterable The iterable
     25     * @return The stream of for that iterable.
     26     * @see Collection#stream()
    2727     */
    28     public static <T> Stream<T> toStream(Iterator<? extends T> iterator) {
    29         Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED);
    30         return StreamSupport.stream(spliterator, false);
     28    public static <T> Stream<T> toStream(Iterable<T> iterable) {
     29        return StreamSupport.stream(iterable.spliterator(), false);
     30    }
     31
     32    /**
     33     * Filter an iterable by (sub)class.
     34     *
     35     * @param <S> Super type of items
     36     * @param <T> type of items
     37     * @param iterable the iterable to filter
     38     * @param clazz the (sub)class
     39     * @return a stream of (sub)class elements
     40     */
     41    public static <S, T extends S> Stream<T> filter(Iterable<S> iterable, Class<T> clazz) {
     42        return StreamUtils.toStream(iterable).filter(clazz::isInstance).map(clazz::cast);
    3143    }
    3244}
  • 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 277caf5..3499b5d 100644
    a b  
    4444import java.util.List;
    4545import java.util.Locale;
    4646import java.util.Objects;
     47import java.util.StringJoiner;
    4748import java.util.concurrent.Executor;
    4849import java.util.concurrent.ForkJoinPool;
    4950import java.util.concurrent.ForkJoinWorkerThread;
     
    5152import java.util.concurrent.atomic.AtomicLong;
    5253import java.util.regex.Matcher;
    5354import java.util.regex.Pattern;
     55import java.util.stream.Collector;
     56import java.util.stream.Collectors;
    5457import java.util.stream.Stream;
    5558import java.util.zip.GZIPInputStream;
    5659import java.util.zip.ZipEntry;
    private Utils() {  
    106109     * @param collection the collection
    107110     * @param predicate the predicate
    108111     * @return {@code true} if {@code predicate} applies to at least one element from {@code collection}
     112     * @deprecated Use {@link Stream#anyMatch} instead.
    109113     */
     114    @Deprecated
    110115    public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    111         for (T item : collection) {
    112             if (predicate.evaluate(item)) {
    113                 return true;
    114             }
    115         }
    116         return false;
     116        return StreamUtils.toStream(collection)
     117                .anyMatch(predicate);
    117118    }
    118119
    119120    /**
    private Utils() {  
    124125     * @param collection the collection
    125126     * @param predicate the predicate
    126127     * @return {@code true} if {@code predicate} applies to all elements from {@code collection}
     128     * @deprecated Use {@link Stream#allMatch} instead.
    127129     */
     130    @Deprecated
    128131    public static <T> boolean forAll(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    129         return !exists(collection, Predicates.not(predicate));
     132        return StreamUtils.toStream(collection)
     133                .allMatch(predicate);
    130134    }
    131135
    132136    /**
    private Utils() {  
    135139     * @param collection The collection
    136140     * @param clazz The class to search for.
    137141     * @return <code>true</code> if that item exists in the collection.
     142     * @deprecated Use {@link Stream#anyMatch} instead.
    138143     */
     144    @Deprecated
    139145    public static <T> boolean exists(Iterable<T> collection, Class<? extends T> clazz) {
    140         return exists(collection, Predicates.<T>isInstanceOf(clazz));
     146        return StreamUtils.toStream(collection)
     147                .anyMatch(clazz::isInstance);
    141148    }
    142149
    143150    /**
    private Utils() {  
    146153     * @param collection The iterable to search in.
    147154     * @param predicate The predicate to match
    148155     * @return the item or <code>null</code> if there was not match.
     156     * @deprecated Use {@code stream}{@link Stream#filter .filter(...)}{@link Stream#findFirst ,findFirst()} instead.
    149157     */
     158    @Deprecated
    150159    public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    151         for (T item : collection) {
    152             if (predicate.evaluate(item)) {
    153                 return item;
    154             }
    155         }
    156         return null;
     160        return StreamUtils.toStream(collection)
     161                .filter(predicate)
     162                .findFirst()
     163                .orElse(null);
    157164    }
    158165
    159166    /**
    private Utils() {  
    191198     */
    192199    @SafeVarargs
    193200    public static <T> T firstNonNull(T... items) {
    194         for (T i : items) {
    195             if (i != null) {
    196                 return i;
    197             }
    198         }
    199         return null;
     201        return Stream.of(items)
     202                .filter(Objects::nonNull)
     203                .findFirst()
     204                .orElse(null);
    200205    }
    201206
    202207    /**
    public static int mod(int a, int n) {  
    301306     * @param values collection of objects, null is converted to the
    302307     *  empty string
    303308     * @return null if values is null. The joined string otherwise.
     309     * @deprecated Use {@link String#join} or {@link Collectors#joining} instead.
    304310     */
     311    @Deprecated
    305312    public static String join(String sep, Collection<?> values) {
    306         CheckParameterUtil.ensureParameterNotNull(sep, "sep");
    307         if (values == null)
    308             return null;
    309         StringBuilder s = null;
    310         for (Object a : values) {
    311             if (a == null) {
    312                 a = "";
    313             }
    314             if (s != null) {
    315                 s.append(sep).append(a);
    316             } else {
    317                 s = new StringBuilder(a.toString());
    318             }
    319         }
    320         return s != null ? s.toString() : "";
     313        return values.stream()
     314                .map(Objects::toString)
     315                .collect(Collectors.joining(sep));
    321316    }
    322317
    323318    /**
    public static String join(String sep, Collection<?> values) {  
    326321     * @return An unordered HTML list
    327322     */
    328323    public static String joinAsHtmlUnorderedList(Iterable<?> values) {
    329         StringBuilder sb = new StringBuilder(1024);
    330         sb.append("<ul>");
    331         for (Object i : values) {
    332             sb.append("<li>").append(i).append("</li>");
    333         }
    334         sb.append("</ul>");
    335         return sb.toString();
     324        return StreamUtils.toStream(values)
     325                .map(Objects::toString)
     326                .collect(Collector.of(
     327                        () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"),
     328                        StringJoiner::add, StringJoiner::merge, StringJoiner::toString
     329                ));
    336330    }
    337331
    338332    /**
  • src/org/openstreetmap/josm/tools/bugreport/ReportedException.java

    diff --git a/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java b/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java
    index 434ca68..22efa08 100644
    a b public String toString() {  
    256256     * @since 10585
    257257     */
    258258    public boolean mayHaveConcurrentSource() {
    259         return StreamUtils.toStream(new CauseTraceIterator())
     259        return StreamUtils.toStream(CauseTraceIterator::new)
    260260                .anyMatch(t -> t instanceof ConcurrentModificationException || t instanceof InvocationTargetException);
    261261    }
    262262
  • test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/UtilsTest.java b/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
    index cbe2b8b..89bec7a 100644
    a b public void testSizeStringNegative() throws Exception {  
    192192        Utils.getSizeString(-1, Locale.ENGLISH);
    193193    }
    194194
     195    /**
     196     * Test of {@link Utils#joinAsHtmlUnorderedList(Iterable)} method.
     197     */
     198    @Test
     199    public void testJoinAsHtmlUnorderedList() {
     200        assertEquals("<ul></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList()));
     201        assertEquals("<ul><li>one</li></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList("one")));
     202        assertEquals("<ul><li>one</li><li>two</li></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList("one", "two")));
     203        assertEquals("<ul><li>one</li><li>two</li><li>many</li></ul>", Utils.joinAsHtmlUnorderedList(Arrays.asList("one", "two", "many")));
     204    }
     205
    195206}