Java 8谓词验证器实现

时间:2018-02-07 05:22:14

标签: java validation

我读了这篇文章(文章| https://dzone.com/articles/fail-fast-validations-using)关于Java验证器的实现。

import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

public class GenericValidator<T> implements Predicate {

  private final List<Predicate<T>> validators = new LinkedList<>();

  public GenericValidator(List<Predicate<T>> validators) {
    this.validators.addAll(validators);
  }

  @Override
  public boolean test(final T toValidate) {
    return validators.parallelStream()
                 .allMatch(predicate -> predicate.test(toValidate));
  }
}

这次调用不是为了测试递归调用吗?比方说,谓词.test在测试函数内部调用实现Predicate

1 个答案:

答案 0 :(得分:0)

这不是递归调用,因为在GenericValidator#test中你调用的是predicate.test(tovalidate),其中谓词是在GenericValidator的对象创建过程中通过构造函数传递的对象。所以,

predicate -> predicate.test(toValidate)

上面的语句将调用您作为构造函数参数传递的谓词的测试方法(您已经传递了谓词列表)。下面是您可以轻松测试的代码。

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

public class Practice {

    public static void main(String args[]) {
     List<Predicate<Integer>> vaList = new ArrayList<>();
     vaList.add(Practice::isGreaterThan5);
     vaList.add(Practice::isLessThan10);
     GenericValidator<Integer> genericValidator = new GenericValidator(vaList);
     genericValidator.test(8);
    }

    public static boolean isGreaterThan5(int e){
        System.out.println("Is greater than 5");
        return e>5;
    }

    public static boolean isLessThan10(int e){
        System.out.println("Is less than 10");
        return e<10;
    }

}


 class GenericValidator<T> implements Predicate<T> {

  private final List<Predicate<T>> validators = new LinkedList<>();

  public GenericValidator(List<Predicate<T>> validators) {
    this.validators.addAll(validators);
  }

  @Override
  public boolean test(final T toValidate) {
      System.out.println("Validating the input inside test....");
    return validators.parallelStream()
                 .allMatch(predicate -> predicate.test(toValidate));
  }
}

输出:

Validating the input inside test....
Is greater than 5
Is less than 10

所以你可以看到它“验证测试中的输入”只有一次打印。因此它不会是递归调用。

修改

我在这里试着回答你的评论。下面是通过实现Predicate接口的旧方法示例。但是,这样做非常沮丧。

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

public class Practice {

    public static void main(String args[]) {
        List<Predicate<Integer>> vaList = new ArrayList<>();
        vaList.add(new GeraterThan5Predicate());
        vaList.add(new LessThan10Predicate());
        GenericValidator<Integer> genericValidator = new GenericValidator(vaList);
        genericValidator.test(8);
    }
}

class GeraterThan5Predicate implements Predicate<Integer> {

    @Override
    public boolean test(Integer e) {
        System.out.println("Is greater than 5");
        return e > 5;
    }

}

class LessThan10Predicate implements Predicate<Integer> {

    @Override
    public boolean test(Integer e) {
        System.out.println("Is less than 10");
        return e > 10;
    }

}

class GenericValidator<T> implements Predicate<T> {

    private final List<Predicate<T>> validators = new LinkedList<>();

    public GenericValidator(List<Predicate<T>> validators) {
        this.validators.addAll(validators);
    }

    @Override
    public boolean test(final T toValidate) {
        System.out.println("Validating the input inside test....");
        return validators.parallelStream().allMatch(predicate -> predicate.test(toValidate));
    }
}

为什么你需要通过GenericValidator实现Predicate接口。我无法回答这个问题。如果您编写如下所示的GenericValidator类仍然有效。

class GenericValidator<T>{

    private final List<Predicate<T>> validators = new LinkedList<>();

    public GenericValidator(List<Predicate<T>> validators) {
        this.validators.addAll(validators);
    }

    public boolean test(final T toValidate) {
        System.out.println("Validating the input inside test....");
        return validators.parallelStream().allMatch(predicate -> predicate.test(toValidate));
    }
}