T
- the type of the inner valuepublic class Exceptional<T>
extends java.lang.Object
ThrowableSupplier
.
Stores value which provided by ThrowableSupplier
or an exception which were thrown.
Exceptional.of(new ThrowableSupplier<String, Throwable>() {
@Override
public String get() throws Throwable {
return IOUtils.read(inputStream);
}
}).ifExceptionIs(IOException.class, new Consumer<IOException>() {
@Override
public void accept(IOException exception) {
logger.log(Level.WARNING, "read file", exception);
}
}).getOrElse("default string");
Exceptional.of(() -> IOUtils.readBytes(inputStream)).getOrElse(new byte[0]);
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object obj) |
T |
get()
Returns inner value.
|
java.lang.Throwable |
getException()
Returns exception.
|
Optional<T> |
getOptional()
Wraps inner value with
Optional container |
T |
getOrElse(T other)
Returns inner value if there were no exceptions, otherwise returns
other . |
T |
getOrThrow()
Returns inner value if there were no exceptions, otherwise throws an exception.
|
<E extends java.lang.Throwable> |
getOrThrow(E exception)
Returns inner value if there were no exceptions, otherwise throws the given
exception . |
T |
getOrThrowRuntimeException()
Returns inner value if there were no exceptions, otherwise throws
RuntimeException . |
int |
hashCode() |
Exceptional<T> |
ifException(Consumer<java.lang.Throwable> consumer)
Invokes consumer function if there were any exception.
|
<E extends java.lang.Throwable> |
ifExceptionIs(java.lang.Class<E> throwableClass,
Consumer<? super E> consumer)
Invokes consumer function if exception class matches
throwableClass . |
Exceptional<T> |
ifPresent(Consumer<? super T> consumer)
Invokes consumer function with value if present.
|
<U> Exceptional<U> |
map(ThrowableFunction<? super T,? extends U,java.lang.Throwable> mapper)
Invokes mapping function on inner value if there were no exceptions.
|
static <T> Exceptional<T> |
of(ThrowableSupplier<T,java.lang.Throwable> supplier)
Returns an
Exceptional with value provided by given ThrowableSupplier function. |
Exceptional<T> |
or(Supplier<Exceptional<T>> supplier)
Returns current
Exceptional if there were no exceptions, otherwise
returns an Exceptional produced by supplier function. |
Exceptional<T> |
recover(ThrowableFunction<java.lang.Throwable,? extends T,java.lang.Throwable> function)
Returns current
Exceptional if there were no exceptions, otherwise
calls function and wraps produced result with an Exceptional . |
Exceptional<T> |
recoverWith(Function<java.lang.Throwable,? extends Exceptional<T>> function)
Returns current
Exceptional if there were no exceptions, otherwise
returns an Exceptional produced by function . |
java.lang.String |
toString() |
public static <T> Exceptional<T> of(ThrowableSupplier<T,java.lang.Throwable> supplier)
Exceptional
with value provided by given ThrowableSupplier
function.T
- the type of valuesupplier
- a supplier functionExceptional
public T get()
public T getOrElse(T other)
other
.other
- the value to be returned if there were any exceptionother
public Optional<T> getOptional()
Optional
containerOptional
public java.lang.Throwable getException()
public T getOrThrow() throws java.lang.Throwable
java.lang.Throwable
- that was thrown in supplier functionpublic T getOrThrowRuntimeException() throws java.lang.RuntimeException
RuntimeException
.java.lang.RuntimeException
- with wrapped exception which was thrown in supplier functionpublic <E extends java.lang.Throwable> T getOrThrow(E exception) throws E extends java.lang.Throwable
exception
.E
- the type of exceptionexception
- an exception to be thrownE
- if there were exceptions in supplier functionE extends java.lang.Throwable
public Exceptional<T> or(Supplier<Exceptional<T>> supplier)
Exceptional
if there were no exceptions, otherwise
returns an Exceptional
produced by supplier function.supplier
- supplier function that produced an Exceptional
to be returnedExceptional
if there were no exceptions, otherwise
an Exceptional
produced by supplier functionjava.lang.NullPointerException
- if supplier
or its result is nullpublic <U> Exceptional<U> map(ThrowableFunction<? super T,? extends U,java.lang.Throwable> mapper)
U
- the type of result valuemapper
- mapping functionExceptional
with transformed value if there were no exceptionsjava.lang.NullPointerException
- if mapper
is nullpublic Exceptional<T> ifPresent(Consumer<? super T> consumer)
consumer
- a consumer functionExceptional
public Exceptional<T> ifException(Consumer<java.lang.Throwable> consumer)
consumer
- a consumer functionExceptional
public <E extends java.lang.Throwable> Exceptional<T> ifExceptionIs(java.lang.Class<E> throwableClass, Consumer<? super E> consumer)
throwableClass
.E
- the type of exceptionthrowableClass
- the class of an exception to be comparedconsumer
- a consumer functionExceptional
public Exceptional<T> recover(ThrowableFunction<java.lang.Throwable,? extends T,java.lang.Throwable> function)
Exceptional
if there were no exceptions, otherwise
calls function
and wraps produced result with an Exceptional
.function
- recovering functionExceptional
if there were no exceptions, otherwise
an Exceptional
with wrapped recovering function resultjava.lang.NullPointerException
- if function
is nullpublic Exceptional<T> recoverWith(Function<java.lang.Throwable,? extends Exceptional<T>> function)
Exceptional
if there were no exceptions, otherwise
returns an Exceptional
produced by function
.function
- recovering functionExceptional
if there were no exceptions, otherwise
an Exceptional
produced by recovering functionjava.lang.NullPointerException
- if function
or produced result is nullpublic boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object