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
020    /**
021     * Returns a sequential {@code Stream} with the iterable as its source.
022     * @param <T> The element type to iterate over
023     * @param iterable The iterable
024     * @return The stream of for that iterable.
025     * @since 10718
026     */
027    public static <T> Stream<T> toStream(Iterable<T> iterable) {
028        return StreamSupport.stream(iterable.spliterator(), false);
029    }
030
031    /**
032     * Creates a new Collector that collects the items and returns them as HTML unordered list.
033     * @return The collector.
034     * @since 10638
035     */
036    public static Collector<String, ?, String> toHtmlList() {
037        return Collector.of(
038                () -> new StringJoiner("</li><li>", "<ul><li>", "</li></ul>").setEmptyValue("<ul></ul>"),
039                StringJoiner::add, StringJoiner::merge, StringJoiner::toString
040        );
041    }
042}