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 |
|---|---|
<R> R |
custom(Function<Exceptional<T>,R> function)
Applies custom operator on
Exceptional. |
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(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 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.
|
boolean |
isPresent()
Checks value present (i.e.
|
<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(java.lang.Throwable throwable)
Returns an
Exceptional with throwable already set. |
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() |
@NotNull public static <T> Exceptional<T> of(@NotNull ThrowableSupplier<T,java.lang.Throwable> supplier)
Exceptional with value provided by given ThrowableSupplier function.T - the type of valuesupplier - a supplier functionExceptionaljava.lang.NullPointerException - if supplier is null@NotNull @Contract(value="_ -> new") public static <T> Exceptional<T> of(@NotNull java.lang.Throwable throwable)
Exceptional with throwable already set.T - the type of valuethrowable - throwable instanceExceptional@Nullable public T get()
public boolean isPresent()
true if a value present, false otherwise@Nullable public T getOrElse(@Nullable T other)
other.other - the value to be returned if there were any exceptionother@Nullable public T getOrElse(@NotNull Supplier<? extends T> other)
other - the supplier function that produces value if there were any exception@NotNull public Optional<T> getOptional()
Optional containerOptional@Nullable public java.lang.Throwable getException()
@Nullable public T getOrThrow() throws java.lang.Throwable
java.lang.Throwable - that was thrown in supplier function@Nullable public T getOrThrowRuntimeException() throws java.lang.RuntimeException
RuntimeException.java.lang.RuntimeException - with wrapped exception which was thrown in supplier function@Nullable public <E extends java.lang.Throwable> T getOrThrow(@NotNull 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@NotNull public Exceptional<T> or(@NotNull 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 null@Nullable
public <R> R custom(@NotNull
Function<Exceptional<T>,R> function)
Exceptional.R - the type of the resultfunction - a transforming functionjava.lang.NullPointerException - if function is null@NotNull public <U> Exceptional<U> map(@NotNull 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 null@NotNull public Exceptional<T> ifPresent(@NotNull Consumer<? super T> consumer)
consumer - a consumer functionExceptional@NotNull public Exceptional<T> ifException(@NotNull Consumer<java.lang.Throwable> consumer)
consumer - a consumer functionExceptional@NotNull public <E extends java.lang.Throwable> Exceptional<T> ifExceptionIs(@NotNull java.lang.Class<E> throwableClass, @NotNull Consumer<? super E> consumer)
throwableClass.E - the type of exceptionthrowableClass - the class of an exception to be comparedconsumer - a consumer functionExceptional@NotNull public Exceptional<T> recover(@NotNull 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 null@NotNull public Exceptional<T> recoverWith(@NotNull 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.Objectpublic int hashCode()
hashCode in class java.lang.Object@NotNull public java.lang.String toString()
toString in class java.lang.Object