org.infinispan.util
Class BidirectionalLinkedHashMap<K,V>

java.lang.Object
  extended by org.infinispan.util.AbstractMap<K,V>
      extended by org.infinispan.util.BidirectionalLinkedHashMap<K,V>
All Implemented Interfaces:
Cloneable, Map<K,V>, BidirectionalMap<K,V>

public class BidirectionalLinkedHashMap<K,V>
extends AbstractMap<K,V>
implements BidirectionalMap<K,V>, Cloneable

Similar to the JDK's LinkedHashMap except that this version makes use of the fact that entries are bidirectionally linked and can hence be navigated either from the start or from the end. It exposes such navigability by overriding Map.keySet() and Map.entrySet() to return ReversibleOrderedSet rather than a standard JDK Set. ReversibleOrderedSets allow you to access 2 iterators: one that iterates from start to end, as usual, and a reversed one that iterates from end to start instead.

Unlike the JDK LinkedHashMap, this implementation does not support null keys.

Since:
4.0
Author:
Manik Surtani

Nested Class Summary
 
Nested classes/interfaces inherited from class org.infinispan.util.AbstractMap
AbstractMap.SimpleEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
 
Fields inherited from class org.infinispan.util.AbstractMap
entrySet, keySet, values
 
Constructor Summary
BidirectionalLinkedHashMap()
          Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
BidirectionalLinkedHashMap(int initialCapacity)
          Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
BidirectionalLinkedHashMap(int initialCapacity, float loadFactor)
          Constructs an empty HashMap with the specified initial capacity and load factor.
BidirectionalLinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
          Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
BidirectionalLinkedHashMap(Map<? extends K,? extends V> m)
          Constructs a new HashMap with the same mappings as the specified Map.
 
Method Summary
 void clear()
          Removes all of the mappings from this map.
 BidirectionalLinkedHashMap clone()
          Returns a shallow copy of this Map instance: the keys and values themselves are not cloned.
 boolean containsKey(Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(Object value)
          Returns true if this map maps one or more keys to the specified value.
 ReversibleOrderedSet<Map.Entry<K,V>> entrySet()
           
 V get(Object key)
          Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 ReversibleOrderedSet<K> keySet()
           
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(Map<? extends K,? extends V> m)
          Copies all of the mappings from the specified map to this map.
 V remove(Object key)
          Removes the mapping for the specified key from this map if present.
 int size()
          Returns the number of key-value mappings in this map.
 Collection<V> values()
           
 
Methods inherited from class org.infinispan.util.AbstractMap
assertKeyNotNull, eq, hash
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

BidirectionalLinkedHashMap

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

Parameters:
initialCapacity - the initial capacity
loadFactor - the load factor
Throws:
IllegalArgumentException - if the initial capacity is negative or the load factor is non-positive

BidirectionalLinkedHashMap

public BidirectionalLinkedHashMap(int initialCapacity,
                                  float loadFactor,
                                  boolean accessOrder)
Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.

Parameters:
initialCapacity - the initial capacity
loadFactor - the load factor
accessOrder - the ordering mode - true for access-order, false for insertion-order
Throws:
IllegalArgumentException - if the initial capacity is negative or the load factor is non-positive

BidirectionalLinkedHashMap

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

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

BidirectionalLinkedHashMap

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


BidirectionalLinkedHashMap

public BidirectionalLinkedHashMap(Map<? extends K,? extends V> m)
Constructs a new HashMap with the same mappings as the specified Map. The HashMap 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:
NullPointerException - if the specified map is null
Method Detail

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface Map<K,V>
Returns:
the number of key-value mappings in this map

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface Map<K,V>
Returns:
true if this map contains no key-value mappings

containsKey

public boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface Map<K,V>
Parameters:
key - The key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key.

put

public V put(K key,
             V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

Specified by:
put in interface Map<K,V>
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

putAll

public void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll in interface Map<K,V>
Parameters:
m - mappings to be stored in this map
Throws:
NullPointerException - if the specified map is null

remove

public V remove(Object key)
Removes the mapping for the specified key from this map if present.

Specified by:
remove in interface Map<K,V>
Parameters:
key - key whose mapping is to be removed from the map
Returns:
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

clear

public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.

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

containsValue

public boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.

Specified by:
containsValue in interface Map<K,V>
Parameters:
value - value whose presence in this map is to be tested
Returns:
true if this map maps one or more keys to the specified value

get

public V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Specified by:
get in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

keySet

public ReversibleOrderedSet<K> keySet()
Specified by:
keySet in interface Map<K,V>
Specified by:
keySet in interface BidirectionalMap<K,V>

entrySet

public ReversibleOrderedSet<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in interface BidirectionalMap<K,V>

clone

public BidirectionalLinkedHashMap clone()
Returns a shallow copy of this Map instance: the keys and values themselves are not cloned.

Overrides:
clone in class Object
Returns:
a shallow copy of this map.

Google Analytics

Copyright © 2010 JBoss, a division of Red Hat. All Rights Reserved.