org.apache.myfaces.trinidad.util
Class ArrayMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.apache.myfaces.trinidad.util.ArrayMap<K,V>
All Implemented Interfaces:
Cloneable, Map<K,V>
Direct Known Subclasses:
PropertyArrayMap

public class ArrayMap<K,V>
extends AbstractMap<K,V>
implements Cloneable

A Map implementation that stores its contents in a single array. This approach is significantly faster for small sets of data than the use of a HashMap or Hashtable, though potentially much slower for very large sets.

ArrayMap is optimized for many-reads-few-write. In particular, it reallocates its array on any insertion or deletion.

ArrayMap also includes a series of static methods for managing the Object array. These may be used in place of instantiating an ArrayMap for clients that don't need a Map implementation. Clients using these methods must be careful to store the returned Object array on any mutator method. They also must provide their own synchronization, if needed. When using these static methods, clients can opt to search for objects by identity (via getByIdentity()) instead of equality, while the static get() method will try identity before equality. This latter approach is extremely fast but still safe for retrieval of Strings that have all been interned, especially if misses are infrequent (since misses do require a scan using Object.equals()). It's worth remembering that String constants are always interned, as required by the language specification.

Version:
$Name: $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/util/ArrayMap.java#0 $) $Date: 10-nov-2005.19:08:36 $

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
ArrayMap()
          Creates an empty ArrayMap, preallocating nothing.
ArrayMap(int size)
          Creates an ArrayMap, preallocating for a certain size.
ArrayMap(int size, int increment)
          Creates an ArrayMap, preallocating for a certain size.
 
Method Summary
 void clear()
          Removes all elements from the ArrayMap.
 Object clone()
          Clones the map.
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<K,V>> entrySet()
           
 V get(Object key)
           
static Object get(Object[] array, Object key)
          Gets the object stored with the given key.
static Object getByEquality(Object[] array, Object key)
          Gets the object stored with the given key, using only object equality.
 V getByIdentity(Object key)
           
static Object getByIdentity(Object[] array, Object key)
          Gets the object stored with the given key, using only object identity.
 K getKey(int index)
          Returns the key at a specific index in the map.
static Iterator<Object> getKeys(Object[] array)
          Returns an Iterator of keys in the array.
 V getValue(int index)
          Returns the value at a specific index in the map.
static Iterator<Object> getValues(Object[] array)
          Returns an Iterator of values in the array.
 Iterator<K> keys()
          Returns an enumeration of the keys in this map.
 V put(K key, V value)
           
static Object[] put(Object[] array, Object key, Object value)
          Adds the key/value pair to the array, returning a new array if necessary.
 V remove(Object key)
           
static Object[] remove(Object[] array, Object key)
          Removes the value for the key from the array, returning a new array if necessary.
static Object[] remove(Object[] array, Object key, boolean reallocate)
          Removes the value for the key from the array, returning a new array if necessary.
 int size()
           
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArrayMap

public ArrayMap()
Creates an empty ArrayMap, preallocating nothing.


ArrayMap

public ArrayMap(int size)
Creates an ArrayMap, preallocating for a certain size.

Parameters:
size - the number of elements to pre-allocate for

ArrayMap

public ArrayMap(int size,
                int increment)
Creates an ArrayMap, preallocating for a certain size.

Parameters:
size - the number of elements to pre-allocate for
increment - the number of additional elements to allocate for when overruning
Method Detail

getKey

public K getKey(int index)
Returns the key at a specific index in the map.


getValue

public V getValue(int index)
Returns the value at a specific index in the map.


get

public static Object get(Object[] array,
                         Object key)
Gets the object stored with the given key. Scans first by object identity, then by object equality.


getByIdentity

public static Object getByIdentity(Object[] array,
                                   Object key)
Gets the object stored with the given key, using only object identity.


getByEquality

public static Object getByEquality(Object[] array,
                                   Object key)
Gets the object stored with the given key, using only object equality.


put

public static Object[] put(Object[] array,
                           Object key,
                           Object value)
Adds the key/value pair to the array, returning a new array if necessary.


remove

public static Object[] remove(Object[] array,
                              Object key)
Removes the value for the key from the array, returning a new array if necessary.


remove

public static Object[] remove(Object[] array,
                              Object key,
                              boolean reallocate)
Removes the value for the key from the array, returning a new array if necessary.


size

public int size()
Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMap<K,V>

containsKey

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

keys

public Iterator<K> keys()
Returns an enumeration of the keys in this map. the Iterator methods on the returned object to fetch the elements sequentially.


getKeys

public static Iterator<Object> getKeys(Object[] array)
Returns an Iterator of keys in the array.


getValues

public static Iterator<Object> getValues(Object[] array)
Returns an Iterator of values in the array.


clone

public Object clone()
Clones the map.

Overrides:
clone in class AbstractMap<K,V>

entrySet

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

get

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

getByIdentity

public V getByIdentity(Object key)

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

remove

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

clear

public void clear()
Removes all elements from the ArrayMap.

Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>


Copyright © 2001-2012 The Apache Software Foundation. All Rights Reserved.