Parsing Engine

danbikel.util
Class HashMapPrimitive<K>

java.lang.Object
  extended by java.util.AbstractMap<K,Object>
      extended by danbikel.util.AbstractMapToPrimitive<K>
          extended by danbikel.util.HashMapPrimitive<K>
All Implemented Interfaces:
FlexibleMap<K,Object>, MapToPrimitive<K>, Serializable, Map<K,Object>
Direct Known Subclasses:
HashMapDouble, HashMapInt

public abstract class HashMapPrimitive<K>
extends AbstractMapToPrimitive<K>
implements FlexibleMap<K,Object>, Serializable

A home-grown hash map from objects to indexed sequences of primitives. Concrete implementations need only provide a map entry implementation and provide a concrete implementation of the getNewEntry(int, Object, HashMapPrimitive.Entry) method.

See Also:
Serialized Form

Nested Class Summary
static class HashMapPrimitive.Entry<K>
          A still-abstract extension of the AbstractMapToPrimitive.Entry that adds a next pointer and an int to cache the hash value of the key held by this entry.
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
protected static int defaultInitialCapacity
          The default initial capacity, 11.
protected static float defaultLoadFactor
          The default load factor, 0.75f.
protected static int hashCodeBitmask
          The hash code bit mask, 0x7fffffff.
protected static BigInteger maxCapacity
          The maximum capacity (number of buckets) of this hash map, Integer.MAX_VALUE.
 
Constructor Summary
HashMapPrimitive()
          Constructs a new map from objects to primitive values, using the default initial capacity and the default load factor.
HashMapPrimitive(int initialCapacity)
          Constructs a new map from objects to primitive values, using the default load factory
HashMapPrimitive(int initialCapacity, float loadFactor)
          Constructs a new map from objects to primitive values.
HashMapPrimitive(Map<? extends K,Object> map)
           
 
Method Summary
 void add(K key, int index, byte addend)
          Adds the specified addend to the byte value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index.
 void add(K key, int index, double addend)
          Adds the specified addend to the double value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index.
 void add(K key, int index, float addend)
          Adds the specified addend to the float value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index.
 void add(K key, int index, int addend)
          Adds the specified addend to the int value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index.
 void add(K key, int index, long addend)
          Adds the specified addend to the long value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index.
 void add(K key, int index, short addend)
          Adds the specified addend to the short value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index.
protected  void addEntryMRU(HashMapPrimitive.Entry entry)
          Adds the specified entry to the beginning of the singly-linked list at its bucket index (indicating it is the most-recently used entry).
 boolean containsKey(Object key)
           
 Set<Map.Entry<K,Object>> entrySet()
           
 Object get(K key, int hashCode)
          Returns the value for the specified key.
 int getCapacity()
          Gets the capacity of this map (optional operation).
 MapToPrimitive.Entry<K> getEntry(K key)
          Gets the map entry associated with the specified key, or null if this map does not contain such a mapping.
 MapToPrimitive.Entry<K> getEntry(K key, int hashCode)
          Returns the entry associated with the specified key, or null if no such entry exists.
 MapToPrimitive.Entry<K> getEntryMRU(K key)
          Gets the map entry for the specified key and, as a side-effect, puts the map entry at the front of the bucket list, indicating that it is the most-recently used entry (useful for caches implementing a bucket-LRU replacement scheme).
 MapToPrimitive.Entry<K> getEntryMRU(K key, int hashCode)
           
 float getLoadFactor()
          Gets the load factor of this map (optional operation).
protected abstract  HashMapPrimitive.Entry<K> getNewEntry(int hash, K key, HashMapPrimitive.Entry<K> next)
          The method used when constructing map entries for concrete subclasses.
