Java BigDecimal Add addToBigDecimalInMap(Map theMap, K mapKey, BigDecimal addNumber)

Here you can find the source of addToBigDecimalInMap(Map theMap, K mapKey, BigDecimal addNumber)

Description

Assuming theMap not null; if null will throw a NullPointerException

License

Open Source License

Declaration

public static <K> BigDecimal addToBigDecimalInMap(Map<K, Object> theMap, K mapKey, BigDecimal addNumber) 

Method Source Code

//package com.java2s;
/*/* w w  w  . ja  v a 2 s. c  om*/
 * Copyright (C) 2003-2011 eXo Platform SAS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.BigDecimal;

import java.util.Map;

public class Main {
    public static final BigDecimal ZERO_BD = BigDecimal.ZERO;

    /**
     * Assuming theMap not null; if null will throw a NullPointerException
     */
    public static <K> BigDecimal addToBigDecimalInMap(Map<K, Object> theMap, K mapKey, BigDecimal addNumber) {
        Object currentNumberObj = theMap.get(mapKey);
        BigDecimal currentNumber = null;
        if (currentNumberObj == null) {
            currentNumber = ZERO_BD;
        } else if (currentNumberObj instanceof BigDecimal) {
            currentNumber = (BigDecimal) currentNumberObj;
        } else if (currentNumberObj instanceof Double) {
            currentNumber = new BigDecimal(((Double) currentNumberObj).doubleValue());
        } else if (currentNumberObj instanceof Long) {
            currentNumber = new BigDecimal(((Long) currentNumberObj).longValue());
        } else {
            throw new IllegalArgumentException("In addToBigDecimalInMap found a Map value of a type not supported: "
                    + currentNumberObj.getClass().getName());
        }

        if (addNumber == null || ZERO_BD.compareTo(addNumber) == 0) {
            return currentNumber;
        }
        currentNumber = currentNumber.add(addNumber);
        theMap.put(mapKey, currentNumber);
        return currentNumber;
    }
}

Related

  1. addBigDecimals(String value1, String value2)
  2. addBigDecimalsInMap(Map baseMap, Map addMap)
  3. addPercent(BigDecimal price, double amount)
  4. addQtde(BigDecimal val1, BigDecimal val2)
  5. addSalePrice(Map complexSalePrice, BigDecimal saleprice)
  6. addToBigDecimalInMap(Object key, BigDecimal value, Map theMap)
  7. addUlp(BigDecimal x)
  8. addVAT(int priceInCents, BigDecimal vat)
  9. addVatAmount(BigDecimal percentage, BigDecimal amount)