Class Maybe<T,E extends Throwable>
The idea is to wrap checked exceptions inside Stream<Maybe<T, E>>
, then map()
,
flatMap()
and filter()
away through normal stream operations.
Exception is only thrown during terminal operations.
For example, the following code fetches and runs pending jobs using a stream of Maybe
:
private Job fetchJob(long jobId) throws IOException;
void runPendingJobs() throws IOException {
Stream<Maybe<Job, IOException>> stream = activeJobIds.stream()
.map(maybe(this::fetchJob))
.filter(byValue(Job::isPending));
Iterate.through(stream, m -> m.orElseThrow(IOException::new).runJob());
}
When it comes to futures, the following asynchronous code example handles exceptions type safely
using Maybe.catchException()
:
CompletionStage<User> assumeAnonymousIfNotAuthenticated(CompletionStage<User> stage) {
CompletionStage<Maybe<User, AuthenticationException>> authenticated =
Maybe.catchException(AuthenticationException.class, stage);
return authenticated.thenApply(maybe -> maybe.orElse(e -> new AnonymousUser()));
}
-
Method Summary
Modifier and TypeMethodDescriptionDeprecated.Turnscondition
to aPredicate
overMaybe
.static <T,
E extends Throwable>
CompletionStage<Maybe<T, E>> catchException
(Class<E> exceptionType, CompletionStage<? extends T> stage) Deprecated.Returns a wrapper ofstage
that ifstage
failed with exception ofexceptionType
, that exception is caught and wrapped inside aMaybe
to complete the wrapper stage normally.catching
(CheckedConsumer<? super E, ? extends X> handler) Deprecated.Catches and handles exception withhandler
, and then skips it in the returnedStream
.except
(E exception) Deprecated.Creates an exceptionalMaybe
forexception
.Deprecated.Flat mapsthis
usingf
unless it wraps exception.Deprecated.Appliesconsumer
ifthis
is present.abstract boolean
Deprecated.Returns true unless this is exceptional.Deprecated.Mapsthis
usingfunction
unless it wraps exception.static <A,
B, T, E extends Throwable>
BiFunction<A, B, Maybe<T, E>> maybe
(CheckedBiFunction<? super A, ? super B, ? extends T, ? extends E> function) Deprecated.Wrapsfunction
to be used for a stream of Maybe.static <A,
B, T, E extends Throwable>
BiFunction<A, B, Maybe<T, E>> maybe
(CheckedBiFunction<? super A, ? super B, ? extends T, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
to be used for a stream of Maybe.maybe
(CheckedFunction<? super F, ? extends T, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
to be used for a stream of Maybe.maybe
(CheckedFunction<? super F, ? extends T, E> function) Deprecated.Wrapsfunction
to be used for a stream of Maybe.maybe
(CheckedSupplier<? extends T, ? extends E> supplier) Deprecated.Invokessupplier
and wraps the returned object or thrown exception in aMaybe<T, E>
.maybe
(CheckedSupplier<? extends T, ? extends E> supplier, Class<E> exceptionType) Deprecated.Wrapssupplier
to be used for a stream of Maybe.static <A,
B, T, E extends Throwable>
BiFunction<A, B, Stream<Maybe<T, E>>> maybeStream
(CheckedBiFunction<? super A, ? super B, ? extends Stream<? extends T>, ? extends E> function) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped.static <A,
B, T, E extends Throwable>
BiFunction<A, B, Stream<Maybe<T, E>>> maybeStream
(CheckedBiFunction<? super A, ? super B, ? extends Stream<? extends T>, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped.maybeStream
(CheckedFunction<? super F, ? extends Stream<? extends T>, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped.maybeStream
(CheckedFunction<? super F, ? extends Stream<? extends T>, E> function) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped.maybeStream
(CheckedSupplier<? extends Stream<? extends T>, ? extends E> supplier, Class<E> exceptionType) Deprecated.Invokessupplier
and wraps the returnedStream<T>
or thrown exception into a stream ofMaybe<T, E>
.maybeStream
(CheckedSupplier<? extends Stream<? extends T>, E> supplier) Deprecated.Invokessupplier
and wraps the returnedStream<T>
or thrown exception into a stream ofMaybe<T, E>
.Deprecated.Collects a stream ofMaybe
to an immutableMaybe<List<T>, E>
, which will wrap the exception if any inputMaybe
is exceptional.Deprecated.Collects a stream ofMaybe
to an immutableMaybe<Set<T>, E>
, which will wrap the exception if any inputMaybe
is exceptional.of
(T value) Deprecated.Creates aMaybe
forvalue
.orElse
(CheckedFunction<? super E, ? extends T, X> function) Deprecated.Either returns the encapsulated value, or translates exception usingfunction
.final T
Deprecated.Returns the encapsulated value or throws exception.orElseThrow
(Function<? super E, X> exceptionWrapper) Deprecated.Either returns success value, or throws exception created byexceptionWrapper
.
-
Method Details
-
of
Deprecated.Creates aMaybe
forvalue
.- Parameters:
value
- can be null
-
except
Deprecated.Creates an exceptionalMaybe
forexception
.If
exception
is anInterruptedException
, the current thread is re-interrupted as a standard practice to avoid swallowing the interruption signal. -
map
Deprecated.Mapsthis
usingfunction
unless it wraps exception. -
flatMap
Deprecated.Flat mapsthis
usingf
unless it wraps exception. -
isPresent
public abstract boolean isPresent()Deprecated.Returns true unless this is exceptional. -
ifPresent
Deprecated.Appliesconsumer
ifthis
is present. Returnsthis
. -
orElse
public abstract <X extends Throwable> T orElse(CheckedFunction<? super E, ? extends T, throws XX> function) Deprecated.Either returns the encapsulated value, or translates exception usingfunction
.- Throws:
X
-
orElseThrow
Deprecated.Returns the encapsulated value or throws exception.If
this
encapsulates an exception, a wrapper exception of typeE
is thrown to capture the caller's stack trace with the original exception as the cause.Consider to use
orElseThrow(Function)
to retain stack trace by wrapping exceptions, for example:orElseThrow(IOException::new)
.If
InterruptedException
is thrown, the current thread'sThread.interrupted()
bit is cleared because it's what most code expects when they catch anInterruptedException
.No exception wrapping is attempted for
InterruptedException
.- Throws:
E
-
orElseThrow
Deprecated.Either returns success value, or throws exception created byexceptionWrapper
.It's recommended for
exceptionWrapper
to wrap the original exception as the cause.- Throws:
X
-
catching
public final <X extends Throwable> Stream<T> catching(CheckedConsumer<? super E, ? extends X> handler) throws XDeprecated.Catches and handles exception withhandler
, and then skips it in the returnedStream
. This is specially useful in aStream
chain to handle and then ignore exceptional results.- Throws:
X
-
byValue
public static <T,E extends Throwable> Predicate<Maybe<T,E>> byValue(Predicate<? super T> condition) Deprecated.Turnscondition
to aPredicate
overMaybe
. The returned predicate matches anyMaybe
with a matching value, as well as any exceptionalMaybe
so as not to accidentally swallow exceptions. -
maybe
public static <T,E extends Throwable> Maybe<T,E> maybe(CheckedSupplier<? extends T, ? extends E> supplier) Deprecated.Invokessupplier
and wraps the returned object or thrown exception in aMaybe<T, E>
.Unchecked exceptions will be immediately propagated without being wrapped.
-
maybeStream
public static <T,E extends Throwable> Stream<Maybe<T,E>> maybeStream(CheckedSupplier<? extends Stream<? extends T>, E> supplier) Deprecated.Invokessupplier
and wraps the returnedStream<T>
or thrown exception into a stream ofMaybe<T, E>
.Useful to be passed to
Stream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>)
.Unchecked exceptions will be immediately propagated without being wrapped.
-
maybe
public static <F,T, Function<F,E extends Throwable> Maybe<T, maybeE>> (CheckedFunction<? super F, ? extends T, E> function) Deprecated.Wrapsfunction
to be used for a stream of Maybe.Unchecked exceptions will be immediately propagated without being wrapped.
-
maybeStream
public static <F,T, Function<F,E extends Throwable> Stream<Maybe<T, maybeStreamE>>> (CheckedFunction<? super F, ? extends Stream<? extends T>, E> function) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped.Useful to be passed to
Stream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>)
.Unchecked exceptions will be immediately propagated without being wrapped.
-
maybe
public static <A,B, BiFunction<A,T, E extends Throwable> B, maybeMaybe<T, E>> (CheckedBiFunction<? super A, ? super B, ? extends T, ? extends E> function) Deprecated.Wrapsfunction
to be used for a stream of Maybe.Unchecked exceptions will be immediately propagated without being wrapped.
-
maybeStream
public static <A,B, BiFunction<A,T, E extends Throwable> B, maybeStreamStream<Maybe<T, E>>> (CheckedBiFunction<? super A, ? super B, ? extends Stream<? extends T>, ? extends E> function) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped.Useful to be passed to
Stream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>)
.Unchecked exceptions will be immediately propagated without being wrapped.
-
maybe
public static <T,E extends Throwable> Maybe<T,E> maybe(CheckedSupplier<? extends T, ? extends E> supplier, Class<E> exceptionType) Deprecated.Wrapssupplier
to be used for a stream of Maybe.Normally one should use
maybe(CheckedSupplier)
unlessE
is an unchecked exception type.For GWT code, wrap the supplier manually, as in:
private static <T> Maybe<T, FooException> foo( CheckedSupplier<T, FooException> supplier) { try { return Maybe.of(supplier.get()); } catch (FooException e) { return Maybe.except(e); } }
-
maybeStream
public static <T,E extends Throwable> Stream<Maybe<T,E>> maybeStream(CheckedSupplier<? extends Stream<? extends T>, ? extends E> supplier, Class<E> exceptionType) Deprecated.Invokessupplier
and wraps the returnedStream<T>
or thrown exception into a stream ofMaybe<T, E>
. -
maybe
public static <F,T, Function<F,E extends Throwable> Maybe<T, maybeE>> (CheckedFunction<? super F, ? extends T, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
to be used for a stream of Maybe.Normally one should use
maybe(CheckedFunction)
unlessE
is an unchecked exception type.For GWT code, wrap the function manually, as in:
private static <F, T> Function<F, Maybe<T, FooException>> foo( CheckedFunction<F, T, FooException> function) { return from -> { try { return Maybe.of(function.apply(from)); } catch (FooException e) { return Maybe.except(e); } }; }
-
maybeStream
public static <F,T, Function<F,E extends Throwable> Stream<Maybe<T, maybeStreamE>>> (CheckedFunction<? super F, ? extends Stream<? extends T>, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped. -
maybe
public static <A,B, BiFunction<A,T, E extends Throwable> B, maybeMaybe<T, E>> (CheckedBiFunction<? super A, ? super B, ? extends T, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
to be used for a stream of Maybe.Normally one should use
maybe(CheckedBiFunction)
unlessE
is an unchecked exception type.For GWT code, wrap the function manually, as in:
private static <A, B, T> BiFunction<A, B, Maybe<T, FooException>> foo( CheckedBiFunction<A, B, T, FooException> function) { return (a, b) -> { try { return Maybe.of(function.apply(a, b)); } catch (FooException e) { return Maybe.except(e); } }; }
-
maybeStream
public static <A,B, BiFunction<A,T, E extends Throwable> B, maybeStreamStream<Maybe<T, E>>> (CheckedBiFunction<? super A, ? super B, ? extends Stream<? extends T>, ? extends E> function, Class<E> exceptionType) Deprecated.Wrapsfunction
that returnsStream<T>
to one that returnsStream<Maybe<T, E>>
with exceptions of typeE
wrapped. -
maybeToList
Deprecated.Collects a stream ofMaybe
to an immutableMaybe<List<T>, E>
, which will wrap the exception if any inputMaybe
is exceptional.- Since:
- 7.0
-
maybeToSet
Deprecated.Collects a stream ofMaybe
to an immutableMaybe<Set<T>, E>
, which will wrap the exception if any inputMaybe
is exceptional.- Since:
- 7.0
-
catchException
public static <T,E extends Throwable> CompletionStage<Maybe<T,E>> catchException(Class<E> exceptionType, CompletionStage<? extends T> stage) Deprecated.Returns a wrapper ofstage
that ifstage
failed with exception ofexceptionType
, that exception is caught and wrapped inside aMaybe
to complete the wrapper stage normally.This is useful if the asynchronous code is interested in recovering from its own exception without having to deal with other exception types.
-