com.mizar.commons.collections
Class AccumulatorMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by com.mizar.commons.collections.AccumulatorMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>

public class AccumulatorMap<K,V>
extends java.util.HashMap<K,V>
implements java.util.Map<K,V>, java.lang.Cloneable, java.io.Serializable

The class extends the standard java.util.HashMap adding methods for sort, increment, and decrement entries.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
AccumulatorMap()
          Constructs an empty AccumulatorMap with the default initial capacity (16) and the default load factor (0.75).
AccumulatorMap(int initialCapacity)
          Constructs an empty AccumulatorMap with the specified initial capacity and the default load factor (0.75).
AccumulatorMap(int initialCapacity, float loadFactor)
          Constructs an empty AccumulatorMap with the specified initial capacity and load factor.
AccumulatorMap(java.util.Map<? extends K,? extends V> m)
          Constructs a new AccumulatorMap with the same mappings as the specified Map.
AccumulatorMap(java.util.Map<? extends K,? extends V> m, java.lang.Number value)
          Constructs a new AccumulatorMap with the same keys as the specified Map but all values are set to value The AccumulatorMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.
 
Method Summary
 java.lang.Number decrement(java.lang.Object key)
           
 java.lang.Number increment(java.lang.Object key)
          The method will ignore any Map that does not have a corresponding entry for key.
 java.lang.Number increment(java.lang.Object key, java.lang.Number value)
          The method is null safe for the value associated with this Map's key entry.
 java.lang.Number max(java.lang.Object key, java.lang.Number value)
           
 java.lang.Number min(java.lang.Object key, java.lang.Number value)
           
 java.lang.Number multiply(java.lang.Object key, java.lang.Number value)
          The method is null safe for the value associated with this Map's key entry.
 V put(java.lang.Object key, java.lang.Number value)
          The method overrides HashMap.put(K,V) by limiting V to subclasses of Number Associates the specified numeric value with the specified key in this map.
 AccumulatorMap<? extends K,? extends V> sort()
          The method does not sort this map.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Constructor Detail

AccumulatorMap

public AccumulatorMap()
Constructs an empty AccumulatorMap with the default initial capacity (16) and the default load factor (0.75).


AccumulatorMap

public AccumulatorMap(int initialCapacity,
                      float loadFactor)
Constructs an empty AccumulatorMap with the specified initial capacity and load factor.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.

AccumulatorMap

public AccumulatorMap(int initialCapacity)
Constructs an empty AccumulatorMap with the specified initial capacity and the default load factor (0.75).

Parameters:
initialCapacity - the initial capacity.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative.

AccumulatorMap

public AccumulatorMap(java.util.Map<? extends K,? extends V> m)
Constructs a new AccumulatorMap with the same mappings as the specified Map. The AccumulatorMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.

Parameters:
m - the map whose mappings are to be placed in this map.
Throws:
java.lang.NullPointerException - if the specified map is null.

AccumulatorMap

public AccumulatorMap(java.util.Map<? extends K,? extends V> m,
                      java.lang.Number value)
Constructs a new AccumulatorMap with the same keys as the specified Map but all values are set to value The AccumulatorMap is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.

Parameters:
m - the map whose mappings are to be placed in this map.
Throws:
java.lang.NullPointerException - if the specified map is null.
Method Detail

put

public V put(java.lang.Object key,
             java.lang.Number value)
The method overrides HashMap.put(K,V) by limiting V to subclasses of Number Associates the specified numeric value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.

multiply

public java.lang.Number multiply(java.lang.Object key,
                                 java.lang.Number value)
The method is null safe for the value associated with this Map's key entry. If this.get(key) returns null the key entry is set to value.

Parameters:
key - The key whose presence in this map is to be incremented by value
value - The value to multiply this map's key value by
Returns:
the new value stored in this map's key value.

increment

public java.lang.Number increment(java.lang.Object key,
                                  java.lang.Number value)
The method is null safe for the value associated with this Map's key entry. If this.get(key) returns null the key entry is set to value.

Parameters:
key - The key whose presence in this map is to be incremented by value
value - The value to increment this map's key value by
Returns:
the incremented value stored in this map's key value.

increment

public java.lang.Number increment(java.lang.Object key)
The method will ignore any Map that does not have a corresponding entry for key. The data type is taken from the existing value associated with key, which must be a subclass of Number.

Parameters:
key - The key whose presence in this map is to be incremented by 1
Returns:
the incremented value stored in this map's key value.
See Also:
increment(Object,Number)

decrement

public java.lang.Number decrement(java.lang.Object key)
Parameters:
key - The key whose presence in this map is to be decremented by 1
Returns:
the incremented value stored in this map's key value.

min

public java.lang.Number min(java.lang.Object key,
                            java.lang.Number value)
Parameters:
key - The key whose presence in this map is to set to the minimum of value and the existing value
value - The value to compare to this map's key value
Returns:
the new value stored in this map's key value.

max

public java.lang.Number max(java.lang.Object key,
                            java.lang.Number value)
Parameters:
key - The key whose presence in this map is to set to the maximum of value and the existing value
value - The value to compare to this map's key value
Returns:
the new value stored in this map's key value.

sort

public AccumulatorMap<? extends K,? extends V> sort()
The method does not sort this map. You must redirect the output of this method to the map to update it.

Example:

 map = map.sort();
 

Returns:
a sorted version of this map