/** * Returns a tupled version of this function. * * @return a tupled function equivalent to this. */ default CheckedFunction1<Tuple1<T1>, R> tupled() { return t -> apply(t._1); }
/** * 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 CheckedFunction1<T1, R> memoized() { if (isMemoized()) { return this; } else { final Map<T1, R> cache = new HashMap<>(); return (CheckedFunction1<T1, R> & Memoized) (t1) -> { synchronized (cache) { if (cache.containsKey(t1)) { return cache.get(t1); } else { final R value = apply(t1); cache.put(t1, value); return value; } } }; } }
/** * Alias for {@link CheckedFunction1#of(CheckedFunction1)} * * @param <R> return type * @param <T1> type of the 1st argument * @param methodReference A method reference * @return A {@link CheckedFunction1} */ public static <T1, R> CheckedFunction1<T1, R> CheckedFunction(CheckedFunction1<T1, R> methodReference) { return CheckedFunction1.of(methodReference); }
public R apply(T t) throws Throwable { return function.apply(t); } }
/** * 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 CheckedFunction1<T1, R> memoized() { if (isMemoized()) { return this; } else { final Map<T1, R> cache = new HashMap<>(); return (CheckedFunction1<T1, R> & Memoized) (t1) -> { synchronized (cache) { if (cache.containsKey(t1)) { return cache.get(t1); } else { final R value = apply(t1); cache.put(t1, value); return value; } } }; } }
/** * Alias for {@link CheckedFunction1#of(CheckedFunction1)} * * @param <R> return type * @param <T1> type of the 1st argument * @param methodReference A method reference * @return A {@link CheckedFunction1} */ public static <T1, R> CheckedFunction1<T1, R> CheckedFunction(CheckedFunction1<T1, R> methodReference) { return CheckedFunction1.of(methodReference); }
/** * Returns a composed function that first applies this CheckedFunction1 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> CheckedFunction1<T1, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1) -> after.apply(apply(t1)); }
/** * Returns a composed function that first applies the {@linkplain CheckedFunction1} {@code before} the * given argument and then applies this CheckedFunction1 to the result. * * @param <V> argument type of before * @param before the function applied before this * @return a function composed of before and this * @throws NullPointerException if before is null */ default <V> CheckedFunction1<V, R> compose(CheckedFunction1<? super V, ? extends T1> before) { Objects.requireNonNull(before, "before is null"); return v -> apply(before.apply(v)); } }
/** * Returns a composed function that first applies this CheckedFunction4 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> CheckedFunction4<T1, T2, T3, T4, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4) -> after.apply(apply(t1, t2, t3, t4)); }
/** * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param <R> return type * @param <T1> 1st argument * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)} * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise. */ static <T1, R> Function1<T1, Try<R>> liftTry(CheckedFunction1<? super T1, ? extends R> partialFunction) { return t1 -> Try.of(() -> partialFunction.apply(t1)); }
/** * Returns a composed function that first applies this CheckedFunction5 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> CheckedFunction5<T1, T2, T3, T4, T5, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4, t5) -> after.apply(apply(t1, t2, t3, t4, t5)); }
/** * 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()); }
/** * Returns a composed function that first applies this CheckedFunction7 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> CheckedFunction7<T1, T2, T3, T4, T5, T6, T7, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4, t5, t6, t7) -> after.apply(apply(t1, t2, t3, t4, t5, t6, t7)); }
/** * Returns a composed function that first applies this CheckedFunction2 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> CheckedFunction2<T1, T2, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2) -> after.apply(apply(t1, t2)); }
/** * Returns a composed function that first applies this CheckedFunction8 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> CheckedFunction8<T1, T2, T3, T4, T5, T6, T7, T8, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4, t5, t6, t7, t8) -> after.apply(apply(t1, t2, t3, t4, t5, t6, t7, t8)); }
/** * Returns a composed function that first applies this CheckedFunction6 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> CheckedFunction6<T1, T2, T3, T4, T5, T6, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4, t5, t6) -> after.apply(apply(t1, t2, t3, t4, t5, t6)); }
/** * Returns a composed function that first applies this CheckedFunction3 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> CheckedFunction3<T1, T2, T3, V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3) -> after.apply(apply(t1, t2, t3)); }
/** * Returns an unchecked function that will <em>sneaky throw</em> if an exceptions occurs when applying the function. * * @return a new Function1 that throws a {@code Throwable}. */ default Function1<T1, R> unchecked() { return (t1) -> { try { return apply(t1); } catch(Throwable t) { return sneakyThrow(t); } }; }
/** * Return a composed function that first applies this CheckedFunction1 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 Function1<T1, R> recover(Function<? super Throwable, ? extends Function<? super T1, ? extends R>> recover) { Objects.requireNonNull(recover, "recover is null"); return (t1) -> { try { return this.apply(t1); } catch (Throwable throwable) { final Function<? super T1, ? extends R> func = recover.apply(throwable); Objects.requireNonNull(func, () -> "recover return null for " + throwable.getClass() + ": " + throwable.getMessage()); return func.apply(t1); } }; }
/** * Creates a function which is restricted by a RateLimiter. * * @param rateLimiter the RateLimiter * @param function the original function * @param <T> the type of function argument * @param <R> the type of function results * @return a function which is restricted by a RateLimiter. */ static <T, R> CheckedFunction1<T, R> decorateCheckedFunction(RateLimiter rateLimiter, CheckedFunction1<T, R> function) { return (T t) -> { waitForPermission(rateLimiter); return function.apply(t); }; }