View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  /* Generated By:JJTree: Do not edit this line. AstNegative.java */
18  
19  package org.apache.el.parser;
20  
21  import java.math.BigDecimal;
22  import java.math.BigInteger;
23  
24  import javax.el.ELException;
25  
26  import org.apache.el.lang.EvaluationContext;
27  
28  
29  /**
30   * @author Jacob Hookom [jacob@hookom.net]
31   * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
32   */
33  public final class AstNegative extends SimpleNode {
34      public AstNegative(int id) {
35          super(id);
36      }
37  
38      public Class getType(EvaluationContext ctx)
39              throws ELException {
40          return Number.class;
41      }
42  
43      public Object getValue(EvaluationContext ctx)
44              throws ELException {
45          Object obj = this.children[0].getValue(ctx);
46  
47          if (obj == null) {
48              return new Long(0);
49          }
50          if (obj instanceof BigDecimal) {
51              return ((BigDecimal) obj).negate();
52          }
53          if (obj instanceof BigInteger) {
54              return ((BigInteger) obj).negate();
55          }
56          if (obj instanceof String) {
57              if (isStringFloat((String) obj)) {
58                  return new Double(-Double.parseDouble((String) obj));
59              }
60              return new Long(-Long.parseLong((String) obj));
61          }
62          if (obj instanceof Long) {
63              return new Long(-((Long) obj).longValue());
64          }
65          if (obj instanceof Double) {
66              return new Double(-((Double) obj).doubleValue());
67          }
68          if (obj instanceof Integer) {
69              return new Integer(-((Integer) obj).intValue());
70          }
71          if (obj instanceof Float) {
72              return new Float(-((Float) obj).floatValue());
73          }
74          if (obj instanceof Short) {
75              return new Short((short) -((Short) obj).shortValue());
76          }
77          if (obj instanceof Byte) {
78              return new Byte((byte) -((Byte) obj).byteValue());
79          }
80          Long num = (Long) coerceToNumber(obj, Long.class);
81          return new Long(-num.longValue());
82      }
83  }