1 28 29 package com.caucho.el; 30 31 import com.caucho.vfs.WriteStream; 32 33 import javax.el.ELContext; 34 import javax.el.ELException; 35 import java.io.IOException ; 36 import java.util.Collection ; 37 import java.util.Map ; 38 39 42 public class UnaryExpr extends Expr { 43 private int _op; 44 private Expr _expr; 45 46 52 private UnaryExpr(int op, Expr expr) 53 { 54 _op = op; 55 _expr = expr; 56 } 57 58 public static Expr create(int op, Expr expr) 59 { 60 switch (op) { 61 case MINUS: 62 return new MinusExpr(expr); 63 } 64 65 return new UnaryExpr(op, expr); 66 } 67 68 71 @Override 72 public boolean isConstant() 73 { 74 return _expr.isConstant(); 75 } 76 77 84 @Override 85 public Object getValue(ELContext env) 86 throws ELException 87 { 88 switch (_op) { 89 case NOT: 90 return new Boolean (! _expr.evalBoolean(env)); 91 92 case MINUS: 93 { 94 Object obj = _expr.getValue(env); 95 96 if (obj == null) 97 return new Long (0); 98 else if (obj instanceof Double || obj instanceof Float ) 99 return new Double (- ((Number ) obj).doubleValue()); 100 else if (obj instanceof Number ) 101 return new Long (- ((Number ) obj).longValue()); 102 else if (obj instanceof String ) { 103 String s = (String ) obj; 104 105 if (s.indexOf('.') < 0) 106 return new Long (- toLong(obj, env)); 107 else 108 return new Double (- toDouble(obj, env)); 109 } 110 else 111 return new Double (- toDouble(obj, env)); 112 } 113 114 case EMPTY: 115 if (evalBoolean(env)) 116 return Boolean.TRUE; 117 else 118 return Boolean.FALSE; 119 } 120 121 throw new UnsupportedOperationException (); 122 } 123 124 131 @Override 132 public boolean evalBoolean(ELContext env) 133 throws ELException 134 { 135 switch (_op) { 136 case NOT: 137 return ! _expr.evalBoolean(env); 138 139 case EMPTY: 140 { 141 Object obj = _expr.getValue(env); 142 143 if (obj == null) 144 return true; 145 else if (obj instanceof String ) 146 return "".equals(obj); 147 else if (obj instanceof Collection ) 148 return ((Collection ) obj).isEmpty(); 149 else if (obj instanceof Map ) 150 return ((Map ) obj).isEmpty(); 151 else if (obj.getClass().isArray()) 152 return java.lang.reflect.Array.getLength(obj) == 0; 153 else 154 return false; 155 } 156 } 157 158 ELException e = new ELException(L.l("can't compare.")); 159 160 error(e, env); 161 162 return false; 163 } 164 165 172 @Override 173 public long evalLong(ELContext env) 174 throws ELException 175 { 176 if (_op != MINUS) { 177 ELException e = new ELException(L.l("'not' and 'empty' operations can not be converted to long values.")); 178 error(e, env); 179 180 return 0; 181 } 182 183 return - _expr.evalLong(env); 185 } 186 187 190 @Override 191 public double evalDouble(ELContext env) 192 throws ELException 193 { 194 if (_op != MINUS) { 195 ELException e = new ELException(L.l("'not' and 'empty' operations can not be converted to double values.")); 196 197 error(e, env); 198 199 return 0; 200 } 201 202 return - _expr.evalDouble(env); 204 } 205 206 209 public void printCreate(WriteStream os) 210 throws IOException 211 { 212 os.print("com.caucho.el.UnaryExpr.create("); 213 os.print(_op + ", "); 214 _expr.printCreate(os); 215 os.print(")"); 216 } 217 218 221 public boolean equals(Object o) 222 { 223 if (! (o instanceof UnaryExpr)) 224 return false; 225 226 UnaryExpr uexpr = (UnaryExpr) o; 227 228 return (_op == uexpr._op && _expr.equals(uexpr._expr)); 229 } 230 231 232 235 public String toString() 236 { 237 String op; 238 239 switch (_op) { 240 case MINUS: 241 op = " -"; 242 break; 243 case NOT: 244 op = " not "; 245 break; 246 case EMPTY: 247 op = " empty "; 248 break; 249 default: 250 op = " unknown(" + _op + ") "; 251 break; 252 } 253 254 return op + _expr; 255 } 256 } 257 | Popular Tags |