activemq-cpp-3.9.0
decaf::util::LinkedHashMap< K, V, HASHCODE > Class Template Reference

Hashed and linked list implementation of the Map interface, with predictable iteration order. More...

#include <src/main/decaf/util/LinkedHashMap.h>

Inheritance diagram for decaf::util::LinkedHashMap< K, V, HASHCODE >:

Public Member Functions

 LinkedHashMap ()
 Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75). More...
 
 LinkedHashMap (int capacity)
 Constructs a new LinkedHashMap instance with the specified capacity. More...
 
 LinkedHashMap (int capacity, float load)
 Constructs a new. More...
 
 LinkedHashMap (int capacity, float load, bool order)
 Constructs a new. More...
 
 LinkedHashMap (const HashMap< K, V > &map)
 Constructs a new LinkedHashMap instance containing the mappings from the specified map. More...
 
virtual ~LinkedHashMap ()
 
virtual bool containsValue (const V &value) const
 Returns true if this map maps one or more keys to the specified value. More...
 
virtual void clear ()
 Removes all of the mappings from this map (optional operation). More...
 
virtual bool put (const K &key, const V &value)
 Associates the specified value with the specified key in this map (optional operation). More...
 
virtual bool put (const K &key, const V &value, V &oldValue)
 Associates the specified value with the specified key in this map (optional operation). More...
 
virtual V remove (const K &key)
 Removes the value (key/value pair) for the specified key from the map, returns a copy of the value that was mapped to the key. More...
 
virtual Set< MapEntry< K, V > > & entrySet ()
 Returns a Set view of the mappings contained in this map. More...
 
virtual const Set< MapEntry< K,
V > > & 
entrySet () const
 
virtual Set< K > & keySet ()
 Returns a Set view of the keys contained in this map. More...
 
virtual const Set< K > & keySet () const
 
virtual Collection< V > & values ()
 Returns a Collection view of the values contained in this map. More...
 
virtual const Collection< V > & values () const
 
virtual std::string toString () const
 
- Public Member Functions inherited from decaf::util::HashMap< K, V, HASHCODE >
 HashMap ()
 Creates a new empty HashMap with default configuration settings. More...
 
 HashMap (int capacity)
 Constructs a new HashMap instance with the specified capacity. More...
 
 HashMap (int capacity, float loadFactor)
 Constructs a new HashMap instance with the specified capacity. More...
 
 HashMap (const HashMap< K, V > &map)
 Creates a new HashMap with default configuration settings and fills it with the contents of the given source Map instance. More...
 
 HashMap (const Map< K, V > &map)
 Creates a new HashMap with default configuration settings and fills it with the contents of the given source Map instance. More...
 
virtual ~HashMap ()
 
HashMap< K, V > & operator= (const Map< K, V > &other)
 
HashMap< K, V > & operator= (const HashMap< K, V > &other)
 
bool operator== (const Map< K, V > &other) const
 
bool operator!= (const Map< K, V > &other) const
 
virtual bool isEmpty () const
 
virtual int size () const
 
virtual bool containsKey (const K &key) const
 Returns true if this map contains a mapping for the specified key. More...
 
virtual V & get (const K &key)
 Gets the value mapped to the specified key in the Map. More...
 
virtual const V & get (const K &key) const
 Gets the value mapped to the specified key in the Map. More...
 
virtual void putAll (const Map< K, V > &map)
 Copies all of the mappings from the specified map to this map (optional operation). More...
 
virtual bool equals (const Map< K, V > &source) const
 Compares the specified object with this map for equality. More...
 
virtual void copy (const Map< K, V > &source)
 Copies the content of the source map into this map. More...
 
- Public Member Functions inherited from decaf::util::AbstractMap< K, V >
 AbstractMap ()
 
 AbstractMap (const Map< K, V > &map DECAF_UNUSED)
 
 AbstractMap (const AbstractMap< K, V > &map DECAF_UNUSED)
 
virtual ~AbstractMap ()
 
virtual void lock ()
 Locks the object. More...
 
virtual bool tryLock ()
 Attempts to Lock the object, if the lock is already held by another thread than this method returns false. More...
 
virtual void unlock ()
 Unlocks the object. More...
 
virtual void wait ()
 Waits on a signal from this object, which is generated by a call to Notify. More...
 
virtual void wait (long long millisecs)
 Waits on a signal from this object, which is generated by a call to Notify. More...
 
virtual void wait (long long millisecs, int nanos)
 Waits on a signal from this object, which is generated by a call to Notify. More...
 
virtual void notify ()
 Signals a waiter on this object that it can now wake up and continue. More...
 
virtual void notifyAll ()
 Signals the waiters on this object that it can now wake up and continue. More...
 
