01: package com.teamkonzept.lib.math;
02:
03: public class BinaryLogicalOperator extends BinaryOperator {
04:
05: final static int AND = 0;
06: final static int OR = 1;
07:
08: int type;
09:
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 evaluate(Object left, Object right)
22: throws BadOperandTypeException {
23: if (!(left instanceof Boolean))
24: throw new BadOperandTypeException(left, "Boolean");
25: if (!(right instanceof Boolean))
26: throw new BadOperandTypeException(right, "Boolean");
27: if (type == AND)
28: return new Boolean(((Boolean) left).booleanValue()
29: && ((Boolean) right).booleanValue());
30: else
31: return new Boolean(((Boolean) left).booleanValue()
32: || ((Boolean) 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(
44: BinaryLogicalOperator.class, op, position);
45: }
46: }
47:
48: }
|