public final class ObjectStreams extends Object
Utility methods for streams.
Modifier and Type | Method and Description |
---|---|
static <T> ObjectStream<T> |
concat(Iterable<? extends ObjectStream<? extends T>> streams)
Concatenate object streams.
|
static <T> ObjectStream<T> |
concat(ObjectStream<? extends T>... objectStreams)
Concatenate object streams.
|
static <T> ObjectStream<T> |
consume(int n,
ObjectStream<T> stream)
Consume and discard the first
n elements from an object stream. |
static int |
count(ObjectStream<?> objectStream)
Count the items in a stream.
|
static <T> ObjectStream<T> |
empty()
Create an empty object stream.
|
static <T> ObjectStream<T> |
filter(ObjectStream<?> stream,
Class<T> type)
Filter an object stream to only contain elements of type type.
|
static <T> ObjectStream<T> |
filter(ObjectStream<T> stream,
Predicate<? super T> predicate)
Filter an object stream.
|
static <T> List<T> |
makeList(ObjectStream<? extends T> objectStream)
Read an object stream into a list, closing when it is finished.
|
static <T> ObjectStream<T> |
of(T... contents)
Create an object stream over a fixed set of elements.
|
static <T> ObjectStream<T> |
sort(ObjectStream<T> objectStream,
Comparator<? super T> comp)
Sort an object stream.
|
static <S,T> ObjectStream<T> |
transform(ObjectStream<S> objectStream,
Function<? super S,? extends T> function)
Transform an object stream’s values.
|
static <T> ObjectStream<T> |
wrap(Collection<? extends T> collection)
Wrap a collection in an object stream.
|
static <T> ObjectStream<T> |
wrap(Iterator<? extends T> iterator)
Wrap an iterator in an object stream.
|
public static <T> ObjectStream<T> wrap(Iterator<? extends T> iterator)
Wrap an iterator in an object stream.
The iterator may not contain null
. This property is checked lazily; the object stream will not fail until the null
would be returned.
T
- The type of data to return.iterator
- An iterator to wrappublic static <T> ObjectStream<T> wrap(Collection<? extends T> collection)
Wrap a collection in an object stream.
The iterator may not contain null
. This property is checked lazily; the object stream will not fail until the null
would be returned.
T
- The type of data to return.collection
- A collection to wrappublic static <T> ObjectStream<T> filter(@WillCloseWhenClosed ObjectStream<T> stream, Predicate<? super T> predicate)
Filter an object stream.
T
- The type of object stream rows.stream
- The source stream.predicate
- A predicate indicating which rows to return.true
.public static <T> ObjectStream<T> filter(@WillCloseWhenClosed ObjectStream<?> stream, Class<T> type)
Filter an object stream to only contain elements of type type. Unlike filter(ObjectStream, Predicate)
with a predicate from Predicates.instanceOf(Class)
, this method also transforms the stream to be of the target type.
T
- The type of value in the stream.stream
- The source stream.type
- The type to filter.public static <T> ObjectStream<T> consume(int n, ObjectStream<T> stream)
Consume and discard the first n
elements from an object stream.
n
- The number of elements to drop.stream
- The stream.T
- The stream’s element type.stream
is modified.public static <S,T> ObjectStream<T> transform(@WillCloseWhenClosed ObjectStream<S> objectStream, Function<? super S,? extends T> function)
Transform an object stream’s values.
S
- The type of source stream rowsT
- The type of output stream rowsobjectStream
- The source streamfunction
- A function to apply to each row in the stream. It will be applied to each value at most once.public static <T> ObjectStream<T> empty()
Create an empty object stream.
T
- The type of value in the object stream.public static <T> List<T> makeList(@WillClose ObjectStream<? extends T> objectStream)
Read an object stream into a list, closing when it is finished.
T
- The type of item in the object stream.objectStream
- The object stream.public static int count(@WillClose ObjectStream<?> objectStream)
Count the items in a stream.
objectStream
- The object stream.public static <T> ObjectStream<T> sort(@WillClose ObjectStream<T> objectStream, Comparator<? super T> comp)
Sort an object stream. This reads the original object stream into a list, sorts it, and returns a new object stream backed by the list (after closing the original object stream).
T
- The type of value in the object stream.objectStream
- The object stream to sort.comp
- The comparator to use to sort the object stream.@SafeVarargs public static <T> ObjectStream<T> of(T... contents)
Create an object stream over a fixed set of elements. This is mostly useful for testing.
contents
- The contents.T
- The data type.public static <T> ObjectStream<T> concat(Iterable<? extends ObjectStream<? extends T>> streams)
Concatenate object streams. Each object stream is closed as closed as it is consumed.
streams
- The object streams to concatenate.T
- The type of data.@SafeVarargs public static <T> ObjectStream<T> concat(ObjectStream<? extends T>... objectStreams)
Concatenate object streams.
concat(Iterable)