KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigDecimal JavaDoc;
37 import java.math.BigInteger JavaDoc;
38
39 /**
40  * Represents a unary minus expression.
41  */

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

51   public MinusExpr(Expr expr)
52   {
53     _expr = expr;
54   }
55
56   /**
57    * Returns true if this is a constant expression.
58    */

59   @Override JavaDoc
60   public boolean isConstant()
61   {
62     return _expr.isConstant();
63   }
64
65   /**
66    * Evaluate the expression as an object.
67    *
68    * @param env the variable resolver
69    *
70    * @return the value as an object
71    */

72   @Override JavaDoc
73   public Object JavaDoc getValue(ELContext env)
74     throws ELException
75   {
76     Object JavaDoc obj = _expr.getValue(env);
77
78     if (obj == null)
79       return new Long JavaDoc(0);
80
81     Class JavaDoc type = obj.getClass();
82     if (Long JavaDoc.class == type)
83       return new Long JavaDoc(- ((Number JavaDoc) obj).longValue());
84     else if (Double JavaDoc.class == type)
85       return new Double JavaDoc(- ((Number JavaDoc) obj).doubleValue());
86     else if (Integer JavaDoc.class == type)
87       return new Integer JavaDoc(- ((Number JavaDoc) obj).intValue());
88     else if (Short JavaDoc.class == type)
89       return new Short JavaDoc((short) (- ((Number JavaDoc) obj).shortValue()));
90     else if (Byte JavaDoc.class == type)
91       return new Byte JavaDoc((byte) (- ((Number JavaDoc) obj).byteValue()));
92     else if (Float JavaDoc.class == type)
93       return new Float JavaDoc(- ((Number JavaDoc) obj).floatValue());
94     else if (BigDecimal JavaDoc.class == type)
95       return ((BigDecimal JavaDoc) obj).negate();
96     else if (BigInteger JavaDoc.class == type)
97       return ((BigInteger JavaDoc) obj).negate();
98     else if (String JavaDoc.class == type && isDouble(obj))
99       return new Double JavaDoc(- toDouble(obj, env));
100     else if (String JavaDoc.class == type)
101       return new Long JavaDoc(- toLong(obj, env));
102     else
103       throw new ELException(L.l("Can't convert {0} to number", obj));
104   }
105
106   /**
107    * Evaluate the expression as a long
108    *
109    * @param env the variable resolver
110    *
111    * @return the value as a long
112    */

113   @Override JavaDoc
114   public long evalLong(ELContext env)
115     throws ELException
116   {
117     return - _expr.evalLong(env);
118   }
119
120   /**
121    * Evaluate the expression as a double
122    */

123   @Override JavaDoc
124   public double evalDouble(ELContext env)
125     throws ELException
126   {
127     return - _expr.evalDouble(env);
128   }
129
130   /**
131    * Prints the Java code to recreate the UnaryExpr.
132    */

133   public void printCreate(WriteStream os)
134     throws IOException JavaDoc
135   {
136     os.print("new com.caucho.el.MinusExpr(");
137     _expr.printCreate(os);
138     os.print(")");
139   }
140
141   /**
142    * Returns true for equal strings.
143    */

144   public boolean equals(Object JavaDoc o)
145   {
146     if (! (o instanceof MinusExpr))
147       return false;
148
149     MinusExpr uexpr = (MinusExpr) o;
150
151     return (_expr.equals(uexpr._expr));
152   }
153
154
155   /**
156    * Returns a readable representation of the expr.
157    */

158   public String JavaDoc toString()
159   {
160     return "-" + _expr;
161   }
162 }
163
Popular Tags