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 38 41 public class CmpExpr extends AbstractBooleanExpr { 42 private int _op; 43 private Expr _left; 44 private Expr _right; 45 46 53 private CmpExpr(int op, Expr left, Expr right) 54 { 55 _op = op; 56 _left = left; 57 _right = right; 58 } 59 60 67 static Expr create(int op, Expr left, Expr right) 68 { 69 switch (op) { 70 case LT: 71 return new LtExpr(left, right); 72 case LE: 73 return new LeExpr(left, right); 74 case GT: 75 return new GtExpr(left, right); 76 case GE: 77 return new GeExpr(left, right); 78 case EQ: 79 return new EqExpr(left, right); 80 case NE: 81 return new NeExpr(left, right); 82 } 83 84 return new CmpExpr(op, left, right); 85 } 86 87 90 @Override 91 public boolean isConstant() 92 { 93 return _left.isConstant() && _right.isConstant(); 94 } 95 96 101 @Override 102 public boolean evalBoolean(ELContext env) 103 throws ELException 104 { 105 Object aObj = _left.getValue(env); 106 Object bObj = _right.getValue(env); 107 108 if (aObj == null || bObj == null) { 109 return false; 111 } 112 113 if (aObj == bObj) 114 return _op == LE || _op == GE; 115 116 if (aObj instanceof Double || aObj instanceof Float || 117 bObj instanceof Double || bObj instanceof Float ) { 118 double a = toDouble(aObj, env); 119 double b = toDouble(bObj, env); 120 121 switch (_op) { 122 case LT: return a < b; 123 case LE: return a <= b; 124 case GT: return a > b; 125 case GE: return a >= b; 126 } 127 } 128 129 if (aObj instanceof Number || bObj instanceof Number ) { 130 double a = toDouble(aObj, env); 134 double b = toDouble(bObj, env); 135 136 switch (_op) { 137 case LT: return a < b; 138 case LE: return a <= b; 139 case GT: return a > b; 140 case GE: return a >= b; 141 } 142 } 143 144 if (aObj instanceof String || bObj instanceof String ) { 145 String a = toString(aObj, env); 146 String b = toString(bObj, env); 147 148 int cmp = a.compareTo(b); 149 150 switch (_op) { 151 case LT: return cmp < 0; 152 case LE: return cmp <= 0; 153 case GT: return cmp > 0; 154 case GE: return cmp >= 0; 155 } 156 } 157 158 if (aObj instanceof Comparable ) { 159 int cmp = ((Comparable ) aObj).compareTo(bObj); 160 161 switch (_op) { 162 case LT: return cmp < 0; 163 case LE: return cmp <= 0; 164 case GT: return cmp > 0; 165 case GE: return cmp >= 0; 166 } 167 } 168 169 if (bObj instanceof Comparable ) { 170 int cmp = ((Comparable ) bObj).compareTo(aObj); 171 172 switch (_op) { 173 case LT: return cmp > 0; 174 case LE: return cmp >= 0; 175 case GT: return cmp < 0; 176 case GE: return cmp <= 0; 177 } 178 } 179 180 ELException e = new ELException(L.l("can't compare {0} and {1}.", 181 aObj, bObj)); 182 183 error(e, env); 184 185 return false; 186 } 187 188 191 @Override 192 public void printCreate(WriteStream os) 193 throws IOException 194 { 195 os.print("new com.caucho.el.CmpExpr("); 196 os.print(_op + ", "); 197 _left.printCreate(os); 198 os.print(", "); 199 _right.printCreate(os); 200 os.print(")"); 201 } 202 203 206 public boolean equals(Object o) 207 { 208 if (! (o instanceof CmpExpr)) 209 return false; 210 211 CmpExpr expr = (CmpExpr) o; 212 213 return (_op == expr._op && 214 _left.equals(expr._left) && 215 _right.equals(expr._right)); 216 } 217 218 221 public String toString() 222 { 223 String op; 224 225 switch (_op) { 226 case LT: 227 op = " lt"; 228 break; 229 case LE: 230 op = " le "; 231 break; 232 case GT: 233 op = " gt "; 234 break; 235 case GE: 236 op = " ge "; 237 break; 238 default: 239 op = " unknown(" + _op + ") "; 240 break; 241 } 242 243 return "(" + _left + op + _right + ")"; 244 } 245 } 246 | Popular Tags |