Changeset 10718 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-08-03T16:06:24+02:00 (8 years ago)
Author:
simon04
Message:

see #11390 - Simplify StreamUtils

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

Legend:

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

    r10638 r10718  
    22package org.openstreetmap.josm.tools;
    33
    4 import java.util.EnumSet;
    5 import java.util.Iterator;
    6 import java.util.Set;
    7 import java.util.Spliterator;
    8 import java.util.Spliterators;
    9 import java.util.function.BiConsumer;
    10 import java.util.function.BinaryOperator;
    11 import java.util.function.Function;
    12 import java.util.function.Supplier;
     4import java.util.StringJoiner;
    135import java.util.stream.Collector;
    146import java.util.stream.Stream;
     
    1810 * Utility methods for streams.
    1911 * @author Michael Zangl
    20  * @since 10585
    2112 */
    2213public final class StreamUtils {
    23 
    24     private static final class HtmlListCollector implements Collector<String, StringBuilder, String> {
    25         @Override
    26         public Supplier<StringBuilder> supplier() {
    27             return StringBuilder::new;
    28         }
    29 
    30         @Override
    31         public BiConsumer<StringBuilder, String> accumulator() {
    32             return (sb, item) -> sb.append("<li>").append(item).append("</li>");
    33         }
    34 
    35         @Override
    36         public BinaryOperator<StringBuilder> combiner() {
    37             return StringBuilder::append;
    38         }
    39 
    40         @Override
    41         public Function<StringBuilder, String> finisher() {
    42             return sb -> "<ul>" + sb.toString() + "</ul>";
    43         }
    44 
    45         @Override
    46         public Set<Characteristics> characteristics() {
    47             return EnumSet.of(Characteristics.CONCURRENT);
    48         }
    49     }
    5014
    5115    /**
     
    5519
    5620    /**
    57      * Convert an iterator to a stream.
     21     * Returns a sequential {@code Stream} with the iterable as its source.
    5822     * @param <T> The element type to iterate over
    59      * @param iterator The iterator
    60      * @return The stream of for that iterator.
     23     * @param iterable The iterable
     24     * @return The stream of for that iterable.
     25     * @since 10718
    6126     */
    62     public static <T> Stream<T> toStream(Iterator<? extends T> iterator) {
    63         Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED);
    64         return StreamSupport.stream(spliterator, false);
     27    public static <T> Stream<T> toStream(Iterable<T> iterable) {
     28        return StreamSupport.stream(iterable.spliterator(), false);
    6529    }
    6630
     
    7135     */
    7236    public static Collector<String, ?, String> toHtmlList() {
    73         return new HtmlListCollector();
     37        return Collector.of(
     38                () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"),
     39                StringJoiner::add, StringJoiner::merge, StringJoiner::toString
     40        );
    7441    }
    7542}
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r10717 r10718  
    319319     */
    320320    public static String joinAsHtmlUnorderedList(Iterable<?> values) {
    321         return StreamUtils.toStream(values.iterator()).map(Object::toString).collect(StreamUtils.toHtmlList());
     321        return StreamUtils.toStream(values).map(Object::toString).collect(StreamUtils.toHtmlList());
    322322    }
    323323
  • trunk/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java

    r10586 r10718  
    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    }
Note: See TracChangeset for help on using the changeset viewer.