Changeset 10718 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2016-08-03T16:06:24+02:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/StreamUtils.java
r10638 r10718 2 2 package org.openstreetmap.josm.tools; 3 3 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; 4 import java.util.StringJoiner; 13 5 import java.util.stream.Collector; 14 6 import java.util.stream.Stream; … … 18 10 * Utility methods for streams. 19 11 * @author Michael Zangl 20 * @since 1058521 12 */ 22 13 public final class StreamUtils { 23 24 private static final class HtmlListCollector implements Collector<String, StringBuilder, String> {25 @Override26 public Supplier<StringBuilder> supplier() {27 return StringBuilder::new;28 }29 30 @Override31 public BiConsumer<StringBuilder, String> accumulator() {32 return (sb, item) -> sb.append("<li>").append(item).append("</li>");33 }34 35 @Override36 public BinaryOperator<StringBuilder> combiner() {37 return StringBuilder::append;38 }39 40 @Override41 public Function<StringBuilder, String> finisher() {42 return sb -> "<ul>" + sb.toString() + "</ul>";43 }44 45 @Override46 public Set<Characteristics> characteristics() {47 return EnumSet.of(Characteristics.CONCURRENT);48 }49 }50 14 51 15 /** … … 55 19 56 20 /** 57 * Convert an iterator to a stream.21 * Returns a sequential {@code Stream} with the iterable as its source. 58 22 * @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 61 26 */ 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); 65 29 } 66 30 … … 71 35 */ 72 36 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 ); 74 41 } 75 42 } -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r10717 r10718 319 319 */ 320 320 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()); 322 322 } 323 323 -
trunk/src/org/openstreetmap/josm/tools/bugreport/ReportedException.java
r10586 r10718 257 257 */ 258 258 public boolean mayHaveConcurrentSource() { 259 return StreamUtils.toStream( new CauseTraceIterator())259 return StreamUtils.toStream(CauseTraceIterator::new) 260 260 .anyMatch(t -> t instanceof ConcurrentModificationException || t instanceof InvocationTargetException); 261 261 }
Note:
See TracChangeset
for help on using the changeset viewer.