@Override public CompletionStage<T> apply(T r, Throwable ex) { return (ex == null) ? thisStage : fn.apply(ex); } }).thenCompose(Functions.<CompletionStage<T>>identity());
/** * Returns a composed function that first applies the {@code before} * function to its input, and then applies the {@code this_} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <R> the type of the result of the {@code this_} function and of the composed function * @param <T> the type of the input to the {@code this_} and of the result of the {@code before} function * @param <V> the type of input to the {@code before} function, and to the * composed function * @param this_ the function to apply after the {@code before} function is applied * @param before the function to apply before the {@code this_} function is applied * @return a composed function that first applies the {@code before} * function and then applies the {@code this_} function * @throws NullPointerException if {@code this_} is null * @throws NullPointerException if before is null * * @see #andThen(Function, Function) */ public static <R, T, V> Function<V, R> compose(Function<? super T, ? extends R> this_, Function<? super V, ? extends T> before) { Objects.requireNonNull(this_); Objects.requireNonNull(before); return (V v) -> this_.apply(before.apply(v)); }
/** * Returns a composed function that first applies the {@code this_} function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <R> the type of the result of the {@code this_} function and of the input to the {@code after} function. * @param <T> the type of the input to the {@code this_} function and to the composed function * @param <V> the type of output of the {@code after} function, and of the * composed function * @param this_ the function to apply before the {@code after} function is applied * @param after the function to apply after the {@code this_} function is applied * @return a composed function that first applies the {@code this_} function and then * applies the {@code after} function * @throws NullPointerException if {@code this_} is null * @throws NullPointerException if after is null * * @see #compose(Function, Function) */ public static <R, T, V> Function<T, V> andThen(Function<? super T, ? extends R> this_, Function<? super R, ? extends V> after) { Objects.requireNonNull(this_); Objects.requireNonNull(after); return (T t) -> after.apply(this_.apply(t)); }
/** * Returns a composed function that first applies the {@code before} * function to its input, and then applies the {@code this_} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <R> the type of the result of the {@code this_} function and of the composed function * @param <T> the type of the input to the {@code this_} and of the result of the {@code before} function * @param <V> the type of input to the {@code before} function, and to the * composed function * @param this_ the function to apply after the {@code before} function is applied * @param before the function to apply before the {@code this_} function is applied * @return a composed function that first applies the {@code before} * function and then applies the {@code this_} function * @throws NullPointerException if {@code this_} is null * @throws NullPointerException if before is null * * @see #andThen(Function, Function) */ public static <R, T, V> Function<V, R> compose(Function<? super T, ? extends R> this_, Function<? super V, ? extends T> before) { Objects.requireNonNull(this_); Objects.requireNonNull(before); return (V v) -> this_.apply(before.apply(v)); }
/** * Returns a composed function that first applies the {@code this_} function to * its input, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <R> the type of the result of the {@code this_} function and of the input to the {@code after} function. * @param <T> the type of the input to the {@code this_} function and to the composed function * @param <V> the type of output of the {@code after} function, and of the * composed function * @param this_ the function to apply before the {@code after} function is applied * @param after the function to apply after the {@code this_} function is applied * @return a composed function that first applies the {@code this_} function and then * applies the {@code after} function * @throws NullPointerException if {@code this_} is null * @throws NullPointerException if after is null * * @see #compose(Function, Function) */ public static <R, T, V> Function<T, V> andThen(Function<? super T, ? extends R> this_, Function<? super R, ? extends V> after) { Objects.requireNonNull(this_); Objects.requireNonNull(after); return (T t) -> after.apply(this_.apply(t)); }
/** * {@code BiConsumer<Map, T>} that accumulates (key, value) pairs * extracted from elements into the map, throwing {@code IllegalStateException} * if duplicate keys are encountered. * * @param keyMapper a function that maps an element into a key * @param valueMapper a function that maps an element into a value * @param <T> type of elements * @param <K> type of map keys * @param <V> type of map values * @return an accumulating consumer */ private static <T, K, V> BiConsumer<Map<K, V>, T> uniqKeysMapAccumulator(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { return (map, element) -> { K k = keyMapper.apply(element); V v = Objects.requireNonNull(valueMapper.apply(element)); V u = null; if (map instanceof ConcurrentMap) { u = ((ConcurrentMap<K, V>) map).putIfAbsent(k, v); } else { u = Maps.putIfAbsent(map, k, v); } if (u != null) { throw duplicateKeyException(k, u, v); } }; }
/** * {@code BiConsumer<Map, T>} that accumulates (key, value) pairs * extracted from elements into the map, throwing {@code IllegalStateException} * if duplicate keys are encountered. * * @param keyMapper a function that maps an element into a key * @param valueMapper a function that maps an element into a value * @param <T> type of elements * @param <K> type of map keys * @param <V> type of map values * @return an accumulating consumer */ private static <T, K, V> BiConsumer<Map<K, V>, T> uniqKeysMapAccumulator(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { return (map, element) -> { K k = keyMapper.apply(element); V v = Objects.requireNonNull(valueMapper.apply(element)); V u = null; if (map instanceof ConcurrentMap) { u = ((ConcurrentMap<K, V>) map).putIfAbsent(k, v); } else { u = Maps.putIfAbsent(map, k, v); } if (u != null) { throw duplicateKeyException(k, u, v); } }; }
V oldValue, newValue; return ((oldValue = map.get(key)) == null && (newValue = mappingFunction.apply(key)) != null && (oldValue = map.putIfAbsent(key, newValue)) == null) ? newValue
/** * Returns a composed function that first applies the {@code this_} function to * its inputs, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <R> the type of the result of the {@code this_} function and the type of the input of the {@code after} function. * @param <T> the type of the first argument to the {@code this_} function * @param <U> the type of the second argument to the {@code this_} function * @param <V> the type of output of the {@code after} function, and of the * composed function * @param this_ the {@code BiFunction} to be applied first. * @param after the function to apply after the {@code this_} function is applied * @return a composed function that first applies the {@code this_} function and then * applies the {@code after} function * @throws NullPointerException if {@code this_} is null * @throws NullPointerException if after is null */ public static <R, T, U, V> BiFunction<T, U, V> andThen(BiFunction<? super T, ? super U, ? extends R> this_, Function<? super R, ? extends V> after) { Objects.requireNonNull(this_); Objects.requireNonNull(after); return (T t, U u) -> after.apply(this_.apply(t, u)); }
/** * Returns a composed function that first applies the {@code this_} function to * its inputs, and then applies the {@code after} function to the result. * If evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param <R> the type of the result of the {@code this_} function and the type of the input of the {@code after} function. * @param <T> the type of the first argument to the {@code this_} function * @param <U> the type of the second argument to the {@code this_} function * @param <V> the type of output of the {@code after} function, and of the * composed function * @param this_ the {@code BiFunction} to be applied first. * @param after the function to apply after the {@code this_} function is applied * @return a composed function that first applies the {@code this_} function and then * applies the {@code after} function * @throws NullPointerException if {@code this_} is null * @throws NullPointerException if after is null */ public static <R, T, U, V> BiFunction<T, U, V> andThen(BiFunction<? super T, ? super U, ? extends R> this_, Function<? super R, ? extends V> after) { Objects.requireNonNull(this_); Objects.requireNonNull(after); return (T t, U u) -> after.apply(this_.apply(t, u)); }
final boolean uniExceptionally(Object r, Function<? super Throwable, ? extends T> f, UniExceptionally<T> c) { Throwable x; if (result == null) { try { if (c != null && !c.claim()) return false; if (r instanceof AltResult && (x = ((AltResult)r).ex) != null) completeValue(f.apply(x)); else internalComplete(r); } catch (Throwable ex) { completeThrowable(ex); } } return true; }
private <V> CompletableFuture<V> uniApplyNow( Object r, Executor e, Function<? super T,? extends V> f) { Throwable x; CompletableFuture<V> d = newIncompleteFuture(); if (r instanceof AltResult) { if ((x = ((AltResult)r).ex) != null) { d.result = encodeThrowable(x, r); return d; } r = null; } try { if (e != null) { e.execute(new UniApply<T,V>(null, d, this, f)); } else { @SuppressWarnings("unchecked") T t = (T) r; d.result = d.encodeValue(f.apply(t)); } } catch (Throwable ex) { d.result = encodeThrowable(ex); } return d; }