LimeWire Collection Component API

org.limewire.collection
Class FixedsizeForgetfulHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<K,V>
              extended by org.limewire.collection.FixedsizeForgetfulHashMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

public class FixedsizeForgetfulHashMap<K,V>
extends LinkedHashMap<K,V>

Provides a better-defined replacement policy version of ForgetfulHashMap. Like ForgetfulHashMap, this is a mapping that "forgets" keys and values using a FIFO replacement policy, much like a cache.

Specifically, FixedsizeForgetfulHashMap allows a key to be re-mapped to a different value and then "renews" this key so it is the last key to be replaced (done in constant time).

    FixedsizeForgetfulHashMap<String, String> ffhm = 
        new FixedsizeForgetfulHashMap<String, String>(3);

    ffhm.put("myKey1", "Abby");
    ffhm.put("myKey2", "Bob");
    ffhm.put("myKey3", "Chris");
    System.out.println(ffhm);

    ffhm.put("myKey4", "Dan");
    System.out.println(ffhm);

    ffhm.put("myKey3", "replace");
    System.out.println(ffhm);   

    Output:
        {myKey1=Abby, myKey2=Bob, myKey3=Chris}
        {myKey2=Bob, myKey3=Chris, myKey4=Dan}
        {myKey2=Bob, myKey4=Dan, myKey3=replace}

Author:
Anurag Singla -- initial version, Christopher Rohrs -- cleaned up and added unit tests, Sam Berlin -- extend LinkedHashMap (adds unimplemented methods, simplifies)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
FixedsizeForgetfulHashMap(int size)
          Create a new instance that holds only the last "size" entries.
FixedsizeForgetfulHashMap(int size, int initialCapacity)
          Create a new instance that holds only the last "size" entries, using the given initialCapacity and a loadFactor of 0.75.
FixedsizeForgetfulHashMap(int size, int initialCapacity, float loadFactor)
          Create a new instance that holds only the last "size" entries, using the given initialCapacity & loadFactor.
 
Method Summary
 FixedsizeForgetfulHashMap<K,V> clone()
          Returns a shallow copy of this Map instance: the keys and values themselves are not cloned.
 boolean isFull()
          Tests if the map is full
 V put(K key, V value)
          Overridden to ensure that remapping a key renews the value in the linked list.
protected  boolean removeEldestEntry(Map.Entry<K,V> eldest)
          Returns true if the eldest entry should be removed.
 Map.Entry<K,V> removeLRUEntry()
          Removes the least recently used entry from the map
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get
 
Methods inherited from class java.util.HashMap
containsKey, entrySet, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, putAll, remove, size, values
 

Constructor Detail

FixedsizeForgetfulHashMap

public FixedsizeForgetfulHashMap(int size)
Create a new instance that holds only the last "size" entries.

Parameters:
size - the number of entries to hold
Throws:
IllegalArgumentException - if size is less < 1.

FixedsizeForgetfulHashMap

public FixedsizeForgetfulHashMap(int size,
                                 int initialCapacity)
Create a new instance that holds only the last "size" entries, using the given initialCapacity and a loadFactor of 0.75.

Parameters:
size - the number of entries to hold
Throws:
IllegalArgumentException - if size is less < 1.

FixedsizeForgetfulHashMap

public FixedsizeForgetfulHashMap(int size,
                                 int initialCapacity,
                                 float loadFactor)
Create a new instance that holds only the last "size" entries, using the given initialCapacity & loadFactor.

Parameters:
size - the number of entries to hold
Throws:
IllegalArgumentException - if size is less < 1.
Method Detail

isFull

public boolean isFull()
Tests if the map is full

Returns:
true, if the map is full (ie if adding any other entry will lead to removal of some other entry to maintain the fixed-size property of the map. Returns false, otherwise

removeLRUEntry

public Map.Entry<K,V> removeLRUEntry()
Removes the least recently used entry from the map

Returns:
A Map.Entry object

clone

public FixedsizeForgetfulHashMap<K,V> clone()
Returns a shallow copy of this Map instance: the keys and values themselves are not cloned.

Overrides:
clone in class HashMap<K,V>
Returns:
a shallow copy of this map.

removeEldestEntry

protected boolean removeEldestEntry(Map.Entry<K,V> eldest)
Returns true if the eldest entry should be removed.

Overrides:
removeEldestEntry in class LinkedHashMap<K,V>

put

public V put(K key,
             V value)
Overridden to ensure that remapping a key renews the value in the linked list.

Specified by:
put in interface Map<K,V>
Overrides:
put in class HashMap<K,V>

LimeWire Collection Component API

Copyright © 2008 Lime Wire LLC. All Rights Reserved.