Ticket #11390: java8-utils.patch

File java8-utils.patch, 7.4 KB (added by simon04, 9 years ago)
  • 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 fb4f678..16bc79d 100644
    a b  
    5050import java.util.List;
    5151import java.util.Locale;
    5252import java.util.Objects;
     53import java.util.StringJoiner;
    5354import java.util.concurrent.Executor;
    5455import java.util.concurrent.ForkJoinPool;
    5556import java.util.concurrent.ForkJoinWorkerThread;
     
    5758import java.util.concurrent.atomic.AtomicLong;
    5859import java.util.regex.Matcher;
    5960import java.util.regex.Pattern;
     61import java.util.stream.Collector;
     62import java.util.stream.Collectors;
    6063import java.util.stream.Stream;
     64import java.util.stream.StreamSupport;
    6165import java.util.zip.GZIPInputStream;
    6266import java.util.zip.ZipEntry;
    6367import java.util.zip.ZipFile;
    private Utils() {  
    104108    }
    105109
    106110    /**
     111     * Returns a sequential {@code Stream} with this collection as its source.
     112     *
     113     * @see Collection#stream()
     114     */
     115    private static <T> Stream<T> stream(Iterable<T> collection) {
     116        return StreamSupport.stream(collection.spliterator(), false);
     117    }
     118
     119    /**
    107120     * Tests whether {@code predicate} applies to at least one element from {@code collection}.
    108121     * <p>
    109122     * Note: you can use {@link Stream#anyMatch(java.util.function.Predicate)} instead.
    private Utils() {  
    111124     * @param collection the collection
    112125     * @param predicate the predicate
    113126     * @return {@code true} if {@code predicate} applies to at least one element from {@code collection}
     127     * @deprecated Use {@link Stream#anyMatch} instead.
    114128     */
     129    @Deprecated
    115130    public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    116         for (T item : collection) {
    117             if (predicate.evaluate(item)) {
    118                 return true;
    119             }
    120         }
    121         return false;
     131        return stream(collection)
     132                .anyMatch(predicate);
    122133    }
    123134
    124135    /**
    private Utils() {  
    129140     * @param collection the collection
    130141     * @param predicate the predicate
    131142     * @return {@code true} if {@code predicate} applies to all elements from {@code collection}
     143     * @deprecated Use {@link Stream#allMatch} instead.
    132144     */
     145    @Deprecated
    133146    public static <T> boolean forAll(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    134         return !exists(collection, Predicates.not(predicate));
     147        return stream(collection)
     148                .allMatch(predicate);
    135149    }
    136150
    137151    /**
    private Utils() {  
    140154     * @param collection The collection
    141155     * @param clazz The class to search for.
    142156     * @return <code>true</code> if that item exists in the collection.
     157     * @deprecated Use {@link Stream#anyMatch} instead.
    143158     */
    144159    public static <T> boolean exists(Iterable<T> collection, Class<? extends T> clazz) {
    145         return exists(collection, Predicates.<T>isInstanceOf(clazz));
     160        return stream(collection)
     161                .anyMatch(clazz::isInstance);
    146162    }
    147163
    148164    /**
    private Utils() {  
    153169     * @return the item or <code>null</code> if there was not match.
    154170     */
    155171    public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
    156         for (T item : collection) {
    157             if (predicate.evaluate(item)) {
    158                 return item;
    159             }
    160         }
    161         return null;
     172        return stream(collection)
     173                .filter(predicate)
     174                .findFirst()
     175                .orElse(null);
    162176    }
    163177
    164178    /**
    private Utils() {  
    196210     */
    197211    @SafeVarargs
    198212    public static <T> T firstNonNull(T... items) {
    199         for (T i : items) {
    200             if (i != null) {
    201                 return i;
    202             }
    203         }
    204         return null;
     213        return Stream.of(items)
     214                .filter(Objects::nonNull)
     215                .findFirst()
     216                .orElse(null);
    205217    }
    206218
    207219    /**
    private Utils() {  
    218230    }
    219231
    220232    /**
     233     * Filter an iterable by (sub)class.
     234     *
     235     * @param <S> Super type of items
     236     * @param <T> type of items
     237     * @param iterable the iterable to filter
     238     * @param clazz the (sub)class
     239     * @return a stream of (sub)class elements
     240     */
     241    public static <S, T extends S> Stream<T> filter(Iterable<S> iterable, Class<T> clazz) {
     242        return stream(iterable).filter(clazz::isInstance).map(clazz::cast);
     243    }
     244
     245    /**
    221246     * Find the index of the first item that matches the predicate.
    222247     * @param <T> The iterable type
    223248     * @param collection The iterable to iterate over.
    public static int mod(int a, int n) {  
    306331     * @param values collection of objects, null is converted to the
    307332     *  empty string
    308333     * @return null if values is null. The joined string otherwise.
     334     * @deprecated Use {@link String#join} or {@link Collectors#joining} instead.
    309335     */
     336    @Deprecated
    310337    public static String join(String sep, Collection<?> values) {
    311         CheckParameterUtil.ensureParameterNotNull(sep, "sep");
    312         if (values == null)
    313             return null;
    314         StringBuilder s = null;
    315         for (Object a : values) {
    316             if (a == null) {
    317                 a = "";
    318             }
    319             if (s != null) {
    320                 s.append(sep).append(a);
    321             } else {
    322                 s = new StringBuilder(a.toString());
    323             }
    324         }
    325         return s != null ? s.toString() : "";
     338        return values.stream()
     339                .map(Objects::toString)
     340                .collect(Collectors.joining(sep));
    326341    }
    327342
    328343    /**
    public static String join(String sep, Collection<?> values) {  
    331346     * @return An unordered HTML list
    332347     */
    333348    public static String joinAsHtmlUnorderedList(Iterable<?> values) {
    334         StringBuilder sb = new StringBuilder(1024);
    335         sb.append("<ul>");
    336         for (Object i : values) {
    337             sb.append("<li>").append(i).append("</li>");
    338         }
    339         sb.append("</ul>");
    340         return sb.toString();
     349        return stream(values)
     350                .map(Objects::toString)
     351                .collect(Collector.of(
     352                        () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"),
     353                        StringJoiner::add, StringJoiner::merge, StringJoiner::toString
     354                ));
    341355    }
    342356
    343357    /**
  • 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}