Here you can find the source of addToBigDecimalInMap(Map
public static <K> BigDecimal addToBigDecimalInMap(Map<K, Object> theMap, K mapKey, BigDecimal addNumber)
//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; } }