您当前的位置: 首页 > 

Predicate

发布时间:2022-01-19 14:36:45 ,浏览量:13

序言

此断言非彼断言.目前来看主要用于校验.同时java.util.function下有很多类型的Predicate,触类旁通其实这个没什么特别的,期主要目的是为了统一校验的使用.cuiyaonan2000@163.com

API
package java.util.function;

import java.util.Objects;

/**
 * Represents a predicate (boolean-valued function) of one argument.
 *
 *
		

This is a functional interface * whose functional method is {@link #test(Object)}. * * @paramthe type of the input to the predicate * * @since 1.8 */ @FunctionalInterface public interface Predicate{ /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); /** * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code false}, then the {@code other} * predicate is not evaluated. * *

Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @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 Predicateand(Predicate other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } /** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */ default Predicatenegate() { return (t) -> !test(t); } /** * Returns a composed predicate that represents a short-circuiting logical * OR of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code true}, then the {@code other} * predicate is not evaluated. * *

Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @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 Predicateor(Predicate other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } /** * Returns a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)}. * * @paramthe 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)} */ staticPredicateisEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : object -> targetRef.equals(object); } }

如上看到的API其实方法比较少,我们只需要实现该接口对应的方法就行了.

我们使用lambda表达式创建一个实例,然后可以增加一些常用的逻辑表达式,来增加业务要求的复杂性.cuiyaonan2000@163.com

常用的表达式如下图所示:

他们的作用如下:

  1. test() :传入值判断是否满足我们的条件
  2. or()  :或运算 即或者满足or()里的逻辑就返回true
  3. and() :与运算,即也同时要满足and()里的逻辑才能返回true
  4. negate(): 取反,即原本是true的,则返回false
  5. equals():比较运算符,特别说明该方法是一个静态方法cuiyaonan2000@163.com

用例
package cui.yao.nan.predicate;

import java.util.function.Predicate;

/**
 * @Author: cuiyaonan2000@163.com
 * @Description: todo
 * @Date: Created at 2022-1-19  14:13
 */
public class Test {

    public static void main(String[] args){

        //类型必须一样入参的类型必须一样.即每次只能针对一个数据进行监察
        Predicatepredicate_1 = a -> a.length()>2;

        System.out.println(predicate_1.test("1333"));//true
        System.out.println(predicate_1.negate().test("1333")); //false

        System.out.println(predicate_1.and(a -> a.startsWith("s")).test("ssssssss")); //true

        System.out.println(predicate_1.or(a -> a.startsWith("s")).test("123123123")) ;//true

        System.out.println(Predicate.isEqual("1").test("2"));//false
    }
}
关注
打赏
1688896170
查看更多评论

暂无认证

  • 13浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.4652s