- Public Member Functions inherited from decaf::util::Map< K, V >
 Map ()
 Default constructor - does nothing. More...
 
virtual ~Map ()
 
- Public Member Functions inherited from decaf::util::concurrent::Synchronizable
virtual ~Synchronizable ()
 

Protected Member Functions

virtual bool removeEldestEntry (const MapEntry< K, V > &eldest DECAF_UNUSED)
 This method is queried from the put and putAll methods to check if the eldest member of the map should be deleted before adding the new member. More...
 
virtual void onEviction (const MapEntry< K, V > &eldest DECAF_UNUSED)
 This method is called when the removeEldestEntry has returned true and a MapEntry is about to be removed from the Map. More...
 
virtual LinkedHashMapEntry * getEntry (const K &key) const
 
virtual HashMap< K, V,
HASHCODE >::HashMapEntry
createEntry (const K &key, int index, const V &value)
 
virtual HashMap< K, V,
HASHCODE >::HashMapEntry
createHashedEntry (const K &key, int index, int hash)
 
void linkEntry (LinkedHashMapEntry *entry)
 
virtual bool putImpl (const K &key, const V &value)
 
virtual bool putImpl (const K &key, const V &value, V &oldValue)
 
- Protected Member Functions inherited from decaf::util::HashMap< K, V, HASHCODE >
void putAllImpl (const Map< K, V > &map)
 
HashMapEntryfindKeyEntry (const K &key, int index, int keyHash) const
 
void rehash (int capacity)
 
void rehash ()
 
void removeEntry (HashMapEntry *entry)
 
HashMapEntryremoveEntry (const K &key)
 

Additional Inherited Members

- Protected Attributes inherited from decaf::util::HashMap< K, V, HASHCODE >
HASHCODE hashFunc
 The Hash Code generator for this map's keys. More...
 
int elementCount
 
decaf::lang::ArrayPointer
< HashMapEntry * > 
elementData
 
int modCount
 
float loadFactor
 
int threshold
 
decaf::lang::Pointer
< HashMapEntrySet
cachedEntrySet
 
decaf::lang::Pointer
< HashMapKeySet
cachedKeySet
 
decaf::lang::Pointer
< HashMapValueCollection
cachedValueCollection
 
decaf::lang::Pointer
< ConstHashMapEntrySet
cachedConstEntrySet
 
decaf::lang::Pointer
< ConstHashMapKeySet
cachedConstKeySet
 
decaf::lang::Pointer
< ConstHashMapValueCollection
cachedConstValueCollection
 
- Protected Attributes inherited from decaf::util::AbstractMap< K, V >
util::concurrent::Mutex mutex
 

Detailed Description

template<typename K, typename V, typename HASHCODE = HashCode<K>>
class decaf::util::LinkedHashMap< K, V, HASHCODE >

Hashed and linked list implementation of the Map interface, with predictable iteration order.

This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap, without incurring the increased cost associated with TreeMap. It can be used to produce a copy of a map that has the same order as the original, regardless of the original map's implementation:

void foo(Map m) { Map copy = new LinkedHashMap(m); ... }

This technique is particularly useful if a module takes a map on input, copies it, and later returns results whose order is determined by that of the copy. (Clients generally appreciate having things returned in the same order they were presented.)

A special constructor is provided to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order). This kind of map is well-suited to building LRU caches. Invoking the put or get method results in an access to the corresponding entry (assuming it exists after the invocation completes). The putAll method generates one entry access for each mapping in the specified map, in the order that key-value mappings are provided by the specified map's entry set iterator. No other methods generate entry accesses. In particular, operations on collection-views do not affect the order of iteration of the backing map.

The removeEldestEntry(MapEntry) method may be overridden to impose a policy for removing stale mappings automatically when new mappings are added to the map. When the entries are about to be removed from the map the onEviction method is called giving subclasses a chance to delete pointer values or perform other cleanup prior to the mapping being removed.

This class provides all of the optional Map operations. Like HashMap, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets. Performance is likely to be just slightly below that of HashMap, due to the added expense of maintaining the linked list, with one exception: Iteration over the collection-views of a LinkedHashMap requires time proportional to the size of the map, regardless of its capacity. Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.

A linked hash map has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashMap. Note, however, that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for HashMap, as iteration times for this class are unaffected by capacity.

Note that this implementation is not synchronized. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

Map<K,V>* m = Collections::synchronizedMap(new LinkedHashMap(...));

A structural modification is any operation that adds or deletes one or more mappings or, in the case of access-ordered linked hash maps, affects iteration order. In insertion-ordered linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification. In access-ordered linked hash maps, merely querying the map with get is a structural modification.)

The iterators returned by the iterator method of the collections returned by all of this class's collection view methods are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

Since
1.0

