@Test public void testIfExceptionIs() { final int INTERRUPTED = 0; final int EXCEPTION = 1; final int FILE_NOT_FOUND = 2; final boolean[] data = new boolean[3]; Exceptional .of(new ThrowableSupplier<Integer, Throwable>() { @Override public Integer get() throws Throwable { throwException(ExceptionType.INTERRUPTED); return 10; } }) .ifExceptionIs(InterruptedException.class, new Consumer<InterruptedException>() { @Override public void accept(InterruptedException value) { data[INTERRUPTED] = true; } }) .ifExceptionIs(Exception.class, new Consumer<Exception>() { @Override public void accept(Exception value) { data[EXCEPTION] = true; } }) .ifExceptionIs(FileNotFoundException.class, new Consumer<Throwable>() { @Override public void accept(Throwable value) { data[FILE_NOT_FOUND] = true; } }); assertTrue(data[INTERRUPTED]); assertTrue(data[EXCEPTION]); assertFalse(data[FILE_NOT_FOUND]); }
@NotNull @SuppressWarnings("unchecked") public <E extends Throwable> Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer) { if ( (throwable != null) && (throwableClass.isAssignableFrom(throwable.getClass())) ) { consumer.accept((E) throwable); } return this; }
Exceptional { @NotNull @SuppressWarnings("unchecked") public <E extends Throwable> Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer) { if ( (throwable != null) && (throwableClass.isAssignableFrom(throwable.getClass())) ) { consumer.accept((E) throwable); } return this; } }
Exceptional { @NotNull @SuppressWarnings("unchecked") public <E extends Throwable> Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer) { if ( (throwable != null) && (throwableClass.isAssignableFrom(throwable.getClass())) ) { consumer.accept((E) throwable); } return this; } private  Exceptional(@Nullable T value, @Nullable Throwable throwable); }
Exceptional { @NotNull @SuppressWarnings("unchecked") public <E extends Throwable> Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer) { if ( (throwable != null) && (throwableClass.isAssignableFrom(throwable.getClass())) ) { consumer.accept((E) throwable); } return this; } private  Exceptional(@Nullable T value, @Nullable Throwable throwable); @NotNull static Exceptional<T> of(@NotNull ThrowableSupplier<T, Throwable> supplier); @NotNull @Contract("_ -> new") static Exceptional<T> of(@NotNull Throwable throwable); @Nullable T get(); boolean isPresent(); @Nullable T getOrElse(@Nullable T other); @Nullable T getOrElse(@NotNull Supplier<? extends T> other); @NotNull Optional<T> getOptional(); @Nullable Throwable getException(); @Nullable T getOrThrow(); @Nullable T getOrThrowRuntimeException(); @Nullable T getOrThrow(@NotNull E exception); @NotNull Exceptional<T> or(@NotNull Supplier<Exceptional<T>> supplier); @Nullable R custom(@NotNull Function<Exceptional<T>, R> function); @NotNull Exceptional<U> map(@NotNull ThrowableFunction<? super T, ? extends U, Throwable> mapper); @NotNull Exceptional<T> ifPresent(@NotNull Consumer<? super T> consumer); @NotNull Exceptional<T> ifException(@NotNull Consumer<Throwable> consumer); @NotNull @SuppressWarnings("unchecked") Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer); @NotNull Exceptional<T> recover(@NotNull final ThrowableFunction<Throwable, ? extends T, Throwable> function); @NotNull Exceptional<T> recoverWith(@NotNull final Function<Throwable, ? extends Exceptional<T>> function); @Override boolean equals(Object obj); @Override int hashCode(); @NotNull @Override String toString(); }
Exceptional { @NotNull @SuppressWarnings("unchecked") public <E extends Throwable> Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer) { if ( (throwable != null) && (throwableClass.isAssignableFrom(throwable.getClass())) ) { consumer.accept((E) throwable); } return this; } private  Exceptional(@Nullable T value, @Nullable Throwable throwable); @NotNull static Exceptional<T> of(@NotNull ThrowableSupplier<T, Throwable> supplier); @NotNull @Contract("_ -> new") static Exceptional<T> of(@NotNull Throwable throwable); @Nullable T get(); boolean isPresent(); @Nullable T getOrElse(@Nullable T other); @Nullable T getOrElse(@NotNull Supplier<? extends T> other); @NotNull Optional<T> getOptional(); @Nullable Throwable getException(); @Nullable T getOrThrow(); @Nullable T getOrThrowRuntimeException(); @Nullable T getOrThrow(@NotNull E exception); @NotNull Exceptional<T> or(@NotNull Supplier<Exceptional<T>> supplier); @Nullable R custom(@NotNull Function<Exceptional<T>, R> function); @NotNull Exceptional<U> map(@NotNull ThrowableFunction<? super T, ? extends U, Throwable> mapper); @NotNull Exceptional<T> ifPresent(@NotNull Consumer<? super T> consumer); @NotNull Exceptional<T> ifException(@NotNull Consumer<Throwable> consumer); @NotNull @SuppressWarnings("unchecked") Exceptional<T> ifExceptionIs(@NotNull Class<E> throwableClass, @NotNull Consumer<? super E> consumer); @NotNull Exceptional<T> recover(@NotNull final ThrowableFunction<Throwable, ? extends T, Throwable> function); @NotNull Exceptional<T> recoverWith(@NotNull final Function<Throwable, ? extends Exceptional<T>> function); @Override boolean equals(Object obj); @Override int hashCode(); @NotNull @Override String toString();  }