Class CountMap<T>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<T,Integer>
com.blackrook.rookscript.struct.CountMap<T>
Type Parameters:
T - the type to count.
All Implemented Interfaces:
Serializable, Cloneable, Map<T,Integer>

public class CountMap<T> extends HashMap<T,Integer>
A special HashMap that increments or decrements a value.
Author:
Matthew Tropiano
See Also:
  • Constructor Details

    • CountMap

      public CountMap()
      Creates a new map with default capacity and load factor.
    • CountMap

      public CountMap(int initialCapacity)
      Creates a new map with specific capacity and default load factor.
      Parameters:
      initialCapacity - initial table capacity.
    • CountMap

      public CountMap(int initialCapacity, float loadFactor)
      Creates a new map with specific capacity and load factor.
      Parameters:
      initialCapacity - initial table capacity.
      loadFactor - load factor before re-hash.
  • Method Details

    • amount

      public int amount(T key)
      Gets the current value corresponding to the key. If the key does not exist, this returns 0.
      Parameters:
      key - the key.
      Returns:
      the current total for the key.
    • give

      public int give(T key, int amount)
      Adds to a key's value. If the value becomes 0, it is removed.
      Parameters:
      key - the key.
      amount - the amount to add. If negative, calls take(key, -amount).
      Returns:
      the new total for the key.
    • take

      public int take(T key, int amount)
      Subtracts from a key's value. You can't remove more than the value (it will be set to 0). If the value becomes 0, it is removed.
      Parameters:
      key - the key.
      amount - the amount to remove. If negative, calls give(key, -amount).
      Returns:
      the new total for the key.