Class Maybe<T>
- java.lang.Object
-
- com.identityworksllc.iiq.common.Maybe<T>
-
- Type Parameters:
T- The type that this object might contain
public final class Maybe<T> extends Object
A functional “monad” that contains either a non-null value of the given type or an exception, but never both.The idea here is:
objectList .stream() .map(obj -> Maybe.wrap(String.class, some::functionThatCanFail)) .filter(Maybe.fnHasValue()) .forEach(items);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMaybe.MaybeConsumer<T>A consumer extension that handles the Maybe concept.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <F> Predicate<Maybe<F>>fnHasError()Creates a Predicate that returns true if the Maybe object has an errorstatic <F> Predicate<Maybe<F>>fnHasValue()Creates a Predicate that returns true if the Maybe object has a valueTget()Gets the value or throws the exception wrapped in an ExecutionExceptionThrowablegetError()Returns the error if there is one, or else throws aNoSuchElementException.booleanhasError()If this Maybe has an error and not a valuebooleanhasValue()If this Maybe has a value and not an error<B> Maybe<B>map(Functions.FunctionWithError<T,B> downstream)Chains a Maybe object by invoking the given function on it.static <A> Maybe<A>of(A value)Returns a Maybe object containing the given valuestatic <A> Maybe<A>of(Maybe<?> value)Chains a Maybe object by passing along the Throwable from an existing Maybe into the next.static <A> Maybe<A>of(Maybe<?> value, Class<A> otherwiseExpectedType)Chains a Maybe object by passing along the Throwable from an existing Maybe into the next.static <A> Maybe<A>of(Class<A> aClass, Functions.SupplierWithError<A> supplier)Returns a Maybe that is either a value or an error, depending on the outcome of the supplier in question.static <R> Maybe<R>of(Throwable e)Creates a new maybe from the given throwable with an inferred type by contextstatic <A> Maybe<A>of(Throwable value, Class<A> otherwiseExpectedType)Returns a Maybe object containing an exception.static <I,O>
Function<I,Maybe<O>>wrap(Class<O> aClass, Functions.FunctionWithError<I,O> func)Returns a function wrapping the input function.
-
-
-
Method Detail
-
fnHasError
public static <F> Predicate<Maybe<F>> fnHasError()
Creates a Predicate that returns true if the Maybe object has an error- Type Parameters:
F- The arbitrary input type- Returns:
- The predicate
-
fnHasValue
public static <F> Predicate<Maybe<F>> fnHasValue()
Creates a Predicate that returns true if the Maybe object has a value- Type Parameters:
F- The arbitrary input type- Returns:
- The predicate
-
of
public static <A> Maybe<A> of(A value)
Returns a Maybe object containing the given value- Type Parameters:
A- The type of the value- Parameters:
value- The value- Returns:
- A Maybe object containing the given value
-
of
public static <A> Maybe<A> of(Class<A> aClass, Functions.SupplierWithError<A> supplier)
Returns a Maybe that is either a value or an error, depending on the outcome of the supplier in question.- Type Parameters:
A- The content type of the Maybe, if it has a value- Parameters:
aClass- The class expected, used solely to distinguish this method from the other of() implementationssupplier- The supplier of a value to wrap in a Maybe- Returns:
- A Maybe repesenting the outcome of the Supplier’s action
-
of
public static <A> Maybe<A> of(Throwable value, Class<A> otherwiseExpectedType)
Returns a Maybe object containing an exception.The class parameter is only used for casting the output type.
- Type Parameters:
A- The parameterized type that we would be expecting if this was not an exception- Parameters:
value- The exceptionotherwiseExpectedType- The parameterized type that we would be expecting if this was not an exception- Returns:
- a Maybe object containing the exception
-
of
public static <A> Maybe<A> of(Maybe<?> value, Class<A> otherwiseExpectedType)
Chains a Maybe object by passing along the Throwable from an existing Maybe into the next.This is for use with streams.
- Type Parameters:
A- The parameterized type that we would be expecting if this was not an exception- Parameters:
value- An existing Maybe in error stateotherwiseExpectedType- The parameterized type that we would be expecting if this was not an exception- Returns:
- a Maybe object containing the exception
-
of
public static <A> Maybe<A> of(Maybe<?> value)
Chains a Maybe object by passing along the Throwable from an existing Maybe into the next.This is for use with streams.
- Type Parameters:
A- The parameterized type that we would be expecting if this was not an exception- Parameters:
value- An existing Maybe in error state- Returns:
- a Maybe object containing the exception
-
of
public static <R> Maybe<R> of(Throwable e)
Creates a new maybe from the given throwable with an inferred type by context- Type Parameters:
R- The inferred type- Parameters:
e- The throwable- Returns:
- A chained Maybe
-
wrap
public static <I,O> Function<I,Maybe<O>> wrap(Class<O> aClass, Functions.FunctionWithError<I,O> func)
Returns a function wrapping the input function.The wrapper will resolve the output of the input function by invoking
com.identityworksllc.iiq.common.Functions.FunctionWithError#applyWithError(I)and wrapping the output (whether a value or an exception) in a Maybe.- Type Parameters:
I- The input type to the functionO- The content type of the Maybe, assuming it was to have a value- Parameters:
aClass- The output class expectedfunc- The function that will be wrapped in a Maybe producer- Returns:
- A Maybe repesenting the outcome of the Supplier’s action
-
get
public T get() throws IllegalStateException
Gets the value or throws the exception wrapped in an ExecutionException- Returns:
- The value if it exists
- Throws:
IllegalStateException- If this Maybe was an exception instead
-
getError
public Throwable getError() throws NoSuchElementException
Returns the error if there is one, or else throws aNoSuchElementException.- Returns:
- The error if one exists
- Throws:
NoSuchElementException- if the error doesn’t exist
-
hasError
public boolean hasError()
If this Maybe has an error and not a value- Returns:
- True if this Maybe has an error
-
hasValue
public boolean hasValue()
If this Maybe has a value and not an error- Returns:
- True if this Maybe does not have an error
-
map
public <B> Maybe<B> map(Functions.FunctionWithError<T,B> downstream)
Chains a Maybe object by invoking the given function on it.There are three possible outcomes:
-
This object already has an error (
hasError()returns true), in which case this method will return a new Maybe with that error. -
Applying the function to this Maybe’s value results in an exception, in which case this method will return a new Maybe with that exception.
-
Applying the function is successful and produces an object of type ‘B’, in which case this method returns a new Maybe containing that object.
- Type Parameters:
B- The output type- Parameters:
downstream- The mapping function to apply to this Maybe- Returns:
- a Maybe object containing the exception
-
-
-