OpenJDK / jdk / hs
changeset 17695:032254f2467b
8004015: Additional static and instance utils for functional interfaces.
8011010: Spec j.u.f.Predicate doesn't specify NPEs thrown by the SE8's Reference Implementation
Reviewed-by: briangoetz, dholmes, chegar
line wrap: on
line diff
--- a/jdk/src/share/classes/java/util/function/BiConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/BiConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -24,14 +24,16 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation which accepts two input arguments and returns no result. This is * the two-arity specialization of {@link Consumer}. Unlike most other * functional interfaces, {@code BiConsumer} is expected to operate via * side-effects. * - * @param <T> the type of the first argument to the {@code accept} operation. - * @param <U> the type of the second argument to the {@code accept} operation. + * @param <T> the type of the first argument to the {@code accept} operation + * @param <U> the type of the second argument to the {@code accept} operation * * @see Consumer * @since 1.8 @@ -47,4 +49,28 @@ * @param u an input object */ void accept(T t, U u); + + /** + * Returns a {@code BiConsumer} which performs, in sequence, the operation + * represented by this object followed by the operation represented by + * the other {@code BiConsumer}. + * + * <p>Any exceptions thrown by either {@code accept} method are relayed + * to the caller; if performing this operation throws an exception, the + * other operation will not be performed. + * + * @param other a BiConsumer which will be chained after this BiConsumer + * @return a BiConsumer which performs in sequence the {@code accept} method + * of this BiConsumer and the {@code accept} method of the specified + * BiConsumer operation + * @throws NullPointerException if other is null + */ + default BiConsumer<T, U> chain(BiConsumer<? super T, ? super U> other) { + Objects.requireNonNull(other); + + return (l, r) -> { + accept(l, r); + other.accept(l, r); + }; + } }
--- a/jdk/src/share/classes/java/util/function/BiFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/BiFunction.java Fri May 17 10:36:04 2013 -0700 @@ -24,15 +24,17 @@ */ package java.util.function; +import java.util.Objects; + /** * Apply a function to the input arguments, yielding an appropriate result. This * is the two-arity specialization of {@link Function}. A function may * variously provide a mapping between types, object instances or keys and * values or any other form of transformation upon the input. * - * @param <T> the type of the first argument to the {@code apply} operation. - * @param <U> the type of the second argument to the {@code apply} operation. - * @param <R> the type of results returned by the {@code apply} operation. + * @param <T> the type of the first argument to the {@code apply} operation + * @param <U> the type of the second argument to the {@code apply} operation + * @param <R> the type of results returned by the {@code apply} operation * * @see Function * @since 1.8 @@ -48,4 +50,22 @@ * @return the function result */ R apply(T t, U u); + + /** + * Returns a new function which applies this function followed by the + * provided function. If either function throws an exception, it is relayed + * to the caller. + * + * @param <V> Type of output objects to the combined function. May be the + * same type as {@code <T>}, {@code <U>} or {@code <R>} + * @param after An additional function to be applied after this function is + * applied + * @return A function which performs this function followed by the provided + * function + * @throws NullPointerException if after is null + */ + default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) { + Objects.requireNonNull(after); + return (T t, U u) -> after.apply(apply(t, u)); + } }
--- a/jdk/src/share/classes/java/util/function/BiPredicate.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/BiPredicate.java Fri May 17 10:36:04 2013 -0700 @@ -30,8 +30,8 @@ * Determines if the input objects match some criteria. This is the two-arity * specialization of {@link Predicate}. * - * @param <T> the type of the first argument to {@code test}. - * @param <U> the type of the second argument to {@code test}. + * @param <T> the type of the first argument to {@code test} + * @param <U> the type of the second argument to {@code test} * * @see Predicate * @since 1.8 @@ -42,9 +42,9 @@ /** * Return {@code true} if the inputs match some criteria. * - * @param t an input object. - * @param u an input object. - * @return {@code true} if the inputs match some criteria. + * @param t an input object + * @param u an input object + * @return {@code true} if the inputs match some criteria */ boolean test(T t, U u); @@ -54,11 +54,12 @@ * this predicate returns {@code false} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ANDed with this predicate. + * @param p a predicate which will be logically-ANDed with this predicate * @return a new predicate which returns {@code true} only if both - * predicates return {@code true}. + * predicates return {@code true} + * @throws NullPointerException if p is null */ - public default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> p) { + default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> p) { Objects.requireNonNull(p); return (T t, U u) -> test(t, u) && p.test(t, u); } @@ -67,9 +68,9 @@ * Returns a predicate which negates the result of this predicate. * * @return a new predicate who's result is always the opposite of this - * predicate. + * predicate */ - public default BiPredicate<T, U> negate() { + default BiPredicate<T, U> negate() { return (T t, U u) -> !test(t, u); } @@ -79,25 +80,13 @@ * predicate returns {@code true} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ORed with this predicate. + * @param p a predicate which will be logically-ORed with this predicate * @return a new predicate which returns {@code true} if either predicate - * returns {@code true}. + * returns {@code true} + * @throws NullPointerException if p is null */ - public default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> p) { + default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> p) { Objects.requireNonNull(p); return (T t, U u) -> test(t, u) || p.test(t, u); } - - /** - * Returns a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. - * - * @param p a predicate which will be logically-XORed with this predicate. - * @return a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. - */ - public default BiPredicate<T, U> xor(BiPredicate<? super T, ? super U> p) { - Objects.requireNonNull(p); - return (T t, U u) -> test(t, u) ^ p.test(t, u); - } }
--- a/jdk/src/share/classes/java/util/function/BooleanSupplier.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/BooleanSupplier.java Fri May 17 10:36:04 2013 -0700 @@ -40,5 +40,5 @@ * * @return a {@code boolean} value */ - public boolean getAsBoolean(); + boolean getAsBoolean(); }
--- a/jdk/src/share/classes/java/util/function/Consumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/Consumer.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation which accepts a single input argument and returns no result. * Unlike most other functional interfaces, {@code Consumer} is expected to @@ -41,5 +43,25 @@ * * @param t the input object */ - public void accept(T t); + void accept(T t); + + /** + * Returns a {@code Consumer} which performs, in sequence, the operation + * represented by this object followed by the operation represented by + * the other {@code Consumer}. + * + * <p>Any exceptions thrown by either {@code accept} method are relayed + * to the caller; if performing this operation throws an exception, the + * other operation will not be performed. + * + * @param other a Consumer which will be chained after this Consumer + * @return a Consumer which performs in sequence the {@code accept} method + * of this Consumer and the {@code accept} method of the specified Consumer + * operation + * @throws NullPointerException if other is null + */ + default Consumer<T> chain(Consumer<? super T> other) { + Objects.requireNonNull(other); + return (T t) -> { accept(t); other.accept(t); }; + } }
--- a/jdk/src/share/classes/java/util/function/DoubleBinaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/DoubleBinaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -43,5 +43,5 @@ * @param right the right operand value * @return the result of the operation */ - public double applyAsDouble(double left, double right); + double applyAsDouble(double left, double right); }
--- a/jdk/src/share/classes/java/util/function/DoubleConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/DoubleConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation which accepts a single double argument and returns no result. * This is the primitive type specialization of {@link Consumer} for @@ -41,5 +43,26 @@ * * @param value the input value */ - public void accept(double value); + void accept(double value); + + /** + * Returns a {@code DoubleConsumer} which performs, in sequence, the operation + * represented by this object followed by the operation represented by + * another {@code DoubleConsumer}. + * + * <p>Any exceptions thrown by either {@code accept} method are relayed + * to the caller; if performing this operation throws an exception, the + * other operation will not be performed. + * + * @param other a DoubleConsumer which will be chained after this + * DoubleConsumer + * @return an DoubleConsumer which performs in sequence the {@code accept} method + * of this DoubleConsumer and the {@code accept} method of the specified IntConsumer + * operation + * @throws NullPointerException if other is null + */ + default DoubleConsumer chain(DoubleConsumer other) { + Objects.requireNonNull(other); + return (double t) -> { accept(t); other.accept(t); }; + } }
--- a/jdk/src/share/classes/java/util/function/DoubleFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/DoubleFunction.java Fri May 17 10:36:04 2013 -0700 @@ -43,5 +43,5 @@ * @param value the input value * @return the function result */ - public R apply(double value); + R apply(double value); }
--- a/jdk/src/share/classes/java/util/function/DoublePredicate.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/DoublePredicate.java Fri May 17 10:36:04 2013 -0700 @@ -40,11 +40,11 @@ /** * Returns {@code true} if the input value matches some criteria. * - * @param value the value to be tested. + * @param value the value to be tested * @return {@code true} if the input value matches some criteria, otherwise - * {@code false}. + * {@code false} */ - public boolean test(double value); + boolean test(double value); /** * Returns a predicate which evaluates to {@code true} only if this @@ -52,11 +52,16 @@ * this predicate returns {@code false} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ANDed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ANDed with this predicate * @return a new predicate which returns {@code true} only if both - * predicates return {@code true}. + * predicates return {@code true} + * @throws NullPointerException if p is null */ - public default DoublePredicate and(DoublePredicate p) { + default DoublePredicate and(DoublePredicate p) { Objects.requireNonNull(p); return (value) -> test(value) && p.test(value); } @@ -65,9 +70,9 @@ * Returns a predicate which negates the result of this predicate. * * @return a new predicate who's result is always the opposite of this - * predicate. + * predicate */ - public default DoublePredicate negate() { + default DoublePredicate negate() { return (value) -> !test(value); } @@ -77,25 +82,17 @@ * predicate returns {@code true} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ANDed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ANDed with this predicate * @return a new predicate which returns {@code true} if either predicate - * returns {@code true}. + * returns {@code true} + * @throws NullPointerException if p is null */ - public default DoublePredicate or(DoublePredicate p) { + default DoublePredicate or(DoublePredicate p) { Objects.requireNonNull(p); return (value) -> test(value) || p.test(value); } - - /** - * Returns a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. - * - * @param p a predicate which will be logically-XORed with this predicate. - * @return a predicate that evaluates to {@code true} if all or none of the - * component predicates evaluate to {@code true}. - */ - public default DoublePredicate xor(DoublePredicate p) { - Objects.requireNonNull(p); - return (value) -> test(value) ^ p.test(value); - } }
--- a/jdk/src/share/classes/java/util/function/DoubleSupplier.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/DoubleSupplier.java Fri May 17 10:36:04 2013 -0700 @@ -39,5 +39,5 @@ * * @return a {@code double} value */ - public double getAsDouble(); + double getAsDouble(); }
--- a/jdk/src/share/classes/java/util/function/DoubleUnaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/DoubleUnaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation on a {@code double} operand yielding a {@code double} * result. This is the primitive type specialization of {@link UnaryOperator} @@ -42,5 +44,46 @@ * @param operand the operand value * @return the operation result value */ - public double applyAsDouble(double operand); + double applyAsDouble(double operand); + + /** + * Compose a new function which applies the provided function followed by + * this function. If either function throws an exception, it is relayed + * to the caller. + * + * @param before An additional function to be applied before this function + * is applied + * @return A function which performs the provided function followed by this + * function + * @throws NullPointerException if before is null + */ + default DoubleUnaryOperator compose(DoubleUnaryOperator before) { + Objects.requireNonNull(before); + return (double v) -> applyAsDouble(before.applyAsDouble(v)); + } + + /** + * Compose a new function which applies this function followed by the + * provided function. If either function throws an exception, it is relayed + * to the caller. + * + * @param after An additional function to be applied after this function is + * applied + * @return A function which performs this function followed by the provided + * function followed + * @throws NullPointerException if after is null + */ + default DoubleUnaryOperator andThen(DoubleUnaryOperator after) { + Objects.requireNonNull(after); + return (double t) -> after.applyAsDouble(applyAsDouble(t)); + } + + /** + * Returns a unary operator that provides its input value as the result. + * + * @return a unary operator that provides its input value as the result + */ + static DoubleUnaryOperator identity() { + return t -> t; + } }
--- a/jdk/src/share/classes/java/util/function/Function.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/Function.java Fri May 17 10:36:04 2013 -0700 @@ -24,14 +24,15 @@ */ package java.util.function; +import java.util.Objects; /** * Apply a function to the input argument, yielding an appropriate result. A * function may variously provide a mapping between types, object instances or * keys and values or any other form of transformation upon the input. * - * @param <T> the type of the input to the {@code apply} operation. - * @param <R> the type of the result of the {@code apply} operation. + * @param <T> the type of the input to the {@code apply} operation + * @param <R> the type of the result of the {@code apply} operation * * @since 1.8 */ @@ -44,5 +45,50 @@ * @param t the input object * @return the function result */ - public R apply(T t); + R apply(T t); + + /** + * Returns a new function which applies the provided function followed by + * this function. If either function throws an exception, it is relayed + * to the caller. + * + * @param <V> type of input objects to the combined function. May be the + * same type as {@code <T>} or {@code <R>} + * @param before an additional function to be applied before this function + * is applied + * @return a function which performs the provided function followed by this + * function + * @throws NullPointerException if before is null + */ + default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { + Objects.requireNonNull(before); + return (V v) -> apply(before.apply(v)); + } + + /** + * Returns a new function which applies this function followed by the + * provided function. If either function throws an exception, it is relayed + * to the caller. + * + * @param <V> type of output objects to the combined function. May be the + * same type as {@code <T>} or {@code <R>} + * @param after an additional function to be applied after this function is + * applied + * @return a function which performs this function followed by the provided + * function + * @throws NullPointerException if after is null + */ + default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { + Objects.requireNonNull(after); + return (T t) -> after.apply(apply(t)); + } + + /** + * Returns a {@code Function} whose {@code apply} method returns its input. + * + * @param <T> the type of the input and output objects to the function + */ + static <T> Function<T, T> identity() { + return t -> t; + } }
--- a/jdk/src/share/classes/java/util/function/IntBinaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/IntBinaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -44,5 +44,5 @@ * @param right the right operand value * @return the result of the operation */ - public int applyAsInt(int left, int right); + int applyAsInt(int left, int right); }
--- a/jdk/src/share/classes/java/util/function/IntConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/IntConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation which accepts a single integer argument and returns no result. * This is the primitive type specialization of {@link Consumer} for {@code int}. @@ -41,5 +43,26 @@ * * @param value the input value */ - public void accept(int value); + void accept(int value); + + /** + * Returns an {@code IntConsumer} which performs, in sequence, the operation + * represented by this object followed by the operation represented by + * another {@code IntConsumer}. + * + * <p>Any exceptions thrown by either {@code accept} method are relayed + * to the caller; if performing this operation throws an exception, the + * other operation will not be performed. + * + * @param other an IntConsumer which will be chained after this + * IntConsumer + * @return an IntConsumer which performs in sequence the {@code accept} method + * of this IntConsumer and the {@code accept} method of the specified IntConsumer + * operation + * @throws NullPointerException if other is null + */ + default IntConsumer chain(IntConsumer other) { + Objects.requireNonNull(other); + return (int t) -> { accept(t); other.accept(t); }; + } }
--- a/jdk/src/share/classes/java/util/function/IntFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/IntFunction.java Fri May 17 10:36:04 2013 -0700 @@ -43,5 +43,5 @@ * @param value the input value * @return the function result */ - public R apply(int value); + R apply(int value); }
--- a/jdk/src/share/classes/java/util/function/IntPredicate.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/IntPredicate.java Fri May 17 10:36:04 2013 -0700 @@ -39,11 +39,11 @@ /** * Returns {@code true} if the input value matches some criteria. * - * @param value the value to be tested. + * @param value the value to be tested * @return {@code true} if the input value matches some criteria, otherwise * {@code false} */ - public boolean test(int value); + boolean test(int value); /** * Returns a predicate which evaluates to {@code true} only if this @@ -51,11 +51,16 @@ * this predicate returns {@code false} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ANDed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ANDed with this predicate * @return a new predicate which returns {@code true} only if both - * predicates return {@code true}. + * predicates return {@code true} + * @throws NullPointerException if p is null */ - public default IntPredicate and(IntPredicate p) { + default IntPredicate and(IntPredicate p) { Objects.requireNonNull(p); return (value) -> test(value) && p.test(value); } @@ -64,9 +69,9 @@ * Returns a predicate which negates the result of this predicate. * * @return a new predicate who's result is always the opposite of this - * predicate. + * predicate */ - public default IntPredicate negate() { + default IntPredicate negate() { return (value) -> !test(value); } @@ -76,25 +81,17 @@ * predicate returns {@code true} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ORed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ORed with this predicate * @return a new predicate which returns {@code true} if either predicate - * returns {@code true}. + * returns {@code true} + * @throws NullPointerException if p is null */ - public default IntPredicate or(IntPredicate p) { + default IntPredicate or(IntPredicate p) { Objects.requireNonNull(p); return (value) -> test(value) || p.test(value); } - - /** - * Returns a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. - * - * @param p a predicate which will be logically-XORed with this predicate. - * @return a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true} - */ - public default IntPredicate xor(IntPredicate p) { - Objects.requireNonNull(p); - return (value) -> test(value) ^ p.test(value); - } }
--- a/jdk/src/share/classes/java/util/function/IntSupplier.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/IntSupplier.java Fri May 17 10:36:04 2013 -0700 @@ -39,5 +39,5 @@ * * @return an {@code int} value */ - public int getAsInt(); + int getAsInt(); }
--- a/jdk/src/share/classes/java/util/function/IntUnaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/IntUnaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation on a single {@code int} operand yielding an {@code int} result. * This is the primitive type specialization of {@link UnaryOperator} for @@ -37,10 +39,51 @@ /** * Returns the {@code int} value result of the operation upon the - * {@code int} operand. + * {@code int} operand. * * @param operand the operand value * @return the operation result value */ - public int applyAsInt(int operand); + int applyAsInt(int operand); + + /** + * Compose a new function which applies the provided function followed by + * this function. If either function throws an exception, it is relayed + * to the caller. + * + * @param before an additional function to be applied before this function + * is applied + * @return a function which performs the provided function followed by this + * function + * @throws NullPointerException if before is null + */ + default IntUnaryOperator compose(IntUnaryOperator before) { + Objects.requireNonNull(before); + return (int v) -> applyAsInt(before.applyAsInt(v)); + } + + /** + * Compose a new function which applies this function followed by the + * provided function. If either function throws an exception, it is relayed + * to the caller. + * + * @param after an additional function to be applied after this function is + * applied + * @return a function which performs this function followed by the provided + * function followed + * @throws NullPointerException if after is null + */ + default IntUnaryOperator andThen(IntUnaryOperator after) { + Objects.requireNonNull(after); + return (int t) -> after.applyAsInt(applyAsInt(t)); + } + + /** + * Returns a unary operator that provides its input value as the result. + * + * @return a unary operator that provides its input value as the result + */ + static IntUnaryOperator identity() { + return t -> t; + } }
--- a/jdk/src/share/classes/java/util/function/LongBinaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/LongBinaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -44,5 +44,5 @@ * @param right the right operand value * @return the result of the operation */ - public long applyAsLong(long left, long right); + long applyAsLong(long left, long right); }
--- a/jdk/src/share/classes/java/util/function/LongConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/LongConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation which accepts a single long argument and returns no result. * This is the {@code long}-consuming primitive type specialization of @@ -41,5 +43,26 @@ * * @param value the input value */ - public void accept(long value); + void accept(long value); + + /** + * Returns a {@code LongConsumer} which performs, in sequence, the operation + * represented by this object followed by the operation represented by + * another {@code LongConsumer}. + * + * <p>Any exceptions thrown by either {@code accept} method are relayed + * to the caller; if performing this operation throws an exception, the + * other operation will not be performed. + * + * @param other a LongConsumer which will be chained after this + * LongConsumer + * @return a LongConsumer which performs in sequence the {@code accept} method + * of this LongConsumer and the {@code accept} method of the specified LongConsumer + * operation + * @throws NullPointerException if other is null + */ + default LongConsumer chain(LongConsumer other) { + Objects.requireNonNull(other); + return (long t) -> { accept(t); other.accept(t); }; + } }
--- a/jdk/src/share/classes/java/util/function/LongFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/LongFunction.java Fri May 17 10:36:04 2013 -0700 @@ -43,5 +43,5 @@ * @param value the input value * @return the function result */ - public R apply(long value); + R apply(long value); }
--- a/jdk/src/share/classes/java/util/function/LongPredicate.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/LongPredicate.java Fri May 17 10:36:04 2013 -0700 @@ -39,11 +39,11 @@ /** * Returns {@code true} if the input value matches some criteria. * - * @param value the value to be tested. + * @param value the value to be tested * @return {@code true} if the input value matches some criteria, otherwise - * {@code false}. + * {@code false} */ - public boolean test(long value); + boolean test(long value); /** * Returns a predicate which evaluates to {@code true} only if this @@ -51,11 +51,15 @@ * this predicate returns {@code false} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ANDed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ANDed with this predicate * @return a new predicate which returns {@code true} only if both - * predicates return {@code true}. + * predicates return {@code true} */ - public default LongPredicate and(LongPredicate p) { + default LongPredicate and(LongPredicate p) { Objects.requireNonNull(p); return (value) -> test(value) && p.test(value); } @@ -64,9 +68,9 @@ * Returns a predicate which negates the result of this predicate. * * @return a new predicate who's result is always the opposite of this - * predicate. + * predicate */ - public default LongPredicate negate() { + default LongPredicate negate() { return (value) -> !test(value); } @@ -76,25 +80,17 @@ * predicate returns {@code true} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ORed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ORed with this predicate * @return a new predicate which returns {@code true} if either predicate - * returns {@code true}. + * returns {@code true} + * @throws NullPointerException if p is null */ - public default LongPredicate or(LongPredicate p) { + default LongPredicate or(LongPredicate p) { Objects.requireNonNull(p); return (value) -> test(value) || p.test(value); } - - /** - * Returns a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. - * - * @param p a predicate which will be logically-XORed with this predicate. - * @return a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. - */ - public default LongPredicate xor(LongPredicate p) { - Objects.requireNonNull(p); - return (value) -> test(value) ^ p.test(value); - } }
--- a/jdk/src/share/classes/java/util/function/LongSupplier.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/LongSupplier.java Fri May 17 10:36:04 2013 -0700 @@ -39,5 +39,5 @@ * * @return a {@code long} value */ - public long getAsLong(); + long getAsLong(); }
--- a/jdk/src/share/classes/java/util/function/LongUnaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/LongUnaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -24,6 +24,8 @@ */ package java.util.function; +import java.util.Objects; + /** * An operation on a single {@code long} operand yielding a {@code long} result. * This is the primitive type specialization of {@link UnaryOperator} for @@ -42,5 +44,46 @@ * @param operand the operand value * @return the operation result value */ - public long applyAsLong(long operand); + long applyAsLong(long operand); + + /** + * Compose a new function which applies the provided function followed by + * this function. If either function throws an exception, it is relayed + * to the caller. + * + * @param before An additional function to be applied before this function + * is applied + * @return A function which performs the provided function followed by this + * function + * @throws NullPointerException if before is null + */ + default LongUnaryOperator compose(LongUnaryOperator before) { + Objects.requireNonNull(before); + return (long v) -> applyAsLong(before.applyAsLong(v)); + } + + /** + * Compose a new function which applies this function followed by the + * provided function. If either function throws an exception, it is relayed + * to the caller. + * + * @param after An additional function to be applied after this function is + * applied + * @return A function which performs this function followed by the provided + * function followed + * @throws NullPointerException if after is null + */ + default LongUnaryOperator andThen(LongUnaryOperator after) { + Objects.requireNonNull(after); + return (long t) -> after.applyAsLong(applyAsLong(t)); + } + + /** + * Returns a unary operator that provides its input value as the result. + * + * @return a unary operator that provides its input value as the result + */ + static LongUnaryOperator identity() { + return t -> t; + } }
--- a/jdk/src/share/classes/java/util/function/ObjDoubleConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ObjDoubleConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -44,5 +44,5 @@ * @param t an input object * @param value an input value */ - public void accept(T t, double value); + void accept(T t, double value); }
--- a/jdk/src/share/classes/java/util/function/ObjIntConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ObjIntConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -30,7 +30,7 @@ * {@link BiConsumer}. Unlike most other functional interfaces, * {@code ObjIntConsumer} is expected to operate via side-effects. * - * @param <T> Type of reference argument to {@code accept()}. + * @param <T> Type of reference argument to {@code accept()} * * @see BiConsumer * @since 1.8 @@ -44,5 +44,5 @@ * @param t an input object * @param value an input value */ - public void accept(T t, int value); + void accept(T t, int value); }
--- a/jdk/src/share/classes/java/util/function/ObjLongConsumer.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ObjLongConsumer.java Fri May 17 10:36:04 2013 -0700 @@ -30,7 +30,7 @@ * {@link BiConsumer}. Unlike most other functional interfaces, * {@code ObjLongConsumer} is expected to operate via side-effects. * - * @param <T> Type of reference argument to {@code accept()}. + * @param <T> Type of reference argument to {@code accept()} * * @see BiConsumer * @since 1.8 @@ -44,5 +44,5 @@ * @param t an input object * @param value an input value */ - public void accept(T t, long value); + void accept(T t, long value); }
--- a/jdk/src/share/classes/java/util/function/Predicate.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/Predicate.java Fri May 17 10:36:04 2013 -0700 @@ -43,7 +43,7 @@ * @return {@code true} if the input object matches some criteria, otherwise * {@code false} */ - public boolean test(T t); + boolean test(T t); /** * Returns a predicate which evaluates to {@code true} only if this @@ -51,11 +51,16 @@ * this predicate returns {@code false} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ANDed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ANDed with this predicate * @return a new predicate which returns {@code true} only if both - * predicates return {@code true}. + * predicates return {@code true} + * @throws NullPointerException if p is null */ - public default Predicate<T> and(Predicate<? super T> p) { + default Predicate<T> and(Predicate<? super T> p) { Objects.requireNonNull(p); return (t) -> test(t) && p.test(t); } @@ -66,7 +71,7 @@ * @return a new predicate who's result is always the opposite of this * predicate. */ - public default Predicate<T> negate() { + default Predicate<T> negate() { return (t) -> !test(t); } @@ -76,25 +81,32 @@ * predicate returns {@code true} then the remaining predicate is not * evaluated. * - * @param p a predicate which will be logically-ORed with this predicate. + * <p>Any exceptions thrown by either {@code test} method are relayed + * to the caller; if performing first operation throws an exception, the + * second operation will not be performed. + * + * @param p a predicate which will be logically-ORed with this predicate * @return a new predicate which returns {@code true} if either predicate - * returns {@code true}. + * returns {@code true} + * @throws NullPointerException if p is null */ - public default Predicate<T> or(Predicate<? super T> p) { + default Predicate<T> or(Predicate<? super T> p) { Objects.requireNonNull(p); return (t) -> test(t) || p.test(t); } /** - * Returns a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. + * Returns a predicate who's result matches + * {@code Objects.equals(target, t)}. * - * @param p a predicate which will be logically-XORed with this predicte. - * @return a predicate that evaluates to {@code true} if both or neither of - * the component predicates evaluate to {@code true}. + * @param <T> the type of values evaluated by the predicate + * @param target the target value to be compared for equality + * @return a predicate who's result matches + * {@code Objects.equals(target, t)} */ - public default Predicate<T> xor(Predicate<? super T> p) { - Objects.requireNonNull(p); - return (t) -> test(t) ^ p.test(t); + static <T> Predicate<T> isEqual(Object target) { + return (null == target) + ? Objects::isNull + : object -> target.equals(object); } }
--- a/jdk/src/share/classes/java/util/function/Supplier.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/Supplier.java Fri May 17 10:36:04 2013 -0700 @@ -40,5 +40,5 @@ * * @return an object */ - public T get(); + T get(); }
--- a/jdk/src/share/classes/java/util/function/ToDoubleBiFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ToDoubleBiFunction.java Fri May 17 10:36:04 2013 -0700 @@ -46,5 +46,5 @@ * @param u an input object * @return the function result value */ - public double applyAsDouble(T t, U u); + double applyAsDouble(T t, U u); }
--- a/jdk/src/share/classes/java/util/function/ToDoubleFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ToDoubleFunction.java Fri May 17 10:36:04 2013 -0700 @@ -42,5 +42,5 @@ * @param t the input object * @return the function result value */ - public double applyAsDouble(T t); + double applyAsDouble(T t); }
--- a/jdk/src/share/classes/java/util/function/ToIntBiFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ToIntBiFunction.java Fri May 17 10:36:04 2013 -0700 @@ -28,10 +28,10 @@ * Apply a function to the input arguments, yielding an appropriate result. * This is the {@code int}-bearing specialization for {@link BiFunction}. * - * @param <T> the type of the first argument to the {@code applyAsLong} - * operation. - * @param <U> the type of the second argument to the {@code applyAsLong} - * operation. + * @param <T> the type of the first argument to the {@code applyAsInt} + * operation + * @param <U> the type of the second argument to the {@code applyAsInt} + * operation * * @see BiFunction * @since 1.8 @@ -46,5 +46,5 @@ * @param u an input object * @return the function result value */ - public int applyAsInt(T t, U u); + int applyAsInt(T t, U u); }
--- a/jdk/src/share/classes/java/util/function/ToIntFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ToIntFunction.java Fri May 17 10:36:04 2013 -0700 @@ -42,5 +42,5 @@ * @param t the input object * @return the function result value */ - public int applyAsInt(T t); + int applyAsInt(T t); }
--- a/jdk/src/share/classes/java/util/function/ToLongBiFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ToLongBiFunction.java Fri May 17 10:36:04 2013 -0700 @@ -46,5 +46,5 @@ * @param u an input object * @return the function result value */ - public long applyAsLong(T t, U u); + long applyAsLong(T t, U u); }
--- a/jdk/src/share/classes/java/util/function/ToLongFunction.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/ToLongFunction.java Fri May 17 10:36:04 2013 -0700 @@ -42,5 +42,5 @@ * @param t the input object * @return the function result value */ - public long applyAsLong(T t); + long applyAsLong(T t); }
--- a/jdk/src/share/classes/java/util/function/UnaryOperator.java Wed May 15 10:25:59 2013 +0200 +++ b/jdk/src/share/classes/java/util/function/UnaryOperator.java Fri May 17 10:36:04 2013 -0700 @@ -36,4 +36,13 @@ */ @FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> { + + /** + * Returns a unary operator that provides its input value as the result. + * + * @return a unary operator that provides its input value as the result + */ + static <T> UnaryOperator<T> identity() { + return t -> t; + } }