Constructor & Destructor Documentation

template<typename K , typename V , typename HASHCODE = HashCode<K>>
decaf::util::LinkedHashMap< K, V, HASHCODE >::LinkedHashMap ( )
inline

Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).

template<typename K , typename V , typename HASHCODE = HashCode<K>>
decaf::util::LinkedHashMap< K, V, HASHCODE >::LinkedHashMap ( int  capacity)
inline

Constructs a new LinkedHashMap instance with the specified capacity.

Parameters
capacityThe initial capacity of this map.
Exceptions
IllegalArgumentExceptionif the capacity is less than zero.
template<typename K , typename V , typename HASHCODE = HashCode<K>>
decaf::util::LinkedHashMap< K, V, HASHCODE >::LinkedHashMap ( int  capacity,
float  load 
)
inline

Constructs a new.

instance with the specified capacity and load factor.

Parameters
capacityThe initial capacity of this map.
loadThe initial load factor for this map.
Exceptions
IllegalArgumentExceptionIf the capacity is less than zero or the load factor is less or equal to zero.
template<typename K , typename V , typename HASHCODE = HashCode<K>>
decaf::util::LinkedHashMap< K, V, HASHCODE >::LinkedHashMap ( int  capacity,
float  load,
bool  order 
)
inline

Constructs a new.

instance with the specified capacity, load factor and a flag specifying the ordering behavior.

Parameters
capacityThe initial capacity of this map.
loadThe initial load factor for this map.
orderTrue if the ordering should be done based on the last access (from least-recently accessed to most-recently accessed), and false if the ordering should be the order in which the entries were inserted.
Exceptions
IllegalArgumentExceptionIf the capacity is less than zero or the load factor is less or equal to zero.
template<typename K , typename V , typename HASHCODE = HashCode<K>>
decaf::util::LinkedHashMap< K, V, HASHCODE >::LinkedHashMap ( const HashMap< K, V > &  map)
inline

Constructs a new LinkedHashMap instance containing the mappings from the specified map.

The order of the elements is preserved.

Parameters
mapThe mappings to add to this Map instance.
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual decaf::util::LinkedHashMap< K, V, HASHCODE >::~LinkedHashMap ( )
inlinevirtual

Member Function Documentation

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual void decaf::util::LinkedHashMap< K, V, HASHCODE >::clear ( )
inlinevirtual

Removes all of the mappings from this map (optional operation).

The map will be empty after this call returns.

Exceptions
UnsupportedOperationExceptionif the clear operation is not supported by this map.

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References decaf::util::HashMap< K, V, HASHCODE >::clear(), and NULL.

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual bool decaf::util::LinkedHashMap< K, V, HASHCODE >::containsValue ( const V &  value) const
inlinevirtual

Returns true if this map maps one or more keys to the specified value.

More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==v). This operation will probably require time linear in the map size for most implementations of the Map interface.

Parameters
valueThe Value to look up in this Map.
Returns
true if this map contains at least one mapping for the value, otherwise false.

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References NULL.

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual HashMap<K, V, HASHCODE>::HashMapEntry* decaf::util::LinkedHashMap< K, V, HASHCODE >::createEntry ( const K &  key,
int  index,
const V &  value 
)
inlineprotectedvirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual HashMap<K, V, HASHCODE>::HashMapEntry* decaf::util::LinkedHashMap< K, V, HASHCODE >::createHashedEntry ( const K &  key,
int  index,
int  hash 
)
inlineprotectedvirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual Set< MapEntry<K,V> >& decaf::util::LinkedHashMap< K, V, HASHCODE >::entrySet ( )
inlinevirtual

Returns a Set view of the mappings contained in this map.

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns
a reference to a Set<MapEntry<K,V>> that is backed by this Map.

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References decaf::util::HashMap< K, V, HASHCODE >::cachedEntrySet, and NULL.

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual const Set< MapEntry<K,V> >& decaf::util::LinkedHashMap< K, V, HASHCODE >::entrySet ( ) const
inlinevirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual LinkedHashMapEntry* decaf::util::LinkedHashMap< K, V, HASHCODE >::getEntry ( const K &  key) const
inlineprotectedvirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual Set<K>& decaf::util::LinkedHashMap< K, V, HASHCODE >::keySet ( )
inlinevirtual

Returns a Set view of the keys contained in this map.

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns
a set view of the keys contained in this map,

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References decaf::util::HashMap< K, V, HASHCODE >::cachedKeySet, and NULL.

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual const Set<K>& decaf::util::LinkedHashMap< K, V, HASHCODE >::keySet ( ) const
inlinevirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
void decaf::util::LinkedHashMap< K, V, HASHCODE >::linkEntry ( LinkedHashMapEntry *  entry)
inlineprotected
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual void decaf::util::LinkedHashMap< K, V, HASHCODE >::onEviction ( const MapEntry< K, V > &eldest  DECAF_UNUSED)
inlineprotectedvirtual

