001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.el;
030:
031: import com.caucho.vfs.WriteStream;
032:
033: import javax.el.ELContext;
034: import javax.el.ELException;
035: import java.io.IOException;
036: import java.math.BigDecimal;
037: import java.math.BigInteger;
038:
039: /**
040: * Represents a unary minus expression.
041: */
042: public class MinusExpr extends Expr {
043: private final Expr _expr;
044:
045: /**
046: * Create a new minus expression.
047: *
048: * @param op the lexical code for the operation
049: * @param expr the base expression
050: */
051: public MinusExpr(Expr expr) {
052: _expr = expr;
053: }
054:
055: /**
056: * Returns true if this is a constant expression.
057: */
058: @Override
059: public boolean isConstant() {
060: return _expr.isConstant();
061: }
062:
063: /**
064: * Evaluate the expression as an object.
065: *
066: * @param env the variable resolver
067: *
068: * @return the value as an object
069: */
070: @Override
071: public Object getValue(ELContext env) throws ELException {
072: Object obj = _expr.getValue(env);
073:
074: if (obj == null)
075: return new Long(0);
076:
077: Class type = obj.getClass();
078: if (Long.class == type)
079: return new Long(-((Number) obj).longValue());
080: else if (Double.class == type)
081: return new Double(-((Number) obj).doubleValue());
082: else if (Integer.class == type)
083: return new Integer(-((Number) obj).intValue());
084: else if (Short.class == type)
085: return new Short((short) (-((Number) obj).shortValue()));
086: else if (Byte.class == type)
087: return new Byte((byte) (-((Number) obj).byteValue()));
088: else if (Float.class == type)
089: return new Float(-((Number) obj).floatValue());
090: else if (BigDecimal.class == type)
091: return ((BigDecimal) obj).negate();
092: else if (BigInteger.class == type)
093: return ((BigInteger) obj).negate();
094: else if (String.class == type && isDouble(obj))
095: return new Double(-toDouble(obj, env));
096: else if (String.class == type)
097: return new Long(-toLong(obj, env));
098: else
099: throw new ELException(L.l("Can't convert {0} to number",
100: obj));
101: }
102:
103: /**
104: * Evaluate the expression as a long
105: *
106: * @param env the variable resolver
107: *
108: * @return the value as a long
109: */
110: @Override
111: public long evalLong(ELContext env) throws ELException {
112: return -_expr.evalLong(env);
113: }
114:
115: /**
116: * Evaluate the expression as a double
117: */
118: @Override
119: public double evalDouble(ELContext env) throws ELException {
120: return -_expr.evalDouble(env);
121: }
122:
123: /**
124: * Prints the Java code to recreate the UnaryExpr.
125: */
126: public void printCreate(WriteStream os) throws IOException {
127: os.print("new com.caucho.el.MinusExpr(");
128: _expr.printCreate(os);
129: os.print(")");
130: }
131:
132: /**
133: * Returns true for equal strings.
134: */
135: public boolean equals(Object o) {
136: if (!(o instanceof MinusExpr))
137: return false;
138:
139: MinusExpr uexpr = (MinusExpr) o;
140:
141: return (_expr.equals(uexpr._expr));
142: }
143:
144: /**
145: * Returns a readable representation of the expr.
146: */
147: public String toString() {
148: return "-" + _expr;
149: }
150: }
|