All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bbn.parliament.util.StreamUtil Maven / Gradle / Ivy

// Copyright (c) 2019, 2020 RTX BBN Technologies Corp.

package com.bbn.parliament.util;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * A utility class to convert iterators and iterables to streams.
 *
 * @author iemmons
 */
public class StreamUtil {
	private static final int DEFAULT_CHARACTERISTICS = Spliterator.ORDERED | Spliterator.IMMUTABLE;

	private StreamUtil() {}	// prevents instantiation

	/**
	 * Converts an iterator to a sequential stream. Assumes an ordered and immutable
	 * iterator.
	 *
	 * @param   The element type of the iterator
	 * @param iter The iterator to convert
	 * @return A sequential stream containing the elements enumerated by the iterator
	 */
	public static  Stream asStream(Iterator iter) {
		return asStream(iter, DEFAULT_CHARACTERISTICS);
	}

	/**
	 * Converts an iterator to a parallel stream. Assumes an ordered and immutable
	 * iterator.
	 *
	 * @param   The element type of the iterator
	 * @param iter The iterator to convert
	 * @return A parallel stream containing the elements enumerated by the iterator
	 */
	public static  Stream asParallelStream(Iterator iter) {
		return asParallelStream(iter, DEFAULT_CHARACTERISTICS);
	}

	/**
	 * Converts an iterator to a sequential stream.
	 *
	 * @param                  The element type of the iterator
	 * @param iter                The iterator to convert
	 * @param iterCharacteristics The iterator's characteristics, as defined by
	 *                            constants in the Spliterator class
	 * @return A sequential stream containing the elements enumerated by the iterator
	 */
	public static  Stream asStream(Iterator iter, int iterCharacteristics) {
		return StreamSupport.stream(
			Spliterators.spliteratorUnknownSize(iter, iterCharacteristics),
			false);
	}

	/**
	 * Converts an iterator to a parallel stream.
	 *
	 * @param                  The element type of the iterator
	 * @param iter                The iterator to convert
	 * @param iterCharacteristics The iterator's characteristics, as defined by
	 *                            constants in the Spliterator class
	 * @return A parallel stream containing the elements enumerated by the iterator
	 */
	public static  Stream asParallelStream(Iterator iter, int iterCharacteristics) {
		return StreamSupport.stream(
			Spliterators.spliteratorUnknownSize(iter, iterCharacteristics),
			true);
	}

	/**
	 * Converts an iterable to a sequential stream.
	 *
	 * @param       The element type of the iterable
	 * @param iterable The iterable to convert
	 * @return A sequential stream containing the elements of the iterable
	 */
	public static  Stream asStream(Iterable iterable) {
		return StreamSupport.stream(iterable.spliterator(), false);
	}

	/**
	 * Converts an iterable to a parallel stream.
	 *
	 * @param       The element type of the iterable
	 * @param iterable The iterable to convert
	 * @return A parallel stream containing the elements of the iterable
	 */
	public static  Stream asParallelStream(Iterable iterable) {
		return StreamSupport.stream(iterable.spliterator(), true);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy