public class Streams extends 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.
Modifier and Type | Class and Description |
---|---|
static class |
Streams.ArrayCollector<O>
A Collector type for arrays.
|
static class |
Streams.FailableStream<O>
A reduced, and simplified version of a
Stream with failable method signatures. |
Constructor and Description |
---|
Streams() |
Modifier and Type | Method and Description |
---|---|
static <O> Streams.FailableStream<O> |
stream(Collection<O> stream)
Converts the given
Collection into a Streams.FailableStream . |
static <O> Streams.FailableStream<O> |
stream(Stream<O> stream)
Converts the given
stream into a Streams.FailableStream . |
static <O> Collector<O,?,O[]> |
toArray(Class<O> pElementType)
Returns a
Collector that accumulates the input elements into a new array. |
public static <O> Streams.FailableStream<O> stream(Collection<O> 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.
O
- The streams element type.stream
- The stream, which is being converted.Streams.FailableStream
, which has been created by converting the stream.public static <O> Streams.FailableStream<O> stream(Stream<O> 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.
O
- The streams element type.stream
- The stream, which is being converted.Streams.FailableStream
, which has been created by converting the stream.public static <O> Collector<O,?,O[]> toArray(Class<O> pElementType)
Collector
that accumulates the input elements into a new array.O
- 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 © 2001–2021 The Apache Software Foundation. All rights reserved.