KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > math > BinaryLogicalOperator


1 package com.teamkonzept.lib.math;
2
3 public class BinaryLogicalOperator extends BinaryOperator{
4     
5     final static int AND = 0;
6     final static int OR = 1;
7     
8     int type;
9
10     public BinaryLogicalOperator(int type, int parenlevel, int position)
11     throws UnsupportedOperatorException{
12     super(parenlevel, getPriority(type, position), position);
13     this.type = type;
14     }
15     
16     /**
17      * evaluates that operator on that operands
18      * both must be of type boolean
19      * result is of type Boolean
20      */

21     public Object JavaDoc evaluate(Object JavaDoc left, Object JavaDoc right)
22     throws BadOperandTypeException{
23     if ( !(left instanceof Boolean JavaDoc) )
24         throw new BadOperandTypeException(left, "Boolean");
25     if ( !(right instanceof Boolean JavaDoc) )
26         throw new BadOperandTypeException(right, "Boolean");
27     if ( type == AND )
28         return new Boolean JavaDoc(((Boolean JavaDoc)left).booleanValue()
29                    && ((Boolean JavaDoc)right).booleanValue());
30     else
31         return new Boolean JavaDoc(((Boolean JavaDoc)left).booleanValue()
32                    || ((Boolean JavaDoc)right).booleanValue());
33     }
34
35     static int getPriority(int op, int position)
36     throws UnsupportedOperatorException{
37     switch ( op ){
38     case AND:
39         return LOGICAL_AND_PRIORITY;
40     case OR:
41         return LOGICAL_OR_PRIORITY;
42     default:
43         throw new UnsupportedOperatorException(BinaryLogicalOperator.class,
44                            op, position);
45     }
46     }
47
48 }
49
Popular Tags