提交 a875642a 编写于 作者: M mduigou

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
上级 cc50b323
...@@ -24,14 +24,16 @@ ...@@ -24,14 +24,16 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation which accepts two input arguments and returns no result. This is * An operation which accepts two input arguments and returns no result. This is
* the two-arity specialization of {@link Consumer}. Unlike most other * the two-arity specialization of {@link Consumer}. Unlike most other
* functional interfaces, {@code BiConsumer} is expected to operate via * functional interfaces, {@code BiConsumer} is expected to operate via
* side-effects. * side-effects.
* *
* @param <T> the type of the first 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. * @param <U> the type of the second argument to the {@code accept} operation
* *
* @see Consumer * @see Consumer
* @since 1.8 * @since 1.8
...@@ -47,4 +49,28 @@ public interface BiConsumer<T, U> { ...@@ -47,4 +49,28 @@ public interface BiConsumer<T, U> {
* @param u an input object * @param u an input object
*/ */
void accept(T t, U u); 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);
};
}
} }
...@@ -24,15 +24,17 @@ ...@@ -24,15 +24,17 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* Apply a function to the input arguments, yielding an appropriate result. This * Apply a function to the input arguments, yielding an appropriate result. This
* is the two-arity specialization of {@link Function}. A function may * is the two-arity specialization of {@link Function}. A function may
* variously provide a mapping between types, object instances or keys and * variously provide a mapping between types, object instances or keys and
* values or any other form of transformation upon the input. * values or any other form of transformation upon the input.
* *
* @param <T> the type of the first argument to 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 <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 <R> the type of results returned by the {@code apply} operation
* *
* @see Function * @see Function
* @since 1.8 * @since 1.8
...@@ -48,4 +50,22 @@ public interface BiFunction<T, U, R> { ...@@ -48,4 +50,22 @@ public interface BiFunction<T, U, R> {
* @return the function result * @return the function result
*/ */
R apply(T t, U u); 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));
}
} }
...@@ -30,8 +30,8 @@ import java.util.Objects; ...@@ -30,8 +30,8 @@ import java.util.Objects;
* Determines if the input objects match some criteria. This is the two-arity * Determines if the input objects match some criteria. This is the two-arity
* specialization of {@link Predicate}. * specialization of {@link Predicate}.
* *
* @param <T> the type of the first 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}. * @param <U> the type of the second argument to {@code test}
* *
* @see Predicate * @see Predicate
* @since 1.8 * @since 1.8
...@@ -42,9 +42,9 @@ public interface BiPredicate<T, U> { ...@@ -42,9 +42,9 @@ public interface BiPredicate<T, U> {
/** /**
* Return {@code true} if the inputs match some criteria. * Return {@code true} if the inputs match some criteria.
* *
* @param t an input object. * @param t an input object
* @param u an input object. * @param u an input object
* @return {@code true} if the inputs match some criteria. * @return {@code true} if the inputs match some criteria
*/ */
boolean test(T t, U u); boolean test(T t, U u);
...@@ -54,11 +54,12 @@ public interface BiPredicate<T, U> { ...@@ -54,11 +54,12 @@ public interface BiPredicate<T, U> {
* this predicate returns {@code false} then the remaining predicate is not * this predicate returns {@code false} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (T t, U u) -> test(t, u) && p.test(t, u); return (T t, U u) -> test(t, u) && p.test(t, u);
} }
...@@ -67,9 +68,9 @@ public interface BiPredicate<T, U> { ...@@ -67,9 +68,9 @@ public interface BiPredicate<T, U> {
* Returns a predicate which negates the result of this predicate. * Returns a predicate which negates the result of this predicate.
* *
* @return a new predicate who's result is always the opposite of this * @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); return (T t, U u) -> !test(t, u);
} }
...@@ -79,25 +80,13 @@ public interface BiPredicate<T, U> { ...@@ -79,25 +80,13 @@ public interface BiPredicate<T, U> {
* predicate returns {@code true} then the remaining predicate is not * predicate returns {@code true} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (T t, U u) -> test(t, u) || p.test(t, u); 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);
}
} }
...@@ -40,5 +40,5 @@ public interface BooleanSupplier { ...@@ -40,5 +40,5 @@ public interface BooleanSupplier {
* *
* @return a {@code boolean} value * @return a {@code boolean} value
*/ */
public boolean getAsBoolean(); boolean getAsBoolean();
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation which accepts a single input argument and returns no result. * An operation which accepts a single input argument and returns no result.
* Unlike most other functional interfaces, {@code Consumer} is expected to * Unlike most other functional interfaces, {@code Consumer} is expected to
...@@ -41,5 +43,25 @@ public interface Consumer<T> { ...@@ -41,5 +43,25 @@ public interface Consumer<T> {
* *
* @param t the input object * @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); };
}
} }
...@@ -43,5 +43,5 @@ public interface DoubleBinaryOperator { ...@@ -43,5 +43,5 @@ public interface DoubleBinaryOperator {
* @param right the right operand value * @param right the right operand value
* @return the result of the operation * @return the result of the operation
*/ */
public double applyAsDouble(double left, double right); double applyAsDouble(double left, double right);
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation which accepts a single double argument and returns no result. * An operation which accepts a single double argument and returns no result.
* This is the primitive type specialization of {@link Consumer} for * This is the primitive type specialization of {@link Consumer} for
...@@ -41,5 +43,26 @@ public interface DoubleConsumer { ...@@ -41,5 +43,26 @@ public interface DoubleConsumer {
* *
* @param value the input value * @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); };
}
} }
...@@ -43,5 +43,5 @@ public interface DoubleFunction<R> { ...@@ -43,5 +43,5 @@ public interface DoubleFunction<R> {
* @param value the input value * @param value the input value
* @return the function result * @return the function result
*/ */
public R apply(double value); R apply(double value);
} }
...@@ -40,11 +40,11 @@ public interface DoublePredicate { ...@@ -40,11 +40,11 @@ public interface DoublePredicate {
/** /**
* Returns {@code true} if the input value matches some criteria. * 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 * @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 * Returns a predicate which evaluates to {@code true} only if this
...@@ -52,11 +52,16 @@ public interface DoublePredicate { ...@@ -52,11 +52,16 @@ public interface DoublePredicate {
* this predicate returns {@code false} then the remaining predicate is not * this predicate returns {@code false} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (value) -> test(value) && p.test(value); return (value) -> test(value) && p.test(value);
} }
...@@ -65,9 +70,9 @@ public interface DoublePredicate { ...@@ -65,9 +70,9 @@ public interface DoublePredicate {
* Returns a predicate which negates the result of this predicate. * Returns a predicate which negates the result of this predicate.
* *
* @return a new predicate who's result is always the opposite of this * @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); return (value) -> !test(value);
} }
...@@ -77,25 +82,17 @@ public interface DoublePredicate { ...@@ -77,25 +82,17 @@ public interface DoublePredicate {
* predicate returns {@code true} then the remaining predicate is not * predicate returns {@code true} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (value) -> test(value) || p.test(value); 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);
}
} }
...@@ -39,5 +39,5 @@ public interface DoubleSupplier { ...@@ -39,5 +39,5 @@ public interface DoubleSupplier {
* *
* @return a {@code double} value * @return a {@code double} value
*/ */
public double getAsDouble(); double getAsDouble();
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation on a {@code double} operand yielding a {@code double} * An operation on a {@code double} operand yielding a {@code double}
* result. This is the primitive type specialization of {@link UnaryOperator} * result. This is the primitive type specialization of {@link UnaryOperator}
...@@ -42,5 +44,46 @@ public interface DoubleUnaryOperator { ...@@ -42,5 +44,46 @@ public interface DoubleUnaryOperator {
* @param operand the operand value * @param operand the operand value
* @return the operation result 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;
}
} }
...@@ -24,14 +24,15 @@ ...@@ -24,14 +24,15 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* Apply a function to the input argument, yielding an appropriate result. A * Apply a function to the input argument, yielding an appropriate result. A
* function may variously provide a mapping between types, object instances or * function may variously provide a mapping between types, object instances or
* keys and values or any other form of transformation upon the input. * 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 <T> the type of the input to the {@code apply} operation
* @param <R> the type of the result of the {@code apply} operation. * @param <R> the type of the result of the {@code apply} operation
* *
* @since 1.8 * @since 1.8
*/ */
...@@ -44,5 +45,50 @@ public interface Function<T, R> { ...@@ -44,5 +45,50 @@ public interface Function<T, R> {
* @param t the input object * @param t the input object
* @return the function result * @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;
}
} }
...@@ -44,5 +44,5 @@ public interface IntBinaryOperator { ...@@ -44,5 +44,5 @@ public interface IntBinaryOperator {
* @param right the right operand value * @param right the right operand value
* @return the result of the operation * @return the result of the operation
*/ */
public int applyAsInt(int left, int right); int applyAsInt(int left, int right);
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation which accepts a single integer argument and returns no result. * An operation which accepts a single integer argument and returns no result.
* This is the primitive type specialization of {@link Consumer} for {@code int}. * This is the primitive type specialization of {@link Consumer} for {@code int}.
...@@ -41,5 +43,26 @@ public interface IntConsumer { ...@@ -41,5 +43,26 @@ public interface IntConsumer {
* *
* @param value the input value * @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); };
}
} }
...@@ -43,5 +43,5 @@ public interface IntFunction<R> { ...@@ -43,5 +43,5 @@ public interface IntFunction<R> {
* @param value the input value * @param value the input value
* @return the function result * @return the function result
*/ */
public R apply(int value); R apply(int value);
} }
...@@ -39,11 +39,11 @@ public interface IntPredicate { ...@@ -39,11 +39,11 @@ public interface IntPredicate {
/** /**
* Returns {@code true} if the input value matches some criteria. * 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 * @return {@code true} if the input value matches some criteria, otherwise
* {@code false} * {@code false}
*/ */
public boolean test(int value); boolean test(int value);
/** /**
* Returns a predicate which evaluates to {@code true} only if this * Returns a predicate which evaluates to {@code true} only if this
...@@ -51,11 +51,16 @@ public interface IntPredicate { ...@@ -51,11 +51,16 @@ public interface IntPredicate {
* this predicate returns {@code false} then the remaining predicate is not * this predicate returns {@code false} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (value) -> test(value) && p.test(value); return (value) -> test(value) && p.test(value);
} }
...@@ -64,9 +69,9 @@ public interface IntPredicate { ...@@ -64,9 +69,9 @@ public interface IntPredicate {
* Returns a predicate which negates the result of this predicate. * Returns a predicate which negates the result of this predicate.
* *
* @return a new predicate who's result is always the opposite of this * @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); return (value) -> !test(value);
} }
...@@ -76,25 +81,17 @@ public interface IntPredicate { ...@@ -76,25 +81,17 @@ public interface IntPredicate {
* predicate returns {@code true} then the remaining predicate is not * predicate returns {@code true} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (value) -> test(value) || p.test(value); 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);
}
} }
...@@ -39,5 +39,5 @@ public interface IntSupplier { ...@@ -39,5 +39,5 @@ public interface IntSupplier {
* *
* @return an {@code int} value * @return an {@code int} value
*/ */
public int getAsInt(); int getAsInt();
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation on a single {@code int} operand yielding an {@code int} result. * An operation on a single {@code int} operand yielding an {@code int} result.
* This is the primitive type specialization of {@link UnaryOperator} for * This is the primitive type specialization of {@link UnaryOperator} for
...@@ -37,10 +39,51 @@ public interface IntUnaryOperator { ...@@ -37,10 +39,51 @@ public interface IntUnaryOperator {
/** /**
* Returns the {@code int} value result of the operation upon the * Returns the {@code int} value result of the operation upon the
* {@code int} operand. * {@code int} operand.
* *
* @param operand the operand value * @param operand the operand value
* @return the operation result 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;
}
} }
...@@ -44,5 +44,5 @@ public interface LongBinaryOperator { ...@@ -44,5 +44,5 @@ public interface LongBinaryOperator {
* @param right the right operand value * @param right the right operand value
* @return the result of the operation * @return the result of the operation
*/ */
public long applyAsLong(long left, long right); long applyAsLong(long left, long right);
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation which accepts a single long argument and returns no result. * An operation which accepts a single long argument and returns no result.
* This is the {@code long}-consuming primitive type specialization of * This is the {@code long}-consuming primitive type specialization of
...@@ -41,5 +43,26 @@ public interface LongConsumer { ...@@ -41,5 +43,26 @@ public interface LongConsumer {
* *
* @param value the input value * @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); };
}
} }
...@@ -43,5 +43,5 @@ public interface LongFunction<R> { ...@@ -43,5 +43,5 @@ public interface LongFunction<R> {
* @param value the input value * @param value the input value
* @return the function result * @return the function result
*/ */
public R apply(long value); R apply(long value);
} }
...@@ -39,11 +39,11 @@ public interface LongPredicate { ...@@ -39,11 +39,11 @@ public interface LongPredicate {
/** /**
* Returns {@code true} if the input value matches some criteria. * 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 * @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 * Returns a predicate which evaluates to {@code true} only if this
...@@ -51,11 +51,15 @@ public interface LongPredicate { ...@@ -51,11 +51,15 @@ public interface LongPredicate {
* this predicate returns {@code false} then the remaining predicate is not * this predicate returns {@code false} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (value) -> test(value) && p.test(value); return (value) -> test(value) && p.test(value);
} }
...@@ -64,9 +68,9 @@ public interface LongPredicate { ...@@ -64,9 +68,9 @@ public interface LongPredicate {
* Returns a predicate which negates the result of this predicate. * Returns a predicate which negates the result of this predicate.
* *
* @return a new predicate who's result is always the opposite of this * @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); return (value) -> !test(value);
} }
...@@ -76,25 +80,17 @@ public interface LongPredicate { ...@@ -76,25 +80,17 @@ public interface LongPredicate {
* predicate returns {@code true} then the remaining predicate is not * predicate returns {@code true} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (value) -> test(value) || p.test(value); 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);
}
} }
...@@ -39,5 +39,5 @@ public interface LongSupplier { ...@@ -39,5 +39,5 @@ public interface LongSupplier {
* *
* @return a {@code long} value * @return a {@code long} value
*/ */
public long getAsLong(); long getAsLong();
} }
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
*/ */
package java.util.function; package java.util.function;
import java.util.Objects;
/** /**
* An operation on a single {@code long} operand yielding a {@code long} result. * An operation on a single {@code long} operand yielding a {@code long} result.
* This is the primitive type specialization of {@link UnaryOperator} for * This is the primitive type specialization of {@link UnaryOperator} for
...@@ -42,5 +44,46 @@ public interface LongUnaryOperator { ...@@ -42,5 +44,46 @@ public interface LongUnaryOperator {
* @param operand the operand value * @param operand the operand value
* @return the operation result 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;
}
} }
...@@ -44,5 +44,5 @@ public interface ObjDoubleConsumer<T> { ...@@ -44,5 +44,5 @@ public interface ObjDoubleConsumer<T> {
* @param t an input object * @param t an input object
* @param value an input value * @param value an input value
*/ */
public void accept(T t, double value); void accept(T t, double value);
} }
...@@ -30,7 +30,7 @@ package java.util.function; ...@@ -30,7 +30,7 @@ package java.util.function;
* {@link BiConsumer}. Unlike most other functional interfaces, * {@link BiConsumer}. Unlike most other functional interfaces,
* {@code ObjIntConsumer} is expected to operate via side-effects. * {@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 * @see BiConsumer
* @since 1.8 * @since 1.8
...@@ -44,5 +44,5 @@ public interface ObjIntConsumer<T> { ...@@ -44,5 +44,5 @@ public interface ObjIntConsumer<T> {
* @param t an input object * @param t an input object
* @param value an input value * @param value an input value
*/ */
public void accept(T t, int value); void accept(T t, int value);
} }
...@@ -30,7 +30,7 @@ package java.util.function; ...@@ -30,7 +30,7 @@ package java.util.function;
* {@link BiConsumer}. Unlike most other functional interfaces, * {@link BiConsumer}. Unlike most other functional interfaces,
* {@code ObjLongConsumer} is expected to operate via side-effects. * {@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 * @see BiConsumer
* @since 1.8 * @since 1.8
...@@ -44,5 +44,5 @@ public interface ObjLongConsumer<T> { ...@@ -44,5 +44,5 @@ public interface ObjLongConsumer<T> {
* @param t an input object * @param t an input object
* @param value an input value * @param value an input value
*/ */
public void accept(T t, long value); void accept(T t, long value);
} }
...@@ -43,7 +43,7 @@ public interface Predicate<T> { ...@@ -43,7 +43,7 @@ public interface Predicate<T> {
* @return {@code true} if the input object matches some criteria, otherwise * @return {@code true} if the input object matches some criteria, otherwise
* {@code false} * {@code false}
*/ */
public boolean test(T t); boolean test(T t);
/** /**
* Returns a predicate which evaluates to {@code true} only if this * Returns a predicate which evaluates to {@code true} only if this
...@@ -51,11 +51,16 @@ public interface Predicate<T> { ...@@ -51,11 +51,16 @@ public interface Predicate<T> {
* this predicate returns {@code false} then the remaining predicate is not * this predicate returns {@code false} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (t) -> test(t) && p.test(t); return (t) -> test(t) && p.test(t);
} }
...@@ -66,7 +71,7 @@ public interface Predicate<T> { ...@@ -66,7 +71,7 @@ public interface Predicate<T> {
* @return a new predicate who's result is always the opposite of this * @return a new predicate who's result is always the opposite of this
* predicate. * predicate.
*/ */
public default Predicate<T> negate() { default Predicate<T> negate() {
return (t) -> !test(t); return (t) -> !test(t);
} }
...@@ -76,25 +81,32 @@ public interface Predicate<T> { ...@@ -76,25 +81,32 @@ public interface Predicate<T> {
* predicate returns {@code true} then the remaining predicate is not * predicate returns {@code true} then the remaining predicate is not
* evaluated. * 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 * @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); Objects.requireNonNull(p);
return (t) -> test(t) || p.test(t); return (t) -> test(t) || p.test(t);
} }
/** /**
* Returns a predicate that evaluates to {@code true} if both or neither of * Returns a predicate who's result matches
* the component predicates evaluate to {@code true}. * {@code Objects.equals(target, t)}.
* *
* @param p a predicate which will be logically-XORed with this predicte. * @param <T> the type of values evaluated by the predicate
* @return a predicate that evaluates to {@code true} if both or neither of * @param target the target value to be compared for equality
* the component predicates evaluate to {@code true}. * @return a predicate who's result matches
* {@code Objects.equals(target, t)}
*/ */
public default Predicate<T> xor(Predicate<? super T> p) { static <T> Predicate<T> isEqual(Object target) {
Objects.requireNonNull(p); return (null == target)
return (t) -> test(t) ^ p.test(t); ? Objects::isNull
: object -> target.equals(object);
} }
} }
...@@ -40,5 +40,5 @@ public interface Supplier<T> { ...@@ -40,5 +40,5 @@ public interface Supplier<T> {
* *
* @return an object * @return an object
*/ */
public T get(); T get();
} }
...@@ -46,5 +46,5 @@ public interface ToDoubleBiFunction<T, U> { ...@@ -46,5 +46,5 @@ public interface ToDoubleBiFunction<T, U> {
* @param u an input object * @param u an input object
* @return the function result value * @return the function result value
*/ */
public double applyAsDouble(T t, U u); double applyAsDouble(T t, U u);
} }
...@@ -42,5 +42,5 @@ public interface ToDoubleFunction<T> { ...@@ -42,5 +42,5 @@ public interface ToDoubleFunction<T> {
* @param t the input object * @param t the input object
* @return the function result value * @return the function result value
*/ */
public double applyAsDouble(T t); double applyAsDouble(T t);
} }
...@@ -28,10 +28,10 @@ package java.util.function; ...@@ -28,10 +28,10 @@ package java.util.function;
* Apply a function to the input arguments, yielding an appropriate result. * Apply a function to the input arguments, yielding an appropriate result.
* This is the {@code int}-bearing specialization for {@link BiFunction}. * This is the {@code int}-bearing specialization for {@link BiFunction}.
* *
* @param <T> the type of the first argument to the {@code applyAsLong} * @param <T> the type of the first argument to the {@code applyAsInt}
* operation. * operation
* @param <U> the type of the second argument to the {@code applyAsLong} * @param <U> the type of the second argument to the {@code applyAsInt}
* operation. * operation
* *
* @see BiFunction * @see BiFunction
* @since 1.8 * @since 1.8
...@@ -46,5 +46,5 @@ public interface ToIntBiFunction<T, U> { ...@@ -46,5 +46,5 @@ public interface ToIntBiFunction<T, U> {
* @param u an input object * @param u an input object
* @return the function result value * @return the function result value
*/ */
public int applyAsInt(T t, U u); int applyAsInt(T t, U u);
} }
...@@ -42,5 +42,5 @@ public interface ToIntFunction<T> { ...@@ -42,5 +42,5 @@ public interface ToIntFunction<T> {
* @param t the input object * @param t the input object
* @return the function result value * @return the function result value
*/ */
public int applyAsInt(T t); int applyAsInt(T t);
} }
...@@ -46,5 +46,5 @@ public interface ToLongBiFunction<T, U> { ...@@ -46,5 +46,5 @@ public interface ToLongBiFunction<T, U> {
* @param u an input object * @param u an input object
* @return the function result value * @return the function result value
*/ */
public long applyAsLong(T t, U u); long applyAsLong(T t, U u);
} }
...@@ -42,5 +42,5 @@ public interface ToLongFunction<T> { ...@@ -42,5 +42,5 @@ public interface ToLongFunction<T> {
* @param t the input object * @param t the input object
* @return the function result value * @return the function result value
*/ */
public long applyAsLong(T t); long applyAsLong(T t);
} }
...@@ -36,4 +36,13 @@ package java.util.function; ...@@ -36,4 +36,13 @@ package java.util.function;
*/ */
@FunctionalInterface @FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> { 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;
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册