KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > ConditionalExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37
38 /**
39  * Represents a conditional expression
40  */

41 public class ConditionalExpr extends Expr {
42   private Expr _test;
43   private Expr _trueExpr;
44   private Expr _falseExpr;
45
46   /**
47    * Creates the conditional expression.
48    *
49    * @param test the conditional expressions test.
50    * @param trueExpr the true subexpression
51    * @param falseExpr the false subexpression
52    */

53   public ConditionalExpr(Expr test, Expr trueExpr, Expr falseExpr)
54   {
55     _test = test;
56     _trueExpr = trueExpr;
57     _falseExpr = falseExpr;
58   }
59
60   /**
61    * Returns true if this is a constant expression.
62    */

63   @Override JavaDoc
64   public boolean isConstant()
65   {
66     return (_test.isConstant() &&
67         _trueExpr.isConstant() &&
68         _falseExpr.isConstant());
69   }
70   
71   /**
72    * Evaluate the expression as an object.
73    *
74    * @param env the variable environment
75    *
76    * @return the result as an object
77    */

78   @Override JavaDoc
79   public Object JavaDoc 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   /**
89    * Evaluate the expression as a long
90    *
91    * @param env the variable environment
92    *
93    * @return the result as an long
94    */

95   @Override JavaDoc
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   /**
106    * Evaluate the expression as a double
107    *
108    * @param env the variable environment
109    *
110    * @return the result as an double
111    */

112   @Override JavaDoc
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   /**
123    * Evaluate the expression as a string
124    *
125    * @param env the variable environment
126    *
127    * @return the result as a string
128    */

129   @Override JavaDoc
130   public String JavaDoc 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   /**
140    * Evaluate the expression as a boolean
141    *
142    * @param env the variable environment
143    *
144    * @return the result as a boolean
145    */

146   @Override JavaDoc
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   /**
157    * Prints the Java code to recreate the expr
158    *
159    * @param os the output stream to the *.java file
160    */

161   @Override JavaDoc
162   public void printCreate(WriteStream os)
163     throws IOException JavaDoc
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   /**
175    * Returns true for equal strings.
176    */

177   public boolean equals(Object JavaDoc 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   /**
190    * Returns a readable representation of the expr.
191    */

192   public String JavaDoc toString()
193   {
194     return "(" + _test + " ? " + _trueExpr + " : " + _falseExpr + ")";
195   }
196 }
197
Popular Tags