KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > optional > BinaryPredicateNut


1 package jfun.yan.xml.nuts.optional;
2
3 import jfun.yan.Component;
4 import jfun.yan.Map2;
5 import jfun.yan.Monad;
6
7 /**
8  * Super class for any tag that compares two objects.
9  * The result is a Component that yields the boolean comparison result.
10  * <p>
11  * @author Ben Yu
12  * Nov 11, 2005 3:41:47 PM
13  */

14 public abstract class BinaryPredicateNut extends BinaryNut {
15   /**
16    * The predicate.
17    * @param v1 the first value.
18    * @param v2 the second value.
19    */

20   public abstract boolean predicate(Object JavaDoc v1, Object JavaDoc v2);
21   public Component eval(){
22     if(!isVal1Set() || !isVal2Set())
23       throw raise("both values should be set for comparison.");
24     //return Components.value(predicate(getVal1(), getVal2()));
25

26     final Map2 cmp = new Map2(){
27       public Object JavaDoc map(Object JavaDoc o1, Object JavaDoc o2){
28         return Boolean.valueOf(predicate(o1, o2));
29       }
30     };
31     final Component c1 = getComponent1();
32     final Component c2 = getComponent2();
33     return Monad.map(c1, c2, cmp).cast(boolean.class);
34   }
35 }
36
Popular Tags