Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.axis.collections
Class SequencedHashMap  view SequencedHashMap download SequencedHashMap.java

java.lang.Object
  extended byorg.apache.axis.collections.SequencedHashMap
All Implemented Interfaces:
java.lang.Cloneable, java.io.Externalizable, java.util.Map, java.io.Serializable
Direct Known Subclasses:
LRUMap

public class SequencedHashMap
extends java.lang.Object
implements java.util.Map, java.lang.Cloneable, java.io.Externalizable

A map of objects whose mapping entries are sequenced based on the order in which they were added. This data structure has fast O(1) search time, deletion time, and insertion time.

Although this map is sequenced, it cannot implement java.util.List because of incompatible interface definitions. The remove methods in List and Map have different return values (see: List.remove(Object)>List.remove(Object) 55 and Map.remove(Object)>Map.remove(Object) 55 ).

This class is not thread safe. When a thread safe implementation is required, use Collections.synchronizedMap(Map)>Collections.synchronizedMap(Map) 55 as it is documented, or use explicit synchronization controls.

Since:
Commons Collections 2.0

Nested Class Summary
private static class SequencedHashMap.Entry
          java.util.Map.Entry that doubles as a node in the linked list of sequenced mappings.
private  class SequencedHashMap.OrderedIterator
           
 
Field Summary
private  java.util.HashMap entries
          Map of keys to entries
private static int ENTRY
           
private static int KEY
           
private  long modCount
          Holds the number of modifications that have occurred to the map, excluding modifications made through a collection view's iterator (e.g.
private static int REMOVED_MASK
           
private  SequencedHashMap.Entry sentinel
          Sentinel used to hold the head and tail of the list of entries.
private static long serialVersionUID
           
private static int VALUE
           
 
Constructor Summary
SequencedHashMap()
          Construct a new sequenced hash map with default initial size and load factor.
SequencedHashMap(int initialSize)
          Construct a new sequenced hash map with the specified initial size and default load factor.
SequencedHashMap(int initialSize, float loadFactor)
          Construct a new sequenced hash map with the specified initial size and load factor.
SequencedHashMap(java.util.Map m)
          Construct a new sequenced hash map and add all the elements in the specified map.
 
Method Summary
 void clear()
          Implements Map.clear()>Map.clear() 55 .
 java.lang.Object clone()
          Creates a shallow copy of this object, preserving the internal structure by copying only references.
 boolean containsKey(java.lang.Object key)
          Implements Map.containsKey(Object)>Map.containsKey(Object) 55 .
 boolean containsValue(java.lang.Object value)
          Implements Map.containsValue(Object)>Map.containsValue(Object) 55 .
private static SequencedHashMap.Entry createSentinel()
          Construct an empty sentinel used to hold the head (sentinel.next) and the tail (sentinel.prev) of the list.
 java.util.Set entrySet()
          Implements Map.entrySet()>Map.entrySet() 55 .
 boolean equals(java.lang.Object obj)
          Implements Map.equals(Object)>Map.equals(Object) 55 .
 java.lang.Object get(int index)
          Gets the key at the specified index.
 java.lang.Object get(java.lang.Object o)
          Implements Map.get(Object)>Map.get(Object) 55 .
private  java.util.Map.Entry getEntry(int index)
          Returns the Map.Entry at the specified index
 java.util.Map.Entry getFirst()
          Return the entry for the "oldest" mapping.
 java.lang.Object getFirstKey()
          Return the key for the "oldest" mapping.
 java.lang.Object getFirstValue()
          Return the value for the "oldest" mapping.
 java.util.Map.Entry getLast()
          Return the entry for the "newest" mapping.
 java.lang.Object getLastKey()
          Return the key for the "newest" mapping.
 java.lang.Object getLastValue()
          Return the value for the "newest" mapping.
 java.lang.Object getValue(int index)
          Gets the value at the specified index.
 int hashCode()
          Implements Map.hashCode()>Map.hashCode() 55 .
 int indexOf(java.lang.Object key)
          Gets the index of the specified key.
