jack-zheng
12/12/2018 - 11:14 AM

apache, validator, source, todo

apache, validator, source, todo

Reading Source Code of Commons-Validator

This post will log the reading tips of Commons-validator lib. it will be a breif record, and about the detail points, they will logged in other posts.

RegexValidator

主要用于正则表达式的验证, 可以用来做匹配验证, 提取匹配结果等常规操作。为了读懂这个 class 你必须要有一定的正则表达式的基础, 至少要到了解goup 的程度,不然 50% 的 methods 你都会看不懂。另外就是需要对 Java 的 regex, Pattern, Matcher的基本使用了解就可以了。这个 class 中,我学到了, 在处理非法输入时, 你可以通过抛出一个 IllegalArgumentException 来实现。

    public RegexValidator(String[] regexs, boolean caseSensitive) {
        if (regexs == null || regexs.length == 0) {
            throw new IllegalArgumentException("Regular expressions are missing");
        }
        ...
    }

InetAddressValidator

static 修饰的变量名用大写!!! 这个 class 中两点是对一些变量的修饰符, 一些常亮都用 private static final 修饰, 对变量的使用做了控制。
还有一个是在 class 中使用了简易的单例模式:

private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
public static InetAddressValidator getInstance() {
        return VALIDATOR;
    }

以上的申明都是在定义阶段完成的, 作为扩展阅读可以复习下各种单例模式的区别, 基本上已经忘得差不多了。。。。

EmailValidator 中 Constructor 的 super() 有什么用意?

他的除了实现了序列化接口之外没有继承其他父类, 感觉这个super()是不是可以不写啊?