Package com.blackrook.rookscript.struct
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>
A special HashMap that increments or decrements a value.
- Author:
- Matthew Tropiano
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
-
Method Summary
Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
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
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
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, callstake(key, -amount)
.- Returns:
- the new total for the key.
-
take
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, callsgive(key, -amount)
.- Returns:
- the new total for the key.
-