/** * Run the given action if and only if this is a success. * * @param action Success listener. * @return This try. */ public Value<V> onSuccess(final Consumer<V> action) { if (isSuccess()) { action.accept(get()); } return this; }
/** * Get the success value or use the given function on failure. * * @param value Default value provider. * @return Success or default value. */ public V orElseGet(Supplier<V> value) { return isSuccess() ? get() : value.get(); }
/** * Get the success value or throw an exception created by the exception provider. * * @param provider Exception provider. * @return Success value. */ public V orElseThrow(Throwing.Function<Throwable, Throwable> provider) { if (isSuccess()) { return get(); } throw Throwing.sneakyThrow(provider.apply(getCause().get())); }
/** * Always run the given action, works like a finally clause. Exception and value might be null. * Exception will be null in case of success. * * @param action Finally action. * @return This try result. */ public Value<V> onComplete(final Throwing.Consumer2<V, Throwable> action) { try { V value = isSuccess() ? get() : null; action.accept(value, getCause().orElse(null)); return this; } catch (Throwable x) { return (Value<V>) failure(x); } }
/** * Get the success value or use the given default value on failure. * * @param value Default value. * @return Success or default value. */ public V orElse(V value) { return isSuccess() ? get() : value; }