Commons Collections - Predicate组

来源:互联网 发布:网络节目审核通则 编辑:程序博客网 时间:2024/05/21 10:55

转自http://blog.sina.com.cn/s/blog_5ca9fdd80100b4o4.html
接下来看Predicate组

Predicate
AndPredicate
OrPredicate
AllPredicate
OnePredicate
NonePredicate
PredicateUtils

Predicate是Commons Collections中定义的一个接口,可以在org.apache.commons.collections包中找到。其中定义的方法签名如下:

public boolean evaluate(Object object)

它以一个Object对象为参数,处理后返回一个boolean值,检验某个对象是否满足某个条件。其实这个Predicate以及上一篇笔记提到的Comparator还有我们即将看到的Transformer和Closure等都有些类似C/C++中的函数指针,它们都只是提供简单而明确定义的函数功能而已。

跟其他组类似,Commons Collections也提供了一组定义好的Predicate类供我们使用,这些类都放在org.apache.commons.collections.functors包中。当然,我们也可以自定义Predicate,只要实现这个Predicate接口即可。在Commons Collections中我们也可以很方便使用的一组预定义复合Predicate,我们提供2个或不定数量个Predicate,然后交给它,它可以帮我们处理额外的逻辑,如AndPredicate处理两个Predicate,只有当两者都返回true它才返回true;AnyPredicate处理多个Predicate,当其中一个满足就返回true,等等。

看看具体的代码中如何使用这些Predicate吧:

package sean.study.commons.collections;import org.apache.commons.collections.Predicate;import org.apache.commons.collections.PredicateUtils;import org.apache.commons.collections.functors.InstanceofPredicate;import org.apache.commons.collections.functors.NotNullPredicate;import org.apache.commons.lang.BooleanUtils;import org.apache.commons.lang.StringUtils;public class PredicateUsage {    public static void main(String[] args) {        demoPredicates();    }    public static void demoPredicates() {        System.out.println(StringUtils.center(" demoPredicates ", 40, "="));        Predicate p1 = new InstanceofPredicate(String.class);        Predicate p2 = NotNullPredicate.getInstance();        Predicate p3 = new Predicate() {            public boolean evaluate(Object obj) {                String str = (String) obj;                return StringUtils.isAlphanumeric(str)                    && str.length() >= 6                    && str.length() <= 10;            }        };        Predicate p4 = PredicateUtils.allPredicate(new Predicate[]{p1, p2, p3});                String input = "ABCD1234";        Object[] raw = new Object[] {            "Is '",            input,            "' a valid input? ",            BooleanUtils.toStringYesNo(p4.evaluate(input)),            "."        };        System.out.println(StringUtils.join(raw));        System.out.println(StringUtils.repeat("=", 40));    }}

输出结果如下:

============ demoPredicates ============

Is ‘ABCD1234’ a valid input? yes.

这里面我首先定义了3个简单的Predicate,p1判断对象是否为String的实例,p2判断是否对象为null,p3是自定义的,判断是否为数字字母的组合,并且长度在6~10字符。然后我用AllPredicate将它们组合到一起,成为p4,当p1、p2、p3都满足时,p4的evaluate方法才返回true。利用Predicate我们可以把判断true或false的逻辑从特定的业务代码分离出来,以后我们重用也好,重新组装也好,都是很方便的。

0 0
原创粉丝点击