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  package org.apache.commons.el;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  
22  import javax.servlet.jsp.el.ELException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   *
29   * <p>The implementation of the unary minus operator
30   * 
31   * @author Nathan Abramson - Art Technology Group
32   * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
33   **/
34  
35  public class UnaryMinusOperator
36    extends UnaryOperator
37  {
38      
39      //-------------------------------------
40      // Constants
41      //-------------------------------------
42      private static Log log = LogFactory.getLog(UnaryMinusOperator.class);
43      
44    //-------------------------------------
45    // Singleton
46    //-------------------------------------
47  
48    public static final UnaryMinusOperator SINGLETON =
49      new UnaryMinusOperator ();
50  
51    //-------------------------------------
52    /**
53     *
54     * Constructor
55     **/
56    public UnaryMinusOperator ()
57    {
58    }
59  
60    //-------------------------------------
61    // Expression methods
62    //-------------------------------------
63    /**
64     *
65     * Returns the symbol representing the operator
66     **/
67    public String getOperatorSymbol ()
68    {
69      return "-";
70    }
71  
72    //-------------------------------------
73    /**
74     *
75     * Applies the operator to the given value
76     **/
77    public Object apply (Object pValue)
78      throws ELException
79    {
80      if (pValue == null) {
81      
82        return PrimitiveObjects.getInteger (0);
83      }
84  
85      else if (pValue instanceof BigInteger) {
86          return ((BigInteger) pValue).negate();
87      }
88  
89      else if (pValue instanceof BigDecimal) {
90          return ((BigDecimal) pValue).negate();
91      }
92  
93      else if (pValue instanceof String) {
94        if (Coercions.isFloatingPointString (pValue)) {
95  	double dval =
96  	  ((Number) 
97  	   (Coercions.coerceToPrimitiveNumber 
98  	    (pValue, Double.class))).
99  	  doubleValue ();
100 	return PrimitiveObjects.getDouble (-dval);
101       }
102       else {
103 	long lval =
104 	  ((Number) 
105 	   (Coercions.coerceToPrimitiveNumber 
106 	    (pValue, Long.class))).
107 	  longValue ();
108 	return PrimitiveObjects.getLong (-lval);
109       }
110     }
111 
112     else if (pValue instanceof Byte) {
113       return PrimitiveObjects.getByte 
114 	((byte) -(((Byte) pValue).byteValue ()));
115     }
116     else if (pValue instanceof Short) {
117       return PrimitiveObjects.getShort 
118 	((short) -(((Short) pValue).shortValue ()));
119     }
120     else if (pValue instanceof Integer) {
121       return PrimitiveObjects.getInteger 
122 	((int) -(((Integer) pValue).intValue ()));
123     }
124     else if (pValue instanceof Long) {
125       return PrimitiveObjects.getLong 
126 	((long) -(((Long) pValue).longValue ()));
127     }
128     else if (pValue instanceof Float) {
129       return PrimitiveObjects.getFloat 
130 	((float) -(((Float) pValue).floatValue ()));
131     }
132     else if (pValue instanceof Double) {
133       return PrimitiveObjects.getDouble 
134 	((double) -(((Double) pValue).doubleValue ()));
135     }
136 
137     else {
138         if (log.isErrorEnabled()) {
139             String message = MessageUtil.getMessageWithArgs(
140                 Constants.UNARY_OP_BAD_TYPE,
141                 getOperatorSymbol(),
142                 pValue.getClass().getName());
143             log.error(message);
144             throw new ELException(message);
145         }     
146       return PrimitiveObjects.getInteger (0);
147     }
148   }
149 
150   //-------------------------------------
151 }