protected  MapToPrimitive.Entry<K> getOrCreateEntry(K key)
          Returns the entry associated with the specified key, or, if no such entry exists, creates one and returns it.
 String getStats()
          Returns a string that represents the useful statistics of this map (useful/necessary in the case of hash maps, where it is desirable to know the number of collisions and average and maximum buckets sizes).
 byte put(K key, int index, byte value)
           
 char put(K key, int index, char value)
           
 double put(K key, int index, double value)
           
 float put(K key, int index, float value)
           
 int put(K key, int index, int value)
           
 long put(K key, int index, long value)
           
 short put(K key, int index, short value)
           
 Object put(K key, Object value)
          Throws an UnsupportedOperationException.
 Object remove(Object key)
          Removes the entry for the specified key.
protected  MapToPrimitive.Entry removeLRU(int hashCode)
          Removes the last entry at the specified bucket index, if that bucket contains at least one entry.
protected  MapToPrimitive.Entry removeLRU(K key)
           
 void removeRandom(int bucketIndex)
          Removes a random entry from the bucket at the specified index (optional operation).
 
Methods inherited from class danbikel.util.AbstractMapToPrimitive
add, add, add, add, add, add, put, put, put, put, put, put, put, removeRandom, toString
 
Methods inherited from class java.util.AbstractMap
clear, clone, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, size, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, size, values
 

Field Detail

defaultLoadFactor

protected static final float defaultLoadFactor
The default load factor, 0.75f.

See Also:
Constant Field Values

defaultInitialCapacity

protected static final int defaultInitialCapacity
The default initial capacity, 11.

See Also:
Constant Field Values

hashCodeBitmask

protected static final int hashCodeBitmask
The hash code bit mask, 0x7fffffff.

See Also:
Constant Field Values

maxCapacity

protected static final BigInteger maxCapacity
The maximum capacity (number of buckets) of this hash map, Integer.MAX_VALUE.

Constructor Detail

HashMapPrimitive

public HashMapPrimitive()
Constructs a new map from objects to primitive values, using the default initial capacity and the default load factor.

See Also:
defaultInitialCapacity, defaultLoadFactor

HashMapPrimitive

public HashMapPrimitive(int initialCapacity)
Constructs a new map from objects to primitive values, using the default load factory

Parameters:
initialCapacity - the initial capacity of this map

HashMapPrimitive

public HashMapPrimitive(int initialCapacity,
                        float loadFactor)
Constructs a new map from objects to primitive values.

Parameters:
initialCapacity - the initial capacity of this map
loadFactor - the load factor of this map

HashMapPrimitive

public HashMapPrimitive(Map<? extends K,Object> map)
Method Detail

getNewEntry

protected abstract HashMapPrimitive.Entry<K> getNewEntry(int hash,
                                                         K key,
                                                         HashMapPrimitive.Entry<K> next)
The method used when constructing map entries for concrete subclasses.

Parameters:
hash - the hash value of the key of the new entry
key - the key of the new entry
next - the next pointer of the new entry’s singly-linked list
Returns:
the newly-constructed entry

remove

public Object remove(Object key)
Removes the entry for the specified key. Unlike the API specified by Map.remove(Object), this method does not return the value associated with the key, because that value could be one or more primitive values.

Specified by:
remove in interface Map<K,Object>
Overrides:
remove in class AbstractMap<K,Object>
Parameters:
key - the key of the entry to remove
Returns:
null

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,Object>
Overrides:
containsKey in class AbstractMap<K,Object>

put

public Object put(K key,
                  Object value)
Throws an UnsupportedOperationException.

Specified by:
put in interface Map<K,Object>
Overrides:
put in class AbstractMap<K,Object>
Parameters:
key - ignored
value - ignored
Returns:
nothing
Throws:
UnsupportedOperationException - under all circumstances

getEntry

public MapToPrimitive.Entry<K> getEntry(K key)
Gets the map entry associated with the specified key, or null if this map does not contain such a mapping.

Specified by:
getEntry in interface MapToPrimitive<K>
Specified by:
getEntry in class AbstractMapToPrimitive<K>
Parameters:
key - the key for which to look up a map entry
Returns:
the map entry for the specified key, or null if no such mapping exists in this map

