001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.el;
018
019import java.math.BigDecimal;
020import java.math.BigInteger;
021
022import javax.servlet.jsp.el.ELException;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027/**
028 *
029 * <p>The implementation of the unary minus operator
030 * 
031 * @author Nathan Abramson - Art Technology Group
032 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
033 **/
034
035public class UnaryMinusOperator
036  extends UnaryOperator
037{
038    
039    //-------------------------------------
040    // Constants
041    //-------------------------------------
042    private static Log log = LogFactory.getLog(UnaryMinusOperator.class);
043    
044  //-------------------------------------
045  // Singleton
046  //-------------------------------------
047
048  public static final UnaryMinusOperator SINGLETON =
049    new UnaryMinusOperator ();
050
051  //-------------------------------------
052  /**
053   *
054   * Constructor
055   **/
056  public UnaryMinusOperator ()
057  {
058  }
059
060  //-------------------------------------
061  // Expression methods
062  //-------------------------------------
063  /**
064   *
065   * Returns the symbol representing the operator
066   **/
067  public String getOperatorSymbol ()
068  {
069    return "-";
070  }
071
072  //-------------------------------------
073  /**
074   *
075   * Applies the operator to the given value
076   **/
077  public Object apply (Object pValue)
078    throws ELException
079  {
080    if (pValue == null) {
081    
082      return PrimitiveObjects.getInteger (0);
083    }
084
085    else if (pValue instanceof BigInteger) {
086        return ((BigInteger) pValue).negate();
087    }
088
089    else if (pValue instanceof BigDecimal) {
090        return ((BigDecimal) pValue).negate();
091    }
092
093    else if (pValue instanceof String) {
094      if (Coercions.isFloatingPointString (pValue)) {
095        double dval =
096          ((Number) 
097           (Coercions.coerceToPrimitiveNumber 
098            (pValue, Double.class))).
099          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}