/**
 * Copyright 2013 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.kundera.utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.math.NumberUtils;


/**
 * Utility class to define various numeric functions 
 * 
 * @author vivek.mishra
 *
 */


public final class NumericUtils
{

    enum NumberType
    {
        INTEGER,FLOAT,LONG,DOUBLE,BIGDECIMAL,SHORT,BIGINTEGER
    }
    
    private static Map<Class, NumberType> numberTypes = new HashMap<Class, NumericUtils.NumberType>();
    static
    {
        numberTypes.put(int.class, NumberType.INTEGER);
        numberTypes.put(Integer.class, NumberType.INTEGER);
        numberTypes.put(float.class, NumberType.FLOAT);
        numberTypes.put(Float.class, NumberType.FLOAT);
        numberTypes.put(long.class, NumberType.LONG);
        numberTypes.put(Long.class, NumberType.LONG);
        numberTypes.put(double.class, NumberType.DOUBLE);
        numberTypes.put(Double.class, NumberType.DOUBLE);
        numberTypes.put(BigDecimal.class, NumberType.BIGDECIMAL);
        numberTypes.put(short.class, NumberType.SHORT);
        numberTypes.put(Short.class, NumberType.SHORT);
        numberTypes.put(BigInteger.class, NumberType.BIGINTEGER);
    }
    
    /**
     *  Check if zero
     * @param value        value string
     * @param valueClazz   value class 
     * @return             
     */
    public static final boolean checkIfZero(String value, Class valueClazz)
    {
        boolean returnValue=false;
        if(value != null && NumberUtils.isNumber(value) && numberTypes.get(valueClazz) != null)
        {
            switch (numberTypes.get(valueClazz))
            {
            
            case INTEGER:
                returnValue = Integer.parseInt(value) == (NumberUtils.INTEGER_ZERO);
                break;

            case FLOAT:
                returnValue = Float.parseFloat(value) == (NumberUtils.FLOAT_ZERO);
                break;

            case LONG:
                returnValue = Long.parseLong(value) == (NumberUtils.LONG_ZERO);
                break;

            case BIGDECIMAL:
                returnValue = new BigDecimal(value) == (BigDecimal.ZERO);
                break;

            case BIGINTEGER:
                returnValue = new BigInteger(value) == (BigInteger.ZERO);
                break;
            
            case SHORT:
                returnValue = new Short(value) == (NumberUtils.SHORT_ZERO);
                break;
            }
        }
        
        return returnValue;
    }
}

@@ -2714,16 +2714,145 @@
 ECIMAL:%0A
+                // Note: cannot use 'equals' here - it would require both BigDecimals to have%0A                // the same scale.%0A
         
@@ -2869,24 +2869,25 @@
 turnValue = 
+(
 new BigDecim
@@ -2891,28 +2891,35 @@
 cimal(value)
- == 
+).compareTo
 (BigDecimal.
@@ -2919,24 +2919,29 @@
 ecimal.ZERO)
+ == 0
 ;%0A          
@@ -3009,24 +3009,25 @@
 turnValue = 
+(
 new BigInteg
@@ -3035,20 +3035,24 @@
 r(value)
- == 
+).equals
 (BigInte
@@ -3152,16 +3152,17 @@
 Value = 
+(
 new Shor
@@ -3161,37 +3161,50 @@
 new Short(value)
+).shortValue()
  == 
-(
 NumberUtils.SHOR
@@ -3205,24 +3205,36 @@
 s.SHORT_ZERO
+.shortValue(
 );%0A         

Fix lgtm.com alerts: unwanted reference equality test