public R orElseThrow(Throwing.Supplier<Throwable> exception) { return toOptional().orElseThrow(() -> Throwing.sneakyThrow(exception.get())); }
/** * Singleton version of this supplier. * * @return A memo function. */ default Supplier<V> singleton() { if (this instanceof Memoized) { return this; } AtomicReference<V> ref = new AtomicReference<>(); return (Supplier<V> & Memoized) () -> { if (ref.get() == null) { ref.set(tryGet()); } return ref.get(); }; } }
/** * Apply the resource and produces an output. * * @param fn Function to apply. * @param <V> Output type. * @return A new try result. */ public <V> Value<V> apply(Throwing.Function<R, V> fn) { return Try.apply(() -> { try (R r1 = this.r.get()) { if (r1 instanceof ProxyCloseable) { return fn.apply((R) ((ProxyCloseable) r1).resource); } return fn.apply(r1); } }); }
/** * Creates a new try from given value provider. * * @param fn Value provider. * @param <V> Value type. * @return A new success try or failure try in case of exception. */ public static <V> Value<V> apply(Throwing.Supplier<? extends V> fn) { try { return new Success<>(fn.get()); } catch (Throwable x) { return new Failure(x); } }
@Override public R tryGet() throws Throwable { if (parent instanceof ProxyCloseable) { ProxyCloseable proxy = (ProxyCloseable) parent; if (proxy.resource == null) { proxy.get(); } this.parentResource = (P) proxy.resource; } else { this.parentResource = parent.get(); } this.resource = mapper.apply(parentResource); return (R) this; } }
private static <V, X extends Throwable> V fnUnwrap(Supplier<V> fn, Class<? extends X> type) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } Throwable t = x; if (type.isInstance(x)) { t = Optional.ofNullable(x.getCause()).orElse(x); } throw sneakyThrow(t); } }
/** * Run an operation over the resource. * * @param fn Function to apply. * @return A new try result. */ public Try run(Throwing.Consumer<R> fn) { return Try.run(() -> { try (R r1 = this.r.get()) { fn.accept(r1); } }); } }
private static <R, X extends Throwable> R fnRecover(Supplier<R> fn, Class<? extends X> type, java.util.function.Function<X, R> recover) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } if (type.isInstance(x)) { return recover.apply(type.cast(x)); } throw sneakyThrow(x); } }
public <T extends V> Value<V, R> is(Throwing.Predicate<T> predicate, Throwing.Supplier<R> result) { return is(predicate, v -> result.get()); }
private static <T> T newObject(final Class<T> klass) { return throwingSupplier(klass::newInstance).get(); }
public Value<V, R> is(V value, Throwing.Supplier<R> result) { return is(source -> Objects.equals(source, value), v -> result.get()); }
@Override public MemcachedClient get() { client = throwingSupplier(() -> { ConnectionFactory connectionFactory = builder.build(); this.builder = null; return new MemcachedClient(connectionFactory, servers); }).get(); return client; }
private final static <R> R memo(Map<Object, R> cache, List<Object> key, Supplier<R> fn) { synchronized (cache) { R value = cache.get(key); if (value == null) { value = fn.get(); cache.put(key, value); } return value; } } }
private static <V> V fnWrap(Supplier<V> fn, java.util.function.Function<Throwable, Exception> wrapper) { try { return fn.tryGet(); } catch (Throwable x) { if (isFatal(x)) { throw sneakyThrow(x); } throw sneakyThrow(wrapper.apply(x)); } }
/** * Apply this function and unwrap any resulting exception. Useful to get clean/shorter stacktrace. * * @param type Exception to unwrap. * @param <X> Exception type. * @return A new function. */ default <X extends Throwable> Supplier<V> unwrap(Class<? extends X> type) { return () -> fnUnwrap(() -> tryGet(), type); }
/** * Apply this function or recover from it in case of exception. * * @param fn Exception recover. * @return A new function. */ default Supplier<V> recover(java.util.function.Function<Throwable, V> fn) { return recover(Throwable.class, fn); }
/** * Apply this function and returns the given default value in case of exception. * * @param defaultValue Exceptional default value. * @return A new function. */ default Supplier<V> orElse(V defaultValue) { return orElse(() -> defaultValue); }