private  void insertEntry(SequencedHashMap.Entry entry)
          Inserts a new internal entry to the tail of the linked list.
 boolean isEmpty()
          Implements Map.isEmpty()>Map.isEmpty() 55 .
 java.util.Iterator iterator()
          Gets an iterator over the keys.
 java.util.Set keySet()
          Implements Map.keySet()>Map.keySet() 55 .
 int lastIndexOf(java.lang.Object key)
          Gets the last index of the specified key.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Implements Map.put(Object, Object)>Map.put(Object, Object) 55 .
 void putAll(java.util.Map t)
          Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as per Map.putAll(Map)>Map.putAll(Map) 55 ).
 void readExternal(java.io.ObjectInput in)
          Deserializes this map from the given stream.
 java.lang.Object remove(int index)
          Removes the element at the specified index.
 java.lang.Object remove(java.lang.Object key)
          Implements Map.remove(Object)>Map.remove(Object) 55 .
private  void removeEntry(SequencedHashMap.Entry entry)
          Removes an internal entry from the linked list.
private  SequencedHashMap.Entry removeImpl(java.lang.Object key)
          Fully remove an entry from the map, returning the old entry or null if there was no such entry with the specified key.
 java.util.List sequence()
          Returns a List view of the keys rather than a set view.
 int size()
          Implements Map.size()>Map.size() 55 .
 java.lang.String toString()
          Provides a string representation of the entries within the map.
 java.util.Collection values()
          Implements Map.values()>Map.values() 55 .
 void writeExternal(java.io.ObjectOutput out)
          Serializes this map to the given stream.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

sentinel

private SequencedHashMap.Entry sentinel
Sentinel used to hold the head and tail of the list of entries.


entries

private java.util.HashMap entries
Map of keys to entries


modCount

private transient long modCount
Holds the number of modifications that have occurred to the map, excluding modifications made through a collection view's iterator (e.g. entrySet().iterator().remove()). This is used to create a fail-fast behavior with the iterators.


KEY

private static final int KEY
See Also:
Constant Field Values

VALUE

private static final int VALUE
See Also:
Constant Field Values

ENTRY

private static final int ENTRY
See Also:
Constant Field Values

REMOVED_MASK

private static final int REMOVED_MASK
See Also:
Constant Field Values

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

SequencedHashMap

public SequencedHashMap()
Construct a new sequenced hash map with default initial size and load factor.


SequencedHashMap

public SequencedHashMap(int initialSize)
Construct a new sequenced hash map with the specified initial size and default load factor.


SequencedHashMap

public SequencedHashMap(int initialSize,
                        float loadFactor)
Construct a new sequenced hash map with the specified initial size and load factor.


SequencedHashMap

public SequencedHashMap(java.util.Map m)
Construct a new sequenced hash map and add all the elements in the specified map. The order in which the mappings in the specified map are added is defined by putAll(Map) 55 .

Method Detail

createSentinel

private static final SequencedHashMap.Entry createSentinel()
Construct an empty sentinel used to hold the head (sentinel.next) and the tail (sentinel.prev) of the list. The sentinal has a null key and value.


removeEntry

private void removeEntry(SequencedHashMap.Entry entry)
Removes an internal entry from the linked list. This does not remove it from the underlying map.


insertEntry

private void insertEntry(SequencedHashMap.Entry entry)
Inserts a new internal entry to the tail of the linked list. This does not add the entry to the underlying map.


size

public int size()
Implements Map.size()>Map.size() 55 .

Specified by:
size in interface java.util.Map

isEmpty

public boolean isEmpty()
Implements Map.isEmpty()>Map.isEmpty() 55 .

Specified by:
isEmpty in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Implements Map.containsKey(Object)>Map.containsKey(Object) 55 .

Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Implements Map.containsValue(Object)>Map.containsValue(Object) 55 .

Specified by:
containsValue in interface java.util.Map

get

public java.lang.Object get(java.lang.Object o)
Implements Map.get(Object)>Map.get(Object) 55 .

Specified by:
get in interface java.util.Map

getFirst

public java.util.Map.Entry getFirst()
Return the entry for the "oldest" mapping. That is, return the Map.Entry for the key-value pair that was first put into the map when compared to all the other pairings in the map. This behavior is equivalent to using entrySet().iterator().next(), but this method provides an optimized implementation.


getFirstKey

public java.lang.Object getFirstKey()
Return the key for the "oldest" mapping. That is, return the key for the mapping that was first put into the map when compared to all the other objects in the map. This behavior is equivalent to using getFirst().getKey(), but this method provides a slightly optimized implementation.


getFirstValue