getOrCreateEntry

protected MapToPrimitive.Entry<K> getOrCreateEntry(K key)
Returns the entry associated with the specified key, or, if no such entry exists, creates one and returns it.

Parameters:
key - the key for which to return or create a map entry
Returns:
the entry associated with the specified key, or, if no such entry exists, creates one and returns it

getEntry

public MapToPrimitive.Entry<K> getEntry(K key,
                                        int hashCode)
Returns the entry associated with the specified key, or null if no such entry exists. It is an error to invoke this method with a hash code that is not equal to key.hashCode().

Specified by:
getEntry in interface MapToPrimitive<K>
Parameters:
key - the key for which to retrieve its map entry
hashCode - the hash code for the specified key
Returns:
the entry associated with the specified key, or null if no such entry exists

getEntryMRU

public MapToPrimitive.Entry<K> getEntryMRU(K key)
Gets the map entry for the specified key and, as a side-effect, puts the map entry at the front of the bucket list, indicating that it is the most-recently used entry (useful for caches implementing a bucket-LRU replacement scheme). This is an optional operation.

Specified by:
getEntryMRU in interface MapToPrimitive<K>
Specified by:
getEntryMRU in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose map entry is to be retrieved and made the MRU in its bucket inside the hash map
Returns:
the map entry for the specified key and, as a side-effect, puts the map entry at the front of the bucket list, indicating that it is the most-recently used entry
Throws:
UnsupportedOperationException - if this map is not a hash map

getEntryMRU

public MapToPrimitive.Entry<K> getEntryMRU(K key,
                                           int hashCode)
Specified by:
getEntryMRU in interface MapToPrimitive<K>

addEntryMRU

protected void addEntryMRU(HashMapPrimitive.Entry entry)
Adds the specified entry to the beginning of the singly-linked list at its bucket index (indicating it is the most-recently used entry). Warning: This method does not check whether the specified entry's key already is in the map; subclasses should only invoke this method when it has already been ascertained that the entry’s key is not in the map.

Parameters:
entry - the entry to add as most-recently used in its bucket

removeLRU

protected MapToPrimitive.Entry removeLRU(K key)

removeLRU

protected MapToPrimitive.Entry removeLRU(int hashCode)
Removes the last entry at the specified bucket index, if that bucket contains at least one entry.

Parameters:
hashCode - the hashCode of an object whose bucket is to be emptied of its least-recently-used entry
Returns:
the removed entry, or null if no entry was removed

removeRandom

public void removeRandom(int bucketIndex)
Removes a random entry from the bucket at the specified index (optional operation).

Specified by:
removeRandom in interface MapToPrimitive<K>
Specified by:
removeRandom in class AbstractMapToPrimitive<K>
Parameters:
bucketIndex - the index of the bucket from which to remove an element
Throws:
IllegalArgumentException - if
0 <= bucketIndex <
                                       

getCapacity()

is false
UnsupportedOperationException - if this map is not a hash map

getCapacity

public int getCapacity()
Description copied from interface: FlexibleMap
Gets the capacity of this map (optional operation).

Specified by:
getCapacity in interface FlexibleMap<K,Object>
Returns:
the capacity of this map (the number of buckets, in the case of a hash map)

getLoadFactor

public float getLoadFactor()
Description copied from interface: FlexibleMap
Gets the load factor of this map (optional operation).

Specified by:
getLoadFactor in interface FlexibleMap<K,Object>
Returns:
the load factor of this map

getStats

public String getStats()
Description copied from interface: FlexibleMap
Returns a string that represents the useful statistics of this map (useful/necessary in the case of hash maps, where it is desirable to know the number of collisions and average and maximum buckets sizes). The format of the string is up to the implementor.

Specified by:
getStats in interface FlexibleMap<K,Object>

get

public Object get(K key,
                  int hashCode)
Description copied from interface: FlexibleMap
Returns the value for the specified key. If the specified hash code is not the value of key.hashCode(), the behavior of thiscollins method is not defined.

