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 LtExpr extends AbstractBooleanExpr { 44 private final Expr _left; 45 private final Expr _right; 46 47 54 public LtExpr(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 == null || bObj == null) 82 return false; 83 84 Class aType = aObj.getClass(); 85 Class bType = bObj.getClass(); 86 87 if (aObj instanceof BigDecimal || bObj instanceof BigDecimal ) { 88 BigDecimal a = toBigDecimal(aObj, env); 89 BigDecimal b = toBigDecimal(bObj, env); 90 91 return a.compareTo(b) < 0; 92 } 93 94 if (aType == Double .class || aType == Float .class || 95 bType == Double .class || bType == Float .class) { 96 double a = toDouble(aObj, env); 97 double b = toDouble(bObj, env); 98 99 return a < b; 100 } 101 102 if (aType == BigInteger .class || bType == BigInteger .class) { 103 BigInteger a = toBigInteger(aObj, env); 104 BigInteger b = toBigInteger(bObj, env); 105 106 return a.compareTo(b) < 0; 107 } 108 109 if (aObj instanceof Number || bObj instanceof Number ) { 110 long a = toLong(aObj, env); 111 long b = toLong(bObj, env); 112 113 return a < b; 114 } 115 116 if (aObj instanceof String || bObj instanceof String ) { 117 String a = toString(aObj, env); 118 String b = toString(bObj, env); 119 120 return a.compareTo(b) < 0; 121 } 122 123 if (aObj instanceof Comparable ) { 124 int cmp = ((Comparable ) aObj).compareTo(bObj); 125 126 return cmp < 0; 127 } 128 129 if (bObj instanceof Comparable ) { 130 int cmp = ((Comparable ) bObj).compareTo(aObj); 131 132 return cmp > 0; 133 } 134 135 ELException e = new ELException(L.l("can't compare {0} and {1}.", 136 aObj, bObj)); 137 138 error(e, env); 139 140 return false; 141 } 142 143 146 @Override 147 public void printCreate(WriteStream os) 148 throws IOException 149 { 150 os.print("new com.caucho.el.LtExpr("); 151 _left.printCreate(os); 152 os.print(", "); 153 _right.printCreate(os); 154 os.print(")"); 155 } 156 157 160 public boolean equals(Object o) 161 { 162 if (! (o instanceof LtExpr)) 163 return false; 164 165 LtExpr expr = (LtExpr) o; 166 167 return (_left.equals(expr._left) && 168 _right.equals(expr._right)); 169 } 170 171 174 public String toString() 175 { 176 return "(" + _left + " lt " + _right + ")"; 177 } 178 } 179 | Popular Tags |