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 ConditionalExpr extends Expr { 42 private Expr _test; 43 private Expr _trueExpr; 44 private Expr _falseExpr; 45 46 53 public ConditionalExpr(Expr test, Expr trueExpr, Expr falseExpr) 54 { 55 _test = test; 56 _trueExpr = trueExpr; 57 _falseExpr = falseExpr; 58 } 59 60 63 @Override 64 public boolean isConstant() 65 { 66 return (_test.isConstant() && 67 _trueExpr.isConstant() && 68 _falseExpr.isConstant()); 69 } 70 71 78 @Override 79 public Object getValue(ELContext env) 80 throws ELException 81 { 82 if (_test.evalBoolean(env)) 83 return _trueExpr.getValue(env); 84 else 85 return _falseExpr.getValue(env); 86 } 87 88 95 @Override 96 public long evalLong(ELContext env) 97 throws ELException 98 { 99 if (_test.evalBoolean(env)) 100 return _trueExpr.evalLong(env); 101 else 102 return _falseExpr.evalLong(env); 103 } 104 105 112 @Override 113 public double evalDouble(ELContext env) 114 throws ELException 115 { 116 if (_test.evalBoolean(env)) 117 return _trueExpr.evalDouble(env); 118 else 119 return _falseExpr.evalDouble(env); 120 } 121 122 129 @Override 130 public String evalString(ELContext env) 131 throws ELException 132 { 133 if (_test.evalBoolean(env)) 134 return _trueExpr.evalString(env); 135 else 136 return _falseExpr.evalString(env); 137 } 138 139 146 @Override 147 public boolean evalBoolean(ELContext env) 148 throws ELException 149 { 150 if (_test.evalBoolean(env)) 151 return _trueExpr.evalBoolean(env); 152 else 153 return _falseExpr.evalBoolean(env); 154 } 155 156 161 @Override 162 public void printCreate(WriteStream os) 163 throws IOException 164 { 165 os.print("new com.caucho.el.ConditionalExpr("); 166 _test.printCreate(os); 167 os.print(", "); 168 _trueExpr.printCreate(os); 169 os.print(", "); 170 _falseExpr.printCreate(os); 171 os.print(")"); 172 } 173 174 177 public boolean equals(Object o) 178 { 179 if (! (o instanceof ConditionalExpr)) 180 return false; 181 182 ConditionalExpr expr = (ConditionalExpr) o; 183 184 return (_test == expr._test && 185 _trueExpr.equals(expr._trueExpr) && 186 _falseExpr.equals(expr._falseExpr)); 187 } 188 189 192 public String toString() 193 { 194 return "(" + _test + " ? " + _trueExpr + " : " + _falseExpr + ")"; 195 } 196 } 197 | Popular Tags |