KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * Represents a unary expression.
41  */

42 public class UnaryExpr extends Expr {
43   private int _op;
44   private Expr _expr;
45
46   /**
47    * Create a new unary expression.
48    *
49    * @param op the lexical code for the operation
50    * @param expr the base expression
51    */

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   /**
69    * Returns true if this is a constant expression.
70    */

71   @Override JavaDoc
72   public boolean isConstant()
73   {
74     return _expr.isConstant();
75   }
76
77   /**
78    * Evaluate the expression as an object.
79    *
80    * @param env the variable resolver
81    *
82    * @return the value as an object
83    */

84   @Override JavaDoc
85   public Object JavaDoc getValue(ELContext env)
86     throws ELException
87   {
88     switch (_op) {
89     case NOT:
90       return new Boolean JavaDoc(! _expr.evalBoolean(env));
91
92     case MINUS:
93     {
94       Object JavaDoc obj = _expr.getValue(env);
95
96       if (obj == null)
97         return new Long JavaDoc(0);
98       else if (obj instanceof Double JavaDoc || obj instanceof Float JavaDoc)
99         return new Double JavaDoc(- ((Number JavaDoc) obj).doubleValue());
100       else if (obj instanceof Number JavaDoc)
101         return new Long JavaDoc(- ((Number JavaDoc) obj).longValue());
102       else if (obj instanceof String JavaDoc) {
103     String JavaDoc s = (String JavaDoc) obj;
104
105     if (s.indexOf('.') < 0)
106       return new Long JavaDoc(- toLong(obj, env));
107     else
108       return new Double JavaDoc(- toDouble(obj, env));
109       }
110       else
111         return new Double JavaDoc(- 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 JavaDoc();
122   }
123
124   /**
125    * Evaluate the expression as a boolean.
126    *
127    * @param env the variable resolver
128    *
129    * @return the value as a boolean
130    */

131   @Override JavaDoc
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 JavaDoc obj = _expr.getValue(env);
142
143       if (obj == null)
144         return true;
145       else if (obj instanceof String JavaDoc)
146         return "".equals(obj);
147       else if (obj instanceof Collection JavaDoc)
148         return ((Collection JavaDoc) obj).isEmpty();
149       else if (obj instanceof Map JavaDoc)
150         return ((Map JavaDoc) 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   /**
166    * Evaluate the expression as a long
167    *
168    * @param env the variable resolver
169    *
170    * @return the value as a long
171    */

172   @Override JavaDoc
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     // _op == MINUS
184
return - _expr.evalLong(env);
185   }
186
187   /**
188    * Evaluate the expression as a double
189    */

190   @Override JavaDoc
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     // _op == MINUS
203
return - _expr.evalDouble(env);
204   }
205
206   /**
207    * Prints the Java code to recreate the UnaryExpr.
208    */

209   public void printCreate(WriteStream os)
210     throws IOException JavaDoc
211   {
212     os.print("com.caucho.el.UnaryExpr.create(");
213     os.print(_op + ", ");
214     _expr.printCreate(os);
215     os.print(")");
216   }
217
218   /**
219    * Returns true for equal strings.
220    */

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

235   public String JavaDoc toString()
236   {
237     String JavaDoc 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