Java系列之Predicate

Java8引入了许多函数式接口(Functional Interface),Predicate(断言)就是其中一个,它的主要作用可以简单描述为:向其传入一个对象(可以理解为参数),将得到一个布尔值作为输出。

接口源码

Predicate接口的源码非常简单,如下所示,重点内容有:

  1. 所有的函数式接口都带有@FunctionalInterface标明其身份;
  2. test()方法用来处理输入的参数t,计算得到输出结果为true还是false;
  3. and(), negate(), or()函数功能类似与或非,能够进行串联;
@FunctionalInterface
public interface Predicate<T> {
    
    
    /**
     * 具体过滤操作 需要被子类实现.
     * 用来处理参数T是否满足要求,可以理解为 条件A
     */
    boolean test(T t);
    /**
     * 调用当前Predicate的test方法之后再去调用other的test方法,相当于进行两次判断
     * 可理解为 条件A && 条件B
     */
    default Predicate<T> and(Predicate<? super T> other) {
    
    
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }
    /**
     * 对当前判断进行"!"操作,即取非操作,可理解为 ! 条件A
     */
    default Predicate<T> negate() {
    
    
        return (t) -> !test(t);
    }
    /**
     * 对当前判断进行"||"操作,即取或操作,可以理解为 条件A ||条件B
     */
    default Predicate<T> or(Predicate<? super T> other) {
    
    
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }
 
    /**
     * 对当前操作进行"="操作,即取等操作,可以理解为 A == B
     */
    static <T> Predicate<T> isEqual(Object targetRef) {
    
    
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

使用举例

看了上面的接口源码,你可能还是一知半解,这是一个接口,到底要怎么使用呢?看如下例子:

// 将实现用户名校验规则的lambda函数赋给Predicate实例
Predicate<String> isUserNameValid = u -> u != null && u.length() > 5 && u.length() < 10;
// 调用test方法,test方法的参数就是待检测的字符串(也就是喂给Predicate实例的数据),将会输出true
System.out.println(isUserNameValid.test("Howard"));

// 同样的道理
Predicate<String> isPasswordValid = p -> p != null && p.length() > 8 && p.length() < 15;
System.out.println(isPasswordValid.test("123456789"));

另外,在Java8的stream里面也有很多Predicate的应用,如filter函数的参数就是Predicate:

Arrays.asList("A1234567890", "Howard", "Tommy", "Jone").stream()
     .filter(str -> str.length() > 5)
     .filter(str -> str.length() < 10)
     .collect(Collectors.toList());

函数文档为:

java.util.stream.Stream<T>
public abstract Stream<T> filter(java.util.function.Predicate<? super T> predicate)

进阶举例

除了像上面这样定义单个的Predicate,也可以利用其and()等函数,将多个语句串联起来:

Predicate<String> predicate1 = str -> str.startsWith("A");
Predicate<String> predicate2 = str -> str.length() > 5;
Predicate<String> predicate3 = str -> str.length() < 10;
predicate1.and(predicate2).and(predicate3).test("A12347");  // true

除此之外,还可以对Predicate进行封装,构建Fluent API风格的校验器:

public class Validator<T> {
    
    
    private Predicate<T> predicate;

    public Validator() {
    
    
    	// 将predicate初始化为true
        this.predicate = t -> true;
    }
	
	// 添加校验策略,由于该函数返回this,因此可以无限使用and串联
    public Validator<T> with(Predicate<T> predicate) {
    
    
        this.predicate = this.predicate.and(predicate);
        return this;
    }

	// 执行校验,并返回校验结果
    public boolean validate(T t) {
    
    
        return this.predicate.test(t);
    }
}

public static void main(String[] args) {
    
    
    boolean result = new Validator<String>()
        .with(u -> u != null)
        .with(u -> u.length() > 5)
        .with(u -> u.length() < 10)
        .validate("Howard");

    System.out.println(result); // true
}

猜你喜欢

转载自blog.csdn.net/qq_26822029/article/details/129069524