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 |
|---|---|
boolean |
equals(Object obj) |
T |
get()
Returns inner value.
|
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 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. |
<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(ThrowableSupplier<T,Throwable> supplier)
Returns an
Exceptional with value provided by given ThrowableSupplier 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 T get()
public T getOrElse(T other)
other.other - the value to be returned if there were any exceptionotherpublic 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 <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> 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 functionExceptionalCopyright © 2016. All rights reserved.