1 29 30 package com.caucho.el; 31 32 import com.caucho.vfs.WriteStream; 33 34 import javax.el.ELContext; 35 import javax.el.ELException; 36 import java.io.IOException ; 37 import java.math.BigDecimal ; 38 import java.math.BigInteger ; 39 40 43 public class LeExpr extends AbstractBooleanExpr { 44 private final Expr _left; 45 private final Expr _right; 46 47 54 public LeExpr(Expr left, Expr right) 55 { 56 _left = left; 57 _right = right; 58 } 59 60 63 @Override 64 public boolean isConstant() 65 { 66 return _left.isConstant() && _right.isConstant(); 67 } 68 69 74 @Override 75 public boolean evalBoolean(ELContext env) 76 throws ELException 77 { 78 Object aObj = _left.getValue(env); 79 Object bObj = _right.getValue(env); 80 81 if (aObj == bObj) 82 return true; 83 84 if (aObj == null || bObj == null) 85 return false; 86 87 Class aType = aObj.getClass(); 88 Class bType = bObj.getClass(); 89 90 if (aObj instanceof BigDecimal || bObj instanceof BigDecimal ) { 91 BigDecimal a = toBigDecimal(aObj, env); 92 BigDecimal b = toBigDecimal(bObj, env); 93 94 return a.compareTo(b) <= 0; 95 } 96 97 if (aType == Double .class || aType == Float .class || 98 bType == Double .class || bType == Float .class) { 99 double a = toDouble(aObj, env); 100 double b = toDouble(bObj, env); 101 102 return a <= b; 103 } 104 105 if (aType == BigInteger .class || bType == BigInteger .class) { 106 BigInteger a = toBigInteger(aObj, env); 107 BigInteger b = toBigInteger(bObj, env); 108 109 return a.compareTo(b) <= 0; 110 } 111 112 if (aObj instanceof Number || bObj instanceof Number ) { 113 long a = toLong(aObj, env); 114 long b = toLong(bObj, env); 115 116 return a <= b; 117 } 118 119 if (aObj instanceof String || bObj instanceof String ) { 120 String a = toString(aObj, env); 121 String b = toString(bObj, env); 122 123 return a.compareTo(b) <= 0; 124 } 125 126 if (aObj instanceof Comparable ) { 127 int cmp = ((Comparable ) aObj).compareTo(bObj); 128 129 return cmp <= 0; 130 } 131 132 if (bObj instanceof Comparable ) { 133 int cmp = ((Comparable ) bObj).compareTo(aObj); 134 135 return cmp >= 0; 136 } 137 138 ELException e = new ELException(L.l("can't compare {0} and {1}.", 139 aObj, bObj)); 140 141 error(e, env); 142 143 return false; 144 } 145 146 149 @Override 150 public void printCreate(WriteStream os) 151 throws IOException 152 { 153 os.print("new com.caucho.el.LeExpr("); 154 _left.printCreate(os); 155 os.print(", "); 156 _right.printCreate(os); 157 os.print(")"); 158 } 159 160 163 public boolean equals(Object o) 164 { 165 if (! (o instanceof LeExpr)) 166 return false; 167 168 LeExpr expr = (LeExpr) o; 169 170 return (_left.equals(expr._left) && 171 _right.equals(expr._right)); 172 } 173 174 177 public String toString() 178 { 179 return "(" + _left + " le " + _right + ")"; 180 } 181 } 182 | Popular Tags |