◎筱米加步枪◎.Blog

Happy coding

Commons Collections之Predicates学习

筱米加步枪 posted @ 2010年7月29日 04:48 in [ 开源框架 ] with tags 函子 functor , 2423 阅读

今天无聊没事做,看到Commons Collections的东西,对Predicates接口和它下面挂着的几个类做了些研究,其实看了源代码之后,就知道很简单了。总结刚才的学习结果:

补充一下一个概念,函子(functor):执行操作或功能的部件,Predicates下面挂着的实现类就是一个函子,执行了各种逻辑运算和判断,你也可以根据自己的需要,实现Predicates类,实现evaluate方法,根据自己定义的算法来实现某一特定判断。

先看,Predicates的几个实现类,清单如下:

EqualPredicate
IdentityPredicate
NotPredicate
InstanceofPredicate
NullIsTruePredicate
NullIsFalsePredicate
NotNullPredicate
TruePredicate
FalsePredicate
UniquePredicate
AndPredicate
OrPredicate
AllPredicate
AnyPredicate
OnePredicate

并写了一个Demo程序,将几个Predicates的区别写在了程序的注释中,这是我的习惯。呵呵~~

public void testPredicate(){
		
		//相当于调用equals对比
		Predicate equal = new EqualPredicate("cst");  
		boolean resultEqual = equal.evaluate("cst");
		System.out.println("EqualPredicate:"+resultEqual);
		
		//相当于调用 == 对比
		Predicate identity = new IdentityPredicate(new String("cst"));
		boolean resultIdentity = identity.evaluate(new String("cst"));
		System.out.println("IdentityPredicate:"+resultIdentity);
		
		//装饰给定的Predicate指定对象想比,并返回相反的值
		Predicate not = new NotPredicate(equal);
		boolean resultNot = not.evaluate("cst");
		System.out.println("NotPredicate:"+resultNot);
		
		//相当于instanceof 对比
		Predicate instanceOf = new InstanceofPredicate(String.class);
		boolean resultInstanceof = instanceOf.evaluate("cst");
		System.out.println("InstanceofPredicate:"+resultInstanceof);
		
		// 装饰给定的Predicate
		//如果对象为null则返回true,否则返回给定Predicate的判定值
		Predicate  nullIsTrue = new NullIsTruePredicate(equal);
		boolean resultNullIsTrue = nullIsTrue.evaluate(null);
		System.out.println("NullIsTruePredicate:"+resultNullIsTrue);
		
		// 装饰给定的Predicate
		// 如果对象为null则返回false,否则返回给定Predicate的判定值
		Predicate  nullIsFalse = new NullIsFalsePredicate(equal);
		boolean resultNullIsFalse = nullIsFalse.evaluate(null);
		System.out.println("NullIsFalsePredicate:"+resultNullIsFalse);
		
		//当对象不为null时返回true
		Predicate notNull = NotNullPredicate.getInstance(); 
		boolean resultNotNull = notNull.evaluate(null);
		System.out.println("NotNullPredicate:"+resultNotNull);
		
		// 总是返回true
		Predicate truep = TruePredicate.getInstance(); 
		boolean resultTrue = truep.evaluate("cst");
		System.out.println("TruePredicate:"+resultTrue);
		
		// 总是返回false
		Predicate  falsep = FalsePredicate.getInstance();
		boolean resultFalse = falsep.evaluate("cst");
		System.out.println("FalsePredicate:"+resultFalse);
		
		// 首次evaluate判定对象总是返回true。
		// 其内部维护一个HashSet,每次调用evaluate(obj)
		//将会调用HashSet的add方法。该类常用于Collections过滤重复的对象。
		UniquePredicate unique = new UniquePredicate();
		boolean resultUnique1 = unique.evaluate("cst");
		boolean resultUnique2 = unique.evaluate("cst");
		System.out.println("UniquePredicate1:"+resultUnique1);
		System.out.println("UniquePredicate2:"+resultUnique2);
		
		
		///////////////////以下是实现复杂的逻辑/////////////////////////
		
		//装饰两个Predicate 判断返回两个Predicate和指定对象比较后and的结果
		Predicate and = new AndPredicate(equal, identity);
		boolean resultAnd = and.evaluate("cst");
		System.out.println("AndPredicate:"+resultAnd);
		
		//装饰两个Predicate 判断返回两个Predicate和指定对象比较后or的结果
		Predicate or = new OrPredicate(equal, identity);
		boolean resultOr = or.evaluate("cst");
		System.out.println("OrPredicate:"+resultOr);
		
		//多个Predicate指定对象比较后多个and的结果
		Predicate  [] predicates = new Predicate[]{equal,identity,not};
		Predicate all = new AllPredicate(predicates);
		boolean resultAll = all.evaluate("cst");
		System.out.println("AllPredicate:"+resultAll);
		
		//多个Predicate指定对象比较后多个or的结果
		Predicate any = new AnyPredicate(predicates);
		boolean resultAny = any.evaluate("cst");
		System.out.println("AnyPredicate:"+resultAny);
		
		//多个Predicate指定对象比较后,只要有一个结果为true就为true
		Predicate one = new OnePredicate(predicates);
		boolean resultOne = one.evaluate("cst");
		System.out.println("OnePredicate:"+resultOne);
	}

运行结果:

EqualPredicate:true
IdentityPredicate:false
NotPredicate:false
InstanceofPredicate:true
NullIsTruePredicate:true
NullIsFalsePredicate:false
NotNullPredicate:false
TruePredicate:true
FalsePredicate:false
UniquePredicate1:true
UniquePredicate2:false
AndPredicate:false
OrPredicate:true
AllPredicate:false
AnyPredicate:true
OnePredicate:tru

继续学习Commons Collections的其他东东~~了解下,方便日后少写人家已经实现过的代码。

  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter