All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.clanie.collections.NumberMap Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2009, Claus Nielsen, [email protected]
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package dk.clanie.collections;

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

/**
 * Number Map.
 * 

* An HashMap which stores Numbers and lets you add to (and * subtract from) the Numbers stored in it without retrieving them first. *

* This means that you can replace code like this:

if (map.containsKey(key)) {
 *     Integer sum = map.get(key);
 *     sum = sum.add(value);
 *     map.put(key, sum);
 *} else {
 *     map.put(value);
 *}

with:

map.add(key, value);
* * @author Claus Nielsen */ @SuppressWarnings("serial") public abstract class NumberMap extends HashMap { /** * Creates a NumberMap for instances of a concrete subclass of Number. * * @param key type * @param element type * @param elementType * @return NumberMap<K, E> */ @SuppressWarnings("unchecked") public static NumberMap newNumberMap(Class elementType) { if (elementType == BigDecimal.class) return (NumberMap) newBigDecimalMap(); if (elementType == BigInteger.class) return (NumberMap) newBigIntegerMap(); if (elementType == Byte.class) return (NumberMap) newByteMap(); if (elementType == Long.class) return (NumberMap) newLongMap(); if (elementType == Double.class) return (NumberMap) newDoubleMap(); if (elementType == Float.class) return (NumberMap) newFloatMap(); if (elementType == Integer.class) return (NumberMap) newIntegerMap(); if (elementType == Short.class) return (NumberMap) newShortMap(); else throw new UnsupportedOperationException(); } /** * Creates a NumberMap for BigDecimals. * * @param * @return NumberMap<K, BigDecimal> */ public static NumberMap newBigDecimalMap() { return new NumberMap() { @Override public void add(K key, BigDecimal addend) { put(key, containsKey(key) ? get(key).add(addend) : addend); } @Override public void sub(K key, BigDecimal subtrahend) { put(key, (containsKey(key) ? get(key) : BigDecimal.ZERO).subtract(subtrahend)); } }; } /** * Creates a NumberMap for BigIntegers. * * @param * @return NumberMap<K, BigInteger> */ public static NumberMap newBigIntegerMap() { return new NumberMap() { @Override public void add(K key, BigInteger addend) { put(key, containsKey(key) ? get(key).add(addend) : addend); } @Override public void sub(K key, BigInteger subtrahend) { put(key, (containsKey(key) ? get(key) : BigInteger.ZERO).subtract(subtrahend)); } }; } /** * Creates a NumberMap for Bytes. * * @param * @return NumberMap<K, Byte> */ public static NumberMap newByteMap() { return new NumberMap() { @Override public void add(K key, Byte addend) { put(key, (byte)(containsKey(key) ? get(key) + addend : addend)); } @Override public void sub(K key, Byte subtrahend) { put(key, (byte)((containsKey(key) ? get(key) : 0) - subtrahend)); } }; } /** * Creates a NumberMap for Doubles. * * @param * @return NumberMap<K, Double> */ public static NumberMap newDoubleMap() { return new NumberMap() { @Override public void add(K key, Double addend) { put(key, containsKey(key) ? (get(key) + addend) : addend); } @Override public void sub(K key, Double subtrahend) { put(key, (containsKey(key) ? get(key) : 0d) - subtrahend); } }; } /** * Creates a NumberMap for Floats. * * @param * @return NumberMap<K, Float> */ public static NumberMap newFloatMap() { return new NumberMap() { @Override public void add(K key, Float addend) { put(key, containsKey(key) ? (get(key) + addend) : addend); } @Override public void sub(K key, Float subtrahend) { put(key, (containsKey(key) ? get(key) : 0f) - subtrahend); } }; } /** * Creates a NumberMap for Integers. * * @param * @return NumberMap<K, Integer> */ public static NumberMap newIntegerMap() { return new NumberMap() { @Override public void add(K key, Integer addend) { put(key, containsKey(key) ? (get(key) + addend) : addend); } @Override public void sub(K key, Integer subtrahend) { put(key, (containsKey(key) ? get(key) : 0) - subtrahend); } }; } /** * Creates a NumberMap for Longs. * * @param * @return NumberMap */ public static NumberMap newLongMap() { return new NumberMap() { @Override public void add(K key, Long addend) { put(key, containsKey(key) ? (get(key) + addend) : addend); } @Override public void sub(K key, Long subtrahend) { put(key, (containsKey(key) ? get(key) : 0l) - subtrahend); } }; } /** * Creates a NumberMap for Shorts. * * @param * @return NumberMap */ public static NumberMap newShortMap() { return new NumberMap() { @Override public void add(K key, Short addend) { put(key, containsKey(key) ? (short) (get(key) + addend) : addend); } @Override public void sub(K key, Short subtrahend) { put(key, (short)((containsKey(key) ? get(key) : 0) - subtrahend)); } }; } /** * Add to the Number specified. * * If the Map dosn't contain an entry with the specified key it is created. * * @param key * @param addend */ public abstract void add(K key, E addend); /** * Subtract to the Number specified. * * If the Map dosn't contain an entry with the specified key it is created. * * @param key * @param addend */ public abstract void sub(K key, E addend); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy