/** * Returns a tupled version of this function. * * @return a tupled function equivalent to this. */ default CheckedFunction1<Tuple0, R> tupled() { return t -> apply(); }
/** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. * <p> * Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default CheckedFunction0<R> memoized() { if (isMemoized()) { return this; } else { final Lazy<R> lazy = Lazy.of(() -> { try { return apply(); } catch (Throwable x) { throw new RuntimeException(x); } }); return (CheckedFunction0<R> & Memoized) () -> { try { return lazy.get(); } catch(RuntimeException x) { throw x.getCause(); } }; } }
/** * Alias for {@link CheckedFunction0#of(CheckedFunction0)} * * @param <R> return type * @param methodReference A method reference * @return A {@link CheckedFunction0} */ public static <R> CheckedFunction0<R> CheckedFunction(CheckedFunction0<R> methodReference) { return CheckedFunction0.of(methodReference); }
/** * Alias for {@link CheckedFunction0#unchecked} * * @param <R> return type * @param f A method reference * @return An unchecked wrapper of supplied {@link CheckedFunction0} */ public static <R> Function0<R> unchecked(CheckedFunction0<R> f) { return f.unchecked(); }
/** * Alias for {@link CheckedFunction0#unchecked} * * @param <R> return type * @param f A method reference * @return An unchecked wrapper of supplied {@link CheckedFunction0} */ public static <R> Function0<R> unchecked(CheckedFunction0<R> f) { return f.unchecked(); }
public T get() throws Throwable { return supplier.apply(); } }
/** * Returns a memoizing version of this function, which computes the return value for given arguments only one time. * On subsequent calls given the same arguments the memoized value is returned. * <p> * Please note that memoizing functions do not permit {@code null} as single argument or return value. * * @return a memoizing function equivalent to this. */ default CheckedFunction0<R> memoized() { if (isMemoized()) { return this; } else { final Lazy<R> lazy = Lazy.of(() -> { try { return apply(); } catch (Throwable x) { throw new RuntimeException(x); } }); return (CheckedFunction0<R> & Memoized) () -> { try { return lazy.get(); } catch(RuntimeException x) { throw x.getCause(); } }; } }
/** * Alias for {@link CheckedFunction0#of(CheckedFunction0)} * * @param <R> return type * @param methodReference A method reference * @return A {@link CheckedFunction0} */ public static <R> CheckedFunction0<R> CheckedFunction(CheckedFunction0<R> methodReference) { return CheckedFunction0.of(methodReference); }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes seven {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction7<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply(); T6 t6 = t6Supplier.apply(); T7 t7 = t7Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5, t6, t7); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes eight {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try"/* https://bugs.openjdk.java.net/browse/JDK-8155591 */) public <R> Try<R> of(CheckedFunction8<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply(); T6 t6 = t6Supplier.apply(); T7 t7 = t7Supplier.apply(); T8 t8 = t8Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5, t6, t7, t8); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes six {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction6<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply(); T6 t6 = t6Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5, t6); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes five {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction5<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes four {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply()) { return f.apply(t1, t2, t3, t4); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes three {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction3<? super T1, ? super T2, ? super T3, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply()) { return f.apply(t1, t2, t3); } }); } }
/** * Returns a composed function that first applies this CheckedFunction0 to the given argument and then applies * {@linkplain CheckedFunction1} {@code after} to the result. * * @param <V> return type of after * @param after the function applied after this * @return a function composed of this and after * @throws NullPointerException if after is null */ default <V> CheckedFunction0<V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return () -> after.apply(apply()); }
/** * Return a composed function that first applies this CheckedFunction0 to the given arguments and in case of throwable * try to get value from {@code recover} function with same arguments and throwable information. * * @param recover the function applied in case of throwable * @return a function composed of this and recover * @throws NullPointerException if recover is null */ default Function0<R> recover(Function<? super Throwable, ? extends Supplier<? extends R>> recover) { Objects.requireNonNull(recover, "recover is null"); return () -> { try { return this.apply(); } catch (Throwable throwable) { final Supplier<? extends R> func = recover.apply(throwable); Objects.requireNonNull(func, () -> "recover return null for " + throwable.getClass() + ": " + throwable.getMessage()); return func.get(); } }; }
/** * Returns an unchecked function that will <em>sneaky throw</em> if an exceptions occurs when applying the function. * * @return a new Function0 that throws a {@code Throwable}. */ default Function0<R> unchecked() { return () -> { try { return apply(); } catch(Throwable t) { return sneakyThrow(t); } }; }
/** * Creates a supplier which is restricted by a RateLimiter. * * @param rateLimiter the RateLimiter * @param supplier the original supplier * @param <T> the type of results supplied supplier * @return a supplier which is restricted by a RateLimiter. */ static <T> CheckedFunction0<T> decorateCheckedSupplier(RateLimiter rateLimiter, CheckedFunction0<T> supplier) { return () -> { waitForPermission(rateLimiter); return supplier.apply(); }; }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes two {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction2<? super T1, ? super T2, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply()) { return f.apply(t1, t2); } }); } }
/** * Creates a Try of a CheckedFunction0. * * @param supplier A checked supplier * @param <T> Component type * @return {@code Success(supplier.apply())} if no exception occurs, otherwise {@code Failure(throwable)} if an * exception occurs calling {@code supplier.apply()}. */ static <T> Try<T> of(CheckedFunction0<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); try { return new Success<>(supplier.apply()); } catch (Throwable t) { return new Failure<>(t); } }