View Javadoc

1   /*
2    * Copyright 2002-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jexl.parser;
17  
18  import java.math.BigDecimal;
19  import java.math.BigInteger;
20  
21  import org.apache.commons.jexl.JexlContext;
22  
23  /***
24   * - (unary minus).
25   * 
26   * @author <a href="mailto:mhw@kremvax.net">Mark H. Wilkinson</a>
27   * @version $Id: ASTUnaryMinusNode.java 398325 2006-04-30 12:34:29Z dion $
28   */
29  public class ASTUnaryMinusNode extends SimpleNode {
30      /***
31       * Create the node given an id.
32       * 
33       * @param id node id.
34       */
35      public ASTUnaryMinusNode(int id) {
36          super(id);
37      }
38  
39      /***
40       * Create a node with the given parser and id.
41       * 
42       * @param p a parser.
43       * @param id node id.
44       */
45      public ASTUnaryMinusNode(Parser p, int id) {
46          super(p, id);
47      }
48  
49      /*** {@inheritDoc} */
50      public Object jjtAccept(ParserVisitor visitor, Object data) {
51          return visitor.visit(this, data);
52      }
53  
54      /*** {@inheritDoc} */
55      public Object value(JexlContext jc) throws Exception {
56          Object val = ((SimpleNode) jjtGetChild(0)).value(jc);
57  
58          if (val instanceof Byte) {
59              byte valueAsByte = ((Byte) val).byteValue();
60              return new Byte((byte) -valueAsByte);
61          } else if (val instanceof Short) {
62              short valueAsShort = ((Short) val).shortValue();
63              return new Short((short) -valueAsShort);
64          } else if (val instanceof Integer) {
65              int valueAsInt = ((Integer) val).intValue();
66              return new Integer(-valueAsInt);
67          } else if (val instanceof Long) {
68              long valueAsLong = ((Long) val).longValue();
69              return new Long(-valueAsLong);
70          } else if (val instanceof Float) {
71              float valueAsFloat = ((Float) val).floatValue();
72              return new Float(-valueAsFloat);
73          } else if (val instanceof Double) {
74              double valueAsDouble = ((Double) val).doubleValue();
75              return new Double(-valueAsDouble);
76          } else if (val instanceof BigDecimal) {
77              BigDecimal valueAsBigD = (BigDecimal) val;
78              return valueAsBigD.negate();
79          } else if (val instanceof BigInteger) {
80              BigInteger valueAsBigI = (BigInteger) val;
81              return valueAsBigI.negate();
82          }
83          throw new Exception("expression not a number");
84      }
85  }