This method is called when the removeEldestEntry has returned true and a MapEntry is about to be removed from the Map.

This method allows for Maps that contain pointers in their MapEntry object to have a chance to properly delete the pointer when the entry is removed.

Parameters
eldestThe MapEntry value that is about to be removed from the Map.

Referenced by decaf::util::LinkedHashMap< K, V, HASHCODE >::put().

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual bool decaf::util::LinkedHashMap< K, V, HASHCODE >::put ( const K &  key,
const V &  value 
)
inlinevirtual

Associates the specified value with the specified key in this map (optional operation).

If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
keyThe target key.
valueThe value to be set.
Returns
true if the put operation replaced a value that was associated with an existing mapping to the given key or false otherwise.
Exceptions
UnsupportedOperationExceptionif this map is unmodifiable.
IllegalArgumentExceptionif some property of the specified key or value prevents it from being stored in this map

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References decaf::util::LinkedHashMap< K, V, HASHCODE >::onEviction(), decaf::util::LinkedHashMap< K, V, HASHCODE >::putImpl(), and decaf::util::LinkedHashMap< K, V, HASHCODE >::removeEldestEntry().

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual bool decaf::util::LinkedHashMap< K, V, HASHCODE >::put ( const K &  key,
const V &  value,
V &  oldValue 
)
inlinevirtual

Associates the specified value with the specified key in this map (optional operation).

If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

This method accepts a reference to a value which will be assigned the previous value for the given key (if any). If there was no previous mapping for the given key the out value is not written to. A return of true indicates that a value was replaced by this put operation.

Parameters
keyThe target key.
valueThe value to be set.
oldValue(out) The value previously held in the mapping for this key. .
Returns
true if the put operation replaced a value that was associated with an existing mapping to the given key or false otherwise.
Exceptions
UnsupportedOperationExceptionif this map is unmodifiable.
IllegalArgumentExceptionif some property of the specified key or value prevents it from being stored in this map

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References decaf::util::LinkedHashMap< K, V, HASHCODE >::onEviction(), decaf::util::LinkedHashMap< K, V, HASHCODE >::putImpl(), and decaf::util::LinkedHashMap< K, V, HASHCODE >::removeEldestEntry().

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual bool decaf::util::LinkedHashMap< K, V, HASHCODE >::putImpl ( const K &  key,
const V &  value 
)
inlineprotectedvirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual V decaf::util::LinkedHashMap< K, V, HASHCODE >::remove ( const K &  key)
inlinevirtual

Removes the value (key/value pair) for the specified key from the map, returns a copy of the value that was mapped to the key.

Care must be taken when using this operation as it will throw an exception if there is no mapping for the given key.

Parameters
keyThe search key whose mapping is to be removed.
Returns
a copy of the element that was previously mapped to the given key.
Exceptions
NoSuchElementExceptionif this key is not in the Map.
UnsupportedOperationExceptionif this map is unmodifiable.

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References NULL, and decaf::util::HashMap< K, V, HASHCODE >::removeEntry().

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual bool decaf::util::LinkedHashMap< K, V, HASHCODE >::removeEldestEntry ( const MapEntry< K, V > &eldest  DECAF_UNUSED)
inlineprotectedvirtual

This method is queried from the put and putAll methods to check if the eldest member of the map should be deleted before adding the new member.

If this map was created with accessOrder = true, then the result of removeEldestEntry is assumed to be false.

Parameters
eldestThe entry to check if it should be removed.
Returns
true if the eldest member should be removed.

Reimplemented in decaf::util::LRUCache< K, V, HASHCODE >.

Referenced by decaf::util::LinkedHashMap< K, V, HASHCODE >::put().

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual std::string decaf::util::LinkedHashMap< K, V, HASHCODE >::toString ( ) const
inlinevirtual
template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual Collection<V>& decaf::util::LinkedHashMap< K, V, HASHCODE >::values ( )
inlinevirtual

Returns a Collection view of the values contained in this map.

The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. For the const version of this method the Collection can only be used as a view into the Map.

Returns
a collection view of the values contained in this map.

Reimplemented from decaf::util::HashMap< K, V, HASHCODE >.

References decaf::util::HashMap< K, V, HASHCODE >::cachedValueCollection, and NULL.

template<typename K , typename V , typename HASHCODE = HashCode<K>>
virtual const Collection<V>& decaf::util::LinkedHashMap< K, V, HASHCODE >::values ( ) const
inlinevirtual

The documentation for this class was generated from the following file: