T
- the type to carry.public class Maybe<T>
extends java.lang.Object
Optional
. A Maybe<T> either carries a T or an
exception that occurred when producing it.Modifier and Type | Class and Description |
---|---|
static interface |
Maybe.ThrowingConsumer<T> |
static interface |
Maybe.ThrowingFunction<I,R> |
static interface |
Maybe.ThrowingRunnable |
static interface |
Maybe.ThrowingSupplier<T> |
Modifier and Type | Method and Description |
---|---|
static Maybe<java.lang.Void> |
attempt(Maybe.ThrowingRunnable runnable)
Construct a
Maybe <Void> from the execution of a void function. |
static <T> Maybe<T> |
attempt(Maybe.ThrowingSupplier<T> runnable)
Construct a
Maybe from the execution of a function. |
boolean |
equals(java.lang.Object obj) |
java.util.Optional<java.lang.Throwable> |
exception()
Returns the exception, if any.
|
boolean |
hasException()
Returns true if this Maybe is in an "unsuccessful" state, i.e.
|
int |
hashCode() |
Maybe<java.lang.Void> |
map(Maybe.ThrowingConsumer<T> function)
Apply a void function if this
Maybe is in a "successful" state. |
<R> Maybe<R> |
map(Maybe.ThrowingFunction<T,R> function)
Apply a transformation function if this
Maybe is in a "successful" state. |
static <T> Maybe<T> |
ofException(java.lang.Throwable exception)
Construct an "unsuccessful"
Maybe containing an exception. |
static <T> Maybe<T> |
ofValue(T value)
Construct a "successful"
Maybe containing a value. |
static Maybe<java.lang.Void> |
ofVoid()
Construct a "successful"
Maybe <Void> containing a null value. |
java.util.Optional<T> |
optionalValue()
Retrieve the value stored in this
Maybe as an Optional . |
Maybe<java.lang.Void> |
then(Maybe.ThrowingRunnable runnable)
Apply a void function if this
Maybe is in a "successful" state. |
<R> Maybe<R> |
then(Maybe.ThrowingSupplier<R> runnable)
Apply a function if this
Maybe is in a "successful" state. |
void |
throwOnException()
Throw the contained exception, wrapped in a
RuntimeException , if any. |
java.lang.String |
toString() |
T |
value()
Retrieve the value stored in this
Maybe . |
public static <T> Maybe<T> ofException(java.lang.Throwable exception)
Maybe
containing an exception.exception
- the exception to wrapjava.lang.NullPointerException
- if the exception is nullpublic static <T> Maybe<T> ofValue(T value)
Maybe
containing a value.value
- the value to wrapjava.lang.NullPointerException
- if the value is nullpublic static Maybe<java.lang.Void> ofVoid()
Maybe
<Void> containing a null value. This special Maybe objects represents a
successful execution with no result (e.g. a void function). Note that ONLY Maybe<Void> is allowed to carry a null
value.public java.util.Optional<java.lang.Throwable> exception()
public boolean hasException()
public T value()
Maybe
. If an exception is stored, this method will re-throw it wrapped
in a RuntimeException
.java.lang.RuntimeException
- if this Maybe objects contains an exceptionpublic java.util.Optional<T> optionalValue()
Maybe
as an Optional
. If an exception is stored in this Maybe,
the returned optional will be empty. For a "successful" Maybe
<Void>, it will contain null.java.lang.RuntimeException
- if this Maybe objects contains an exceptionpublic void throwOnException()
RuntimeException
, if any. If called on a "successful" Maybe,
this method does nothing.public static <T> Maybe<T> attempt(Maybe.ThrowingSupplier<T> runnable)
Maybe
from the execution of a function.runnable
- the function to run.Maybe
object containing either the return value of the function, or an exception of one
occurred.public static Maybe<java.lang.Void> attempt(Maybe.ThrowingRunnable runnable)
Maybe
<Void> from the execution of a void function.runnable
- the function to run.Maybe
<Void> if the function run successfully, or an exception of one occurred.public <R> Maybe<R> map(Maybe.ThrowingFunction<T,R> function)
Maybe
is in a "successful" state. Pass through the exception in
case it is in an "unsuccessful" state. If the transformation function throws, the exception is returned wrapped
in an "unsuccessful" Maybe.function
- the function to applypublic Maybe<java.lang.Void> map(Maybe.ThrowingConsumer<T> function)
Maybe
is in a "successful" state. Pass through the exception in case it is
in an "unsuccessful" state. If the transformation function throws, the exception is returned wrapped in an
"unsuccessful" Maybe. If it succeeds, return an empty Maybe<Void>.function
- the function to applypublic <R> Maybe<R> then(Maybe.ThrowingSupplier<R> runnable)
Maybe
is in a "successful" state. Pass through the exception in case it is in an
"unsuccessful" state. The function does not get the value of the Maybe passed as parameter, which is useful e.g.
for chaining Maybe<Void>. If the function succeeds, its result is returned as a successful Maybe.runnable
- the function to applypublic Maybe<java.lang.Void> then(Maybe.ThrowingRunnable runnable)
Maybe
is in a "successful" state. Pass through the exception in case it is
in an "unsuccessful" state. The function does not get the value of the Maybe passed as parameter, which is useful
e.g. for chaining Maybe<Void>. If the function succeeds, a successful empty Maybe<Void> is returned.runnable
- the function to applypublic int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object