KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > parser > AstNegative


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */
/* Generated By:JJTree: Do not edit this line. AstNegative.java */
23
24 package com.sun.el.parser;
25
26 import java.math.BigDecimal JavaDoc;
27 import java.math.BigInteger JavaDoc;
28
29 import javax.el.ELException;
30
31 import com.sun.el.lang.EvaluationContext;
32
33 /**
34  * @author Jacob Hookom [jacob@hookom.net]
35  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
36  */

37 public final class AstNegative extends SimpleNode {
38     public AstNegative(int id) {
39         super(id);
40     }
41
42     public Class JavaDoc getType(EvaluationContext ctx)
43             throws ELException {
44         return Number JavaDoc.class;
45     }
46
47     public Object JavaDoc getValue(EvaluationContext ctx)
48             throws ELException {
49         Object JavaDoc obj = this.children[0].getValue(ctx);
50
51         if (obj == null) {
52             return new Long JavaDoc(0);
53         }
54         if (obj instanceof BigDecimal JavaDoc) {
55             return ((BigDecimal JavaDoc) obj).negate();
56         }
57         if (obj instanceof BigInteger JavaDoc) {
58             return ((BigInteger JavaDoc) obj).negate();
59         }
60         if (obj instanceof String JavaDoc) {
61             if (isStringFloat((String JavaDoc) obj)) {
62                 return new Double JavaDoc(-Double.parseDouble((String JavaDoc) obj));
63             }
64             return new Long JavaDoc(-Long.parseLong((String JavaDoc) obj));
65         }
66         Class JavaDoc type = obj.getClass();
67         if (obj instanceof Long JavaDoc || Long.TYPE == type) {
68             return new Long JavaDoc(-((Long JavaDoc) obj).longValue());
69         }
70         if (obj instanceof Double JavaDoc || Double.TYPE == type) {
71             return new Double JavaDoc(-((Double JavaDoc) obj).doubleValue());
72         }
73         if (obj instanceof Integer JavaDoc || Integer.TYPE == type) {
74             return new Integer JavaDoc(-((Integer JavaDoc) obj).intValue());
75         }
76         if (obj instanceof Float JavaDoc || Float.TYPE == type) {
77             return new Float JavaDoc(-((Float JavaDoc) obj).floatValue());
78         }
79         if (obj instanceof Short JavaDoc || Short.TYPE == type) {
80             return new Short JavaDoc((short) -((Short JavaDoc) obj).shortValue());
81         }
82         if (obj instanceof Byte JavaDoc || Byte.TYPE == type) {
83             return new Byte JavaDoc((byte) -((Byte JavaDoc) obj).byteValue());
84         }
85         Long JavaDoc num = (Long JavaDoc) coerceToNumber(obj, Long JavaDoc.class);
86         return new Long JavaDoc(-num.longValue());
87     }
88 }
89
Popular Tags