Ignore:
Timestamp:
2016-07-25T21:19:38+02:00 (5 years ago)
Author:
Don-vip
Message:

see #11390, fix #13206 - Migrate Function interface to java 8 (patch by michael2402, modified) - gsoc-core

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

Legend:

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

    r10585 r10638  
    22package org.openstreetmap.josm.tools;
    33
     4import java.util.EnumSet;
    45import java.util.Iterator;
     6import java.util.Set;
    57import java.util.Spliterator;
    68import java.util.Spliterators;
     9import java.util.function.BiConsumer;
     10import java.util.function.BinaryOperator;
     11import java.util.function.Function;
     12import java.util.function.Supplier;
     13import java.util.stream.Collector;
    714import java.util.stream.Stream;
    815import java.util.stream.StreamSupport;
     
    1421 */
    1522public 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    }
    1650
    1751    /**
     
    3064        return StreamSupport.stream(spliterator, false);
    3165    }
     66
     67    /**
     68     * Creates a new Collector that collects the items and returns them as HTML unordered list.
     69     * @return The collector.
     70     * @since 10638
     71     */
     72    public static Collector<String, ?, String> toHtmlList() {
     73        return new HtmlListCollector();
     74    }
    3275}
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r10627 r10638  
    327327     */
    328328    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();
     329        return StreamUtils.toStream(values.iterator()).map(x -> x.toString()).collect(StreamUtils.toHtmlList());
    336330    }
    337331
Note: See TracChangeset for help on using the changeset viewer.