001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.util.StringJoiner;
005import java.util.stream.Collector;
006import java.util.stream.Stream;
007import java.util.stream.StreamSupport;
008
009/**
010 * Utility methods for streams.
011 * @author Michael Zangl
012 */
013public final class StreamUtils {
014
015    /**
016     * Utility class
017     */
018    private StreamUtils() {
019        // Hide default constructor for utility classes
020    }
021
022    /**
023     * Returns a sequential {@code Stream} with the iterable as its source.
024     * @param <T> The element type to iterate over
025     * @param iterable The iterable
026     * @return The stream of for that iterable.
027     * @since 10718
028     */
029    public static <T> Stream<T> toStream(Iterable<T> iterable) {
030        return StreamSupport.stream(iterable.spliterator(), false);
031    }
032
033    /**
034     * Creates a new Collector that collects the items and returns them as HTML unordered list.
035     * @return The collector.
036     * @since 10638
037     */
038    public static Collector<String, ?, String> toHtmlList() {
039        return Collector.of(
040                () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"),
041                StringJoiner::add, StringJoiner::merge, StringJoiner::toString
042        );
043    }
044}