View Javadoc
1 /*** 2 * BooleanOperator.java 3 * 4 * $Author: mballesteros $ 5 * $Date: 2003/11/28 19:18:04 $ 6 * $Revision: 1.1 $ 7 */ 8 package net.sf.jec; 9 10 /*** 11 * 12 * @author mballesteros 13 */ 14 public class BooleanOperator extends Operator { 15 16 private static final int EQUAL = 0; 17 private static final int NOT_EQUAL = 1; 18 private static final int LESS_THAN = 2; 19 private static final int GREATER_THAN = 3; 20 private static final int AND = 4; 21 private static final int OR = 5; 22 23 private int type; 24 private Operator op1, op2; 25 private boolean throwExceptions; 26 27 /*** Creates a new instance of BooleanOperator */ 28 public BooleanOperator(String type, Operator op1, Operator op2, 29 boolean throwExceptions) { 30 if (type.equals("==")) this.type = EQUAL; 31 else if (type.equals("!=")) this.type = NOT_EQUAL; 32 else if (type.equals("<")) this.type = LESS_THAN; 33 else if (type.equals(">")) this.type = GREATER_THAN; 34 else if (type.equals("&&")) this.type = AND; 35 else if (type.equals("||")) this.type = OR; 36 this.op1 = op1; 37 this.op2 = op2; 38 this.throwExceptions = throwExceptions; 39 } 40 41 /*** Returns a function representation String for this operator 42 */ 43 public String toFunctionString() { 44 return "Boolean(" + (type==EQUAL?"==":(type==NOT_EQUAL?"!=":(type==LESS_THAN?"<":">"))) 45 + ", " + op1 + ", " + op2 + ")"; 46 } 47 48 /*** Returns an expression String representation for this operator 49 */ 50 public String toExpressionString() { 51 return op1 + (type==EQUAL?"==":(type==NOT_EQUAL?"!=":(type==LESS_THAN?"<":">"))) 52 + op2; 53 } 54 55 /*** Returns the associated value to the given context and root context 56 * @param rootCtx The root context, needed for operators that require 57 * expression evaluation from root point. Example: Indexer operators 58 * @param ctx The current context where the operator will work over 59 */ 60 protected Object directMap(Object rootCtx, Object ctx) throws EvaluationException { 61 Object o1 = op1.apply(rootCtx, ctx); 62 Object o2 = op2.apply(rootCtx, ctx); 63 64 if (o1 == o2) return Boolean.TRUE; 65 66 switch (type) { 67 case EQUAL: 68 if ( (o1 instanceof Number) && (o2 instanceof Number)) { 69 if ( ((Number)o1).doubleValue() == ((Number)o2).doubleValue()) return Boolean.TRUE; 70 else return Boolean.FALSE; 71 } else { 72 if ( o1.equals(o2) ) return Boolean.TRUE; else return Boolean.FALSE; 73 } 74 case NOT_EQUAL: 75 if ( (o1 instanceof Number) && (o2 instanceof Number)) { 76 if ( ((Number)o1).doubleValue() != ((Number)o2).doubleValue()) return Boolean.TRUE; 77 else return Boolean.FALSE; 78 } else { 79 if ( !o1.equals(o2) ) return Boolean.TRUE; else return Boolean.FALSE; 80 } 81 case LESS_THAN: 82 if ( (o1 instanceof Comparable) && (o2 instanceof Comparable)) { 83 if ( ((Comparable)o1).compareTo(o2) < 0 ) return Boolean.TRUE; 84 else return Boolean.FALSE; 85 } else { 86 return Boolean.FALSE; 87 } 88 case GREATER_THAN: 89 if ( (o1 instanceof Comparable) && (o2 instanceof Comparable)) { 90 if ( ((Comparable)o1).compareTo(o2) > 0 ) return Boolean.TRUE; 91 else return Boolean.FALSE; 92 } else { 93 return Boolean.FALSE; 94 } 95 case AND: 96 if ( (o1==Boolean.TRUE) && (o2==Boolean.TRUE) ) { 97 return Boolean.TRUE; 98 } else { 99 return Boolean.FALSE; 100 } 101 case OR: 102 if ( (o1==Boolean.TRUE) || (o2==Boolean.TRUE) ) { 103 return Boolean.TRUE; 104 } else { 105 return Boolean.FALSE; 106 } 107 default: 108 return null; 109 } 110 } 111 }

This page was automatically generated by Maven