T - the type of the inner valuepublic class Exceptional<T> extends 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 |
|---|---|
<R> R |
custom(Function<Exceptional<T>,R> function)
Applies custom operator on
Exceptional. |
boolean |
equals(Object obj) |
T |
get()
Returns inner value.
|
Throwable |
getException()
Returns exception.
|
Optional<T> |
getOptional()
Wraps inner value with
Optional container |
T |
getOrElse(Supplier<? extends T> other)
Returns inner value if there were no exceptions, otherwise returns value produced by supplier function.
|
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 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<Throwable> consumer)
Invokes consumer function if there were any exception.
|
<E extends Throwable> |
ifExceptionIs(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.
|
boolean |
isPresent()
Checks value present (i.e. there were no exceptions).
|
<U> Exceptional<U> |
map(ThrowableFunction<? super T,? extends U,Throwable> mapper)
Invokes mapping function on inner value if there were no exceptions.
|
static <T> Exceptional<T> |
of(Throwable throwable)
Returns an
Exceptional with throwable already set. |
static <T> Exceptional<T> |
of(ThrowableSupplier<T,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<Throwable,? extends T,Throwable> function)
Returns current
Exceptional if there were no exceptions, otherwise
calls function and wraps produced result with an Exceptional. |
Exceptional<T> |
recoverWith(Function<Throwable,? extends Exceptional<T>> function)
Returns current
Exceptional if there were no exceptions, otherwise
returns an Exceptional produced by function. |
String |
toString() |
public static <T> Exceptional<T> of(ThrowableSupplier<T,Throwable> supplier)
Exceptional with value provided by given ThrowableSupplier function.T - the type of valuesupplier - a supplier functionExceptionalpublic static <T> Exceptional<T> of(Throwable throwable)
Exceptional with throwable already set.T - the type of valuethrowable - throwable instanceExceptionalpublic T get()
public boolean isPresent()
true if a value present, false otherwisepublic T getOrElse(T other)
other.other - the value to be returned if there were any exceptionotherpublic T getOrElse(Supplier<? extends T> other)
other - the supplier function that produces value if there were any exceptionpublic Optional<T> getOptional()
Optional containerOptionalpublic Throwable getException()
public T getOrThrow() throws Throwable
Throwable - that was thrown in supplier functionpublic T getOrThrowRuntimeException() throws RuntimeException
RuntimeException.RuntimeException - with wrapped exception which was thrown in supplier functionpublic <E extends Throwable> T getOrThrow(E exception) throws E extends Throwable
exception.E - the type of exceptionexception - an exception to be thrownE - if there were exceptions in supplier functionE extends Throwablepublic 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 functionNullPointerException - if supplier or its result is nullpublic <R> R custom(Function<Exceptional<T>,R> function)
Exceptional.R - the type of the resultfunction - a transforming functionNullPointerException - if function is nullpublic <U> Exceptional<U> map(ThrowableFunction<? super T,? extends U,Throwable> mapper)
U - the type of result valuemapper - mapping functionExceptional with transformed value if there were no exceptionsNullPointerException - if mapper is nullpublic Exceptional<T> ifPresent(Consumer<? super T> consumer)
consumer - a consumer functionExceptionalpublic Exceptional<T> ifException(Consumer<Throwable> consumer)
consumer - a consumer functionExceptionalpublic <E extends Throwable> Exceptional<T> ifExceptionIs(Class<E> throwableClass, Consumer<? super E> consumer)
throwableClass.E - the type of exceptionthrowableClass - the class of an exception to be comparedconsumer - a consumer functionExceptionalpublic Exceptional<T> recover(ThrowableFunction<Throwable,? extends T,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 resultNullPointerException - if function is nullpublic Exceptional<T> recoverWith(Function<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 functionNullPointerException - if function or produced result is nullCopyright © 2018. All rights reserved.