Coverage Report - org.apache.commons.el.UnaryMinusOperator
 
Classes in this File Line Coverage Branch Coverage Complexity
UnaryMinusOperator
0%
0/34
0%
0/28
9
 
 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  0
     private static Log log = LogFactory.getLog(UnaryMinusOperator.class);
 43  
     
 44  
   //-------------------------------------
 45  
   // Singleton
 46  
   //-------------------------------------
 47  
 
 48  0
   public static final UnaryMinusOperator SINGLETON =
 49  
     new UnaryMinusOperator ();
 50  
 
 51  
   //-------------------------------------
 52  
   /**
 53  
    *
 54  
    * Constructor
 55  
    **/
 56  
   public UnaryMinusOperator ()
 57  0
   {
 58  0
   }
 59  
 
 60  
   //-------------------------------------
 61  
   // Expression methods
 62  
   //-------------------------------------
 63  
   /**
 64  
    *
 65  
    * Returns the symbol representing the operator
 66  
    **/
 67  
   public String getOperatorSymbol ()
 68  
   {
 69  0
     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  0
     if (pValue == null) {
 81  
     
 82  0
       return PrimitiveObjects.getInteger (0);
 83  
     }
 84  
 
 85  0
     else if (pValue instanceof BigInteger) {
 86  0
         return ((BigInteger) pValue).negate();
 87  
     }
 88  
 
 89  0
     else if (pValue instanceof BigDecimal) {
 90  0
         return ((BigDecimal) pValue).negate();
 91  
     }
 92  
 
 93  0
     else if (pValue instanceof String) {
 94  0
       if (Coercions.isFloatingPointString (pValue)) {
 95  0
         double dval =
 96  
           ((Number) 
 97  
            (Coercions.coerceToPrimitiveNumber 
 98  
             (pValue, Double.class))).
 99  
           doubleValue ();
 100  0
         return PrimitiveObjects.getDouble (-dval);
 101  
       }
 102  
       else {
 103  0
         long lval =
 104  
           ((Number) 
 105  
            (Coercions.coerceToPrimitiveNumber 
 106  
             (pValue, Long.class))).
 107  
           longValue ();
 108  0
         return PrimitiveObjects.getLong (-lval);
 109  
       }
 110  
     }
 111  
 
 112  0
     else if (pValue instanceof Byte) {
 113  0
       return PrimitiveObjects.getByte 
 114  
         ((byte) -(((Byte) pValue).byteValue ()));
 115  
     }
 116  0
     else if (pValue instanceof Short) {
 117  0
       return PrimitiveObjects.getShort 
 118  
         ((short) -(((Short) pValue).shortValue ()));
 119  
     }
 120  0
     else if (pValue instanceof Integer) {
 121  0
       return PrimitiveObjects.getInteger 
 122  
         ((int) -(((Integer) pValue).intValue ()));
 123  
     }
 124  0
     else if (pValue instanceof Long) {
 125  0
       return PrimitiveObjects.getLong 
 126  
         ((long) -(((Long) pValue).longValue ()));
 127  
     }
 128  0
     else if (pValue instanceof Float) {
 129  0
       return PrimitiveObjects.getFloat 
 130  
         ((float) -(((Float) pValue).floatValue ()));
 131  
     }
 132  0
     else if (pValue instanceof Double) {
 133  0
       return PrimitiveObjects.getDouble 
 134  
         ((double) -(((Double) pValue).doubleValue ()));
 135  
     }
 136  
 
 137  
     else {
 138  0
         if (log.isErrorEnabled()) {
 139  0
             String message = MessageUtil.getMessageWithArgs(
 140  
                 Constants.UNARY_OP_BAD_TYPE,
 141  
                 getOperatorSymbol(),
 142  
                 pValue.getClass().getName());
 143  0
             log.error(message);
 144  0
             throw new ELException(message);
 145  
         }     
 146  0
       return PrimitiveObjects.getInteger (0);
 147  
     }
 148  
   }
 149  
 
 150  
   //-------------------------------------
 151  
 }