public class Streams
extends java.lang.Object
java.util.stream
package, or more generally,
with Java 8 lambdas. More specifically, it attempts to address the fact that lambdas are supposed not to throw
Exceptions, at least not checked Exceptions, AKA instances of Exception
. This enforces the use of constructs
like:
Consumer<java.lang.reflect.Method> consumer = m -> {
try {
m.invoke(o, args);
} catch (Throwable t) {
throw Failable.rethrow(t);
}
};
stream.forEach(consumer);
Using a Streams.FailableStream
, this can be rewritten as follows:
Streams.failable(stream).forEach((m) -> m.invoke(o, args));
Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than in the
first version.Stream
,
Failable
Modifier and Type | Class and Description |
---|---|
static class |
Streams.ArrayCollector<E>
A Collector type for arrays.
|
static class |
Streams.FailableStream<T>
A reduced, and simplified version of a
Stream with failable method signatures. |
Constructor and Description |
---|
Streams() |
Modifier and Type | Method and Description |
---|---|
static <T> Streams.FailableStream<T> |
failableStream(java.util.Collection<T> stream)
Converts the given
Collection into a Streams.FailableStream . |
static <T> Streams.FailableStream<T> |
failableStream(java.util.stream.Stream<T> stream)
Converts the given
stream into a Streams.FailableStream . |
static <E> java.util.stream.Stream<E> |
instancesOf(java.lang.Class<? super E> clazz,
java.util.Collection<? super E> collection)
Streams only instances of the give Class in a collection.
|
static <E> java.util.stream.Stream<E> |
nonNull(java.util.Collection<E> collection)
Streams the non-null elements of a collection.
|
static <E> java.util.stream.Stream<E> |
nonNull(E... array)
Streams the non-null elements of an array.
|
static <E> java.util.stream.Stream<E> |
nonNull(java.util.stream.Stream<E> stream)
Streams the non-null elements of a stream.
|
static <E> java.util.stream.Stream<E> |
of(java.util.Collection<E> collection)
Delegates to
Collection.stream() or returns Stream.empty() if the collection is null. |
static <E> java.util.stream.Stream<E> |
of(java.util.Enumeration<E> enumeration)
Streams the elements of the given enumeration in order.
|
static <E> java.util.stream.Stream<E> |
of(java.lang.Iterable<E> iterable)
Creates a stream on the given Iterable.
|
static <E> java.util.stream.Stream<E> |
of(java.util.Iterator<E> iterator)
Creates a stream on the given Iterator.
|
static <T> java.util.stream.Stream<T> |
of(T... values)
Null-safe version of
Stream.of(Object[]) . |
static <E> Streams.FailableStream<E> |
stream(java.util.Collection<E> collection)
Deprecated.
|
static <T> Streams.FailableStream<T> |
stream(java.util.stream.Stream<T> stream)
Deprecated.
|
static <T> java.util.stream.Collector<T,?,T[]> |
toArray(java.lang.Class<T> pElementType)
Returns a
Collector that accumulates the input elements into a new array. |
public static <T> Streams.FailableStream<T> failableStream(java.util.Collection<T> stream)
Collection
into a Streams.FailableStream
. This is basically a simplified, reduced version
of the Stream
class, with the same underlying element stream, except that failable objects, like
FailablePredicate
, FailableFunction
, or FailableConsumer
may be applied, instead of
Predicate
, Function
, or Consumer
. The idea is to rewrite a code snippet like this:
final List<O> list;
final Method m;
final Function<O, String> mapper = (o) -> {
try {
return (String) m.invoke(o);
} catch (Throwable t) {
throw Failable.rethrow(t);
}
};
final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list;
final Method m;
final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional,
intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
better than the first version.T
- The streams element type.stream
- The stream, which is being converted.Streams.FailableStream
, which has been created by converting the stream.public static <T> Streams.FailableStream<T> failableStream(java.util.stream.Stream<T> stream)
stream
into a Streams.FailableStream
. This is basically a simplified, reduced
version of the Stream
class, with the same underlying element stream, except that failable objects, like
FailablePredicate
, FailableFunction
, or FailableConsumer
may be applied, instead of
Predicate
, Function
, or Consumer
. The idea is to rewrite a code snippet like this:
final List<O> list;
final Method m;
final Function<O, String> mapper = (o) -> {
try {
return (String) m.invoke(o);
} catch (Throwable t) {
throw Failable.rethrow(t);
}
};
final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list;
final Method m;
final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional,
intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
better than the first version.T
- The streams element type.stream
- The stream, which is being converted.Streams.FailableStream
, which has been created by converting the stream.public static <E> java.util.stream.Stream<E> instancesOf(java.lang.Class<? super E> clazz, java.util.Collection<? super E> collection)
This method shorthand for:
(Stream<E>) Streams.toStream(collection).filter(collection, SomeClass.class::isInstance);
E
- the type of elements in the collection we want to stream.clazz
- the type of elements in the collection we want to stream.collection
- the collection to stream or null.public static <E> java.util.stream.Stream<E> nonNull(java.util.Collection<E> collection)
E
- the type of elements in the collection.collection
- the collection to stream or null.@SafeVarargs public static <E> java.util.stream.Stream<E> nonNull(E... array)
E
- the type of elements in the collection.array
- the array to stream or null.public static <E> java.util.stream.Stream<E> nonNull(java.util.stream.Stream<E> stream)
E
- the type of elements in the collection.stream
- the stream to stream or null.public static <E> java.util.stream.Stream<E> of(java.util.Collection<E> collection)
Collection.stream()
or returns Stream.empty()
if the collection is null.E
- the type of elements in the collection.collection
- the collection to stream or null.Collection.stream()
or Stream.empty()
if the collection is null.public static <E> java.util.stream.Stream<E> of(java.util.Enumeration<E> enumeration)
E
- The enumeration element type.enumeration
- The enumeration to stream.public static <E> java.util.stream.Stream<E> of(java.lang.Iterable<E> iterable)
E
- the type of elements in the Iterable.iterable
- the Iterable to stream or null.Stream.empty()
if the Iterable is null.public static <E> java.util.stream.Stream<E> of(java.util.Iterator<E> iterator)
E
- the type of elements in the Iterator.iterator
- the Iterator to stream or null.Stream.empty()
if the Iterator is null.@SafeVarargs public static <T> java.util.stream.Stream<T> of(T... values)
Stream.of(Object[])
.T
- the type of stream elements.values
- the elements of the new stream, may be null
.values
or Stream.empty()
.@Deprecated public static <E> Streams.FailableStream<E> stream(java.util.Collection<E> collection)
failableStream(Collection)
.Collection
into a Streams.FailableStream
. This is basically a simplified, reduced version
of the Stream
class, with the same underlying element stream, except that failable objects, like
FailablePredicate
, FailableFunction
, or FailableConsumer
may be applied, instead of
Predicate
, Function
, or Consumer
. The idea is to rewrite a code snippet like this:
final List<O> list;
final Method m;
final Function<O, String> mapper = (o) -> {
try {
return (String) m.invoke(o);
} catch (Throwable t) {
throw Failable.rethrow(t);
}
};
final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list;
final Method m;
final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional,
intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
better than the first version.E
- The streams element type.collection
- The stream, which is being converted.Streams.FailableStream
, which has been created by converting the stream.@Deprecated public static <T> Streams.FailableStream<T> stream(java.util.stream.Stream<T> stream)
failableStream(Stream)
.stream
into a Streams.FailableStream
. This is basically a simplified, reduced
version of the Stream
class, with the same underlying element stream, except that failable objects, like
FailablePredicate
, FailableFunction
, or FailableConsumer
may be applied, instead of
Predicate
, Function
, or Consumer
. The idea is to rewrite a code snippet like this:
final List<O> list;
final Method m;
final Function<O, String> mapper = (o) -> {
try {
return (String) m.invoke(o);
} catch (Throwable t) {
throw Failable.rethrow(t);
}
};
final List<String> strList = list.stream().map(mapper).collect(Collectors.toList());
as follows:
final List<O> list;
final Method m;
final List<String> strList = Failable.stream(list.stream()).map((o) -> (String) m.invoke(o)).collect(Collectors.toList());
While the second version may not be quite as efficient (because it depends on the creation of additional,
intermediate objects, of type FailableStream), it is much more concise, and readable, and meets the spirit of Lambdas
better than the first version.T
- The streams element type.stream
- The stream, which is being converted.Streams.FailableStream
, which has been created by converting the stream.public static <T> java.util.stream.Collector<T,?,T[]> toArray(java.lang.Class<T> pElementType)
Collector
that accumulates the input elements into a new array.T
- the type of the input elementspElementType
- Type of an element in the array.Collector
which collects all the input elements into an array, in encounter orderCopyright © 2010 - 2023 Adobe. All Rights Reserved