Predicate.java官方文档翻译

package java.util.function;

import java.util.Objects;

/**
* Represents a predicate (boolean-valued function) of one argument.
* 表示一个参数的谓词(booean值函数)
*
* <p>这是一个 <a href="package-summary.html">函数式接口</a>
* 它的函数式方法是 {@link #test(Object)}.
*
* @param <T> the type of the input to the predicate
*
* @since 1.8
*/
@FunctionalInterface
public interface Predicate<T> {

    /**
     * 在给定的参数上计算这个predicate
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

    /**
     * 返回一个predicate,它表示一个predicate与另一个predicate 短路的逻辑与。
     *评估组合的predicate时,如果第一个predicate是{@code false},
     *那么第二个 {@code other}不会执行。
     *
     *
     * 在评估任一predicate时抛出的任何异常都传递给调用者,如果
     * 对该predicate的评估抛出异常,则不会对其他的 {@code other} predicate进行评估。
     *
     * @param other a predicate that will be logically-ANDed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * AND of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    /**
     * 返回一个predicate。表示这个predicate的逻辑非
     *
     * @return a predicate that represents the logical negation of this
     * predicate
     */
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    /**
       * 返回一个predicate,它表示一个predicate与另一个predicate 短路的逻辑或。
     *评估组合的predicate时,如果第一个predicate是{@code false},
     *那么第二个 {@code other}不会执行。
     *
     *
     * 在评估任一predicate时抛出的任何异常都传递给调用者,如果
     * 对该predicate的评估抛出异常,则不会对其他的 {@code other} predicate进行评估。
     *
     * @param other a predicate that will be logically-ORed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * OR of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    /**
     * 返回一个predicate测试两个参数是否相等通过
     *  {@link Objects#equals(Object, Object)}.
     *
     * @param <T> the type of arguments to the predicate
     * @param targetRef the object reference with which to compare for equality,
     *               which may be {@code null}
     * @return a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}
     */
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

猜你喜欢

转载自linkinzlz.iteye.com/blog/2397366