Specified by:
get in interface FlexibleMap<K,Object>
Parameters:
key - the key whose value is to be looked up
hashCode - the value of key.hashCode()

put

public byte put(K key,
                int index,
                byte value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

add

public void add(K key,
                int index,
                byte addend)
Description copied from class: AbstractMapToPrimitive
Adds the specified addend to the byte value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index. If this map maps keys to multiple byte values, then the other bytes will be set to their default instance-variable value, 0.

Specified by:
add in interface MapToPrimitive<K>
Overrides:
add in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose byte value is to be incremented, or for which a mapping is to be added to the specified addend
index - the index of the byte value to be incremented
addend - the amount by which to increment the byte value for the specified key

put

public char put(K key,
                int index,
                char value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

put

public short put(K key,
                 int index,
                 short value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

add

public void add(K key,
                int index,
                short addend)
Description copied from class: AbstractMapToPrimitive
Adds the specified addend to the short value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index. If this map maps keys to multiple short values, then the other shorts will be set to their default instance-variable value, 0.

Specified by:
add in interface MapToPrimitive<K>
Overrides:
add in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose short value is to be incremented, or for which a mapping is to be added to the specified addend
index - the index of the short value to be incremented
addend - the amount by which to increment the short value for the specified key

put

public int put(K key,
               int index,
               int value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

add

public void add(K key,
                int index,
                int addend)
Description copied from class: AbstractMapToPrimitive
Adds the specified addend to the int value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index. If this map maps keys to multiple int values, then the other ints will be set to their default instance-variable value, 0.

Specified by:
add in interface MapToPrimitive<K>
Overrides:
add in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose int value is to be incremented, or for which a mapping is to be added to the specified addend
index - the index of the int value to be incremented
addend - the amount by which to increment the int value for the specified key

put

public long put(K key,
                int index,
                long value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

add

public void add(K key,
                int index,
                long addend)
Description copied from class: AbstractMapToPrimitive
Adds the specified addend to the long value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index. If this map maps keys to multiple long values, then the other longs will be set to their default instance-variable value, 0.

Specified by:
add in interface MapToPrimitive<K>
Overrides:
add in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose long value is to be incremented, or for which a mapping is to be added to the specified addend
index - the index of the long value to be incremented
addend - the amount by which to increment the long value for the specified key

put

public float put(K key,
                 int index,
                 float value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

add

public void add(K key,
                int index,
                float addend)
Description copied from class: AbstractMapToPrimitive
Adds the specified addend to the float value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index. If this map maps keys to multiple float values, then the other floats will be set to their default instance-variable value, 0.

Specified by:
add in interface MapToPrimitive<K>
Overrides:
add in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose float value is to be incremented, or for which a mapping is to be added to the specified addend
index - the index of the float value to be incremented
addend - the amount by which to increment the float value for the specified key

put

public double put(K key,
                  int index,
                  double value)
Specified by:
put in interface MapToPrimitive<K>
Overrides:
put in class AbstractMapToPrimitive<K>

add

public void add(K key,
                int index,
                double addend)
Description copied from class: AbstractMapToPrimitive
Adds the specified addend to the double value at the specified index associated with the specified key, or if no mapping previously existed for the specified key, then this method adds a new map entry mapping the key to the specified addend at the specified index. If this map maps keys to multiple double values, then the other doubles will be set to their default instance-variable value, 0.

Specified by:
add in interface MapToPrimitive<K>
Overrides:
add in class AbstractMapToPrimitive<K>
Parameters:
key - the key whose double value is to be incremented, or for which a mapping is to be added to the specified addend
index - the index of the double value to be incremented
addend - the amount by which to increment the double value for the specified key

entrySet

public Set<Map.Entry<K,Object>> entrySet()
Specified by:
entrySet in interface Map<K,Object>
Specified by:
entrySet in class AbstractMap<K,Object>

Parsing Engine

Author: Dan Bikel.