public java.lang.Object getFirstValue()
Return the value for the "oldest" mapping. That is, return the value for the mapping that was first put into the map when compared to all the other objects in the map. This behavior is equivalent to using getFirst().getValue(), but this method provides a slightly optimized implementation.


getLast

public java.util.Map.Entry getLast()
Return the entry for the "newest" mapping. That is, return the Map.Entry for the key-value pair that was first put into the map when compared to all the other pairings in the map. The behavior is equivalent to:
    Object obj = null;
    Iterator iter = entrySet().iterator();
    while(iter.hasNext()) {
      obj = iter.next();
    }
    return (Map.Entry)obj;
  
However, the implementation of this method ensures an O(1) lookup of the last key rather than O(n).


getLastKey

public java.lang.Object getLastKey()
Return the key for the "newest" mapping. That is, return the key for the mapping that was last put into the map when compared to all the other objects in the map. This behavior is equivalent to using getLast().getKey(), but this method provides a slightly optimized implementation.


getLastValue

public java.lang.Object getLastValue()
Return the value for the "newest" mapping. That is, return the value for the mapping that was last put into the map when compared to all the other objects in the map. This behavior is equivalent to using getLast().getValue(), but this method provides a slightly optimized implementation.


put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Implements Map.put(Object, Object)>Map.put(Object, Object) 55 .

Specified by:
put in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Implements Map.remove(Object)>Map.remove(Object) 55 .

Specified by:
remove in interface java.util.Map

removeImpl

private SequencedHashMap.Entry removeImpl(java.lang.Object key)
Fully remove an entry from the map, returning the old entry or null if there was no such entry with the specified key.


putAll

public void putAll(java.util.Map t)
Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as per Map.putAll(Map)>Map.putAll(Map) 55 ). The order in which the entries are added is determined by the iterator returned from Map.entrySet()>Map.entrySet() 55 for the specified map.

Specified by:
putAll in interface java.util.Map

clear

public void clear()
Implements Map.clear()>Map.clear() 55 .

Specified by:
clear in interface java.util.Map

equals

public boolean equals(java.lang.Object obj)
Implements Map.equals(Object)>Map.equals(Object) 55 .

Specified by:
equals in interface java.util.Map

hashCode

public int hashCode()
Implements Map.hashCode()>Map.hashCode() 55 .

Specified by:
hashCode in interface java.util.Map

toString

public java.lang.String toString()
Provides a string representation of the entries within the map. The format of the returned string may change with different releases, so this method is suitable for debugging purposes only. If a specific format is required, use entrySet() 55 .iterator() 55 and iterate over the entries in the map formatting them as appropriate.


keySet

public java.util.Set keySet()
Implements Map.keySet()>Map.keySet() 55 .

Specified by:
keySet in interface java.util.Map

values

public java.util.Collection values()
Implements Map.values()>Map.values() 55 .

Specified by:
values in interface java.util.Map

entrySet

public java.util.Set entrySet()
Implements Map.entrySet()>Map.entrySet() 55 .

Specified by:
entrySet in interface java.util.Map

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates a shallow copy of this object, preserving the internal structure by copying only references. The keys and values themselves are not clone()'d. The cloned object maintains the same sequence.


getEntry

private java.util.Map.Entry getEntry(int index)
Returns the Map.Entry at the specified index


get

public java.lang.Object get(int index)
Gets the key at the specified index.


getValue

public java.lang.Object getValue(int index)
Gets the value at the specified index.


indexOf

public int indexOf(java.lang.Object key)
Gets the index of the specified key.


iterator

public java.util.Iterator iterator()
Gets an iterator over the keys.


lastIndexOf

public int lastIndexOf(java.lang.Object key)
Gets the last index of the specified key.


sequence

public java.util.List sequence()
Returns a List view of the keys rather than a set view. The returned list is unmodifiable. This is required because changes to the values of the list (using ListIterator.set(Object)>ListIterator.set(Object) 55 ) will effectively remove the value from the list and reinsert that value at the end of the list, which is an unexpected side effect of changing the value of a list. This occurs because changing the key, changes when the mapping is added to the map and thus where it appears in the list.

An alternative to this method is to use keySet() 55


remove

public java.lang.Object remove(int index)
Removes the element at the specified index.


readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Deserializes this map from the given stream.

Specified by:
readExternal in interface java.io.Externalizable

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Serializes this map to the given stream.

Specified by:
writeExternal in interface java.io.Externalizable