KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jexl > parser > ASTUnaryMinusNode


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 JavaDoc;
19 import java.math.BigInteger JavaDoc;
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 JavaDoc jjtAccept(ParserVisitor visitor, Object JavaDoc data) {
51         return visitor.visit(this, data);
52     }
53
54     /** {@inheritDoc} */
55     public Object JavaDoc value(JexlContext jc) throws Exception JavaDoc {
56         Object JavaDoc val = ((SimpleNode) jjtGetChild(0)).value(jc);
57
58         if (val instanceof Byte JavaDoc) {
59             byte valueAsByte = ((Byte JavaDoc) val).byteValue();
60             return new Byte JavaDoc((byte) -valueAsByte);
61         } else if (val instanceof Short JavaDoc) {
62             short valueAsShort = ((Short JavaDoc) val).shortValue();
63             return new Short JavaDoc((short) -valueAsShort);
64         } else if (val instanceof Integer JavaDoc) {
65             int valueAsInt = ((Integer JavaDoc) val).intValue();
66             return new Integer JavaDoc(-valueAsInt);
67         } else if (val instanceof Long JavaDoc) {
68             long valueAsLong = ((Long JavaDoc) val).longValue();
69             return new Long JavaDoc(-valueAsLong);
70         } else if (val instanceof Float JavaDoc) {
71             float valueAsFloat = ((Float JavaDoc) val).floatValue();
72             return new Float JavaDoc(-valueAsFloat);
73         } else if (val instanceof Double JavaDoc) {
74             double valueAsDouble = ((Double JavaDoc) val).doubleValue();
75             return new Double JavaDoc(-valueAsDouble);
76         } else if (val instanceof BigDecimal JavaDoc) {
77             BigDecimal JavaDoc valueAsBigD = (BigDecimal JavaDoc) val;
78             return valueAsBigD.negate();
79         } else if (val instanceof BigInteger JavaDoc) {
80             BigInteger JavaDoc valueAsBigI = (BigInteger JavaDoc) val;
81             return valueAsBigI.negate();
82         }
83         throw new Exception JavaDoc("expression not a number");
84     }
85 }
86
Popular Tags