org.geotools.util
Class LRULinkedHashMap<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.geotools.util.LRULinkedHashMap<K,V>
Type Parameters:
K - The type of keys in the map.
V - The type of values in the map.
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>

public final class LRULinkedHashMap<K,V>
extends java.util.LinkedHashMap<K,V>

A Map with a fixed maximum size which removes the least recently used (LRU) entry if an entry is added when full. This class implements a simple technique for LRU pooling of objects.

This class is not thread-safe. Synchronizations (if wanted) are user's reponsability.

Since:
2.3
Version:
$Id: LRULinkedHashMap.java 30792 2008-06-23 19:19:58Z desruisseaux $
Author:
Simone Giannecchini, Martin Desruisseaux
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
LRULinkedHashMap()
          Constructs a LRULinkedHashMap with default initial capacity, maximum size and load factor.
LRULinkedHashMap(int initialCapacity)
          Constructs a LRULinkedHashMap with default maximum size and load factor.
LRULinkedHashMap(int initialCapacity, float loadFactor)
          Constructs a LRULinkedHashMap with default maximum size.
LRULinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
          Constructs a LRULinkedHashMap with default maximum size.
LRULinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder, int maximumSize)
          Constructs a LRULinkedHashMap with the specified maximum size.
LRULinkedHashMap(java.util.Map<K,V> map)
          Constructs a LRULinkedHashMap with all entries from the specified map.
LRULinkedHashMap(java.util.Map<K,V> map, int maximumSize)
          Constructs a LRULinkedHashMap with all entries from the specified map and maximum number of entries.
 
Method Summary
static
<K,V> LRULinkedHashMap<K,V>
createForRecentAccess(int maximumSize)
          Creates a map for the most recently accessed entries.
static
<K,V> LRULinkedHashMap<K,V>
createForRecentInserts(int maximumSize)
          Creates a map for the most recently inserted entries.
 int getMaximumSize()
          Returns the maximal size allowed for this map.
protected  boolean removeEldestEntry(java.util.Map.Entry<K,V> eldest)
          Returns true if this map should remove its eldest entry.
 void setMaximumSize(int max)
          Sets the maximal size allowed for this map.
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, put, 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, put, putAll, remove, size, values
 

Constructor Detail

LRULinkedHashMap

public LRULinkedHashMap()
Constructs a LRULinkedHashMap with default initial capacity, maximum size and load factor.


LRULinkedHashMap

public LRULinkedHashMap(int initialCapacity)
Constructs a LRULinkedHashMap with default maximum size and load factor.

Parameters:
initialCapacity - The initial capacity.

LRULinkedHashMap

public LRULinkedHashMap(int initialCapacity,
                        float loadFactor)
Constructs a LRULinkedHashMap with default maximum size.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.

LRULinkedHashMap

public LRULinkedHashMap(int initialCapacity,
                        float loadFactor,
                        boolean accessOrder)
Constructs a LRULinkedHashMap with default maximum size.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
accessOrder - The ordering mode: true for access-order, false for insertion-order.

LRULinkedHashMap

public LRULinkedHashMap(int initialCapacity,
                        float loadFactor,
                        boolean accessOrder,
                        int maximumSize)
Constructs a LRULinkedHashMap with the specified maximum size.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
accessOrder - The ordering mode: true for access-order, false for insertion-order.
maximumSize - Maximum number of entries for this LRU map.

LRULinkedHashMap

public LRULinkedHashMap(java.util.Map<K,V> map)
Constructs a LRULinkedHashMap with all entries from the specified map. The maximum size is set to the given map size.

Parameters:
map - The map whose mappings are to be placed in this map.
Since:
2.5

LRULinkedHashMap

public LRULinkedHashMap(java.util.Map<K,V> map,
                        int maximumSize)
Constructs a LRULinkedHashMap with all entries from the specified map and maximum number of entries.

Parameters:
map - The map whose mappings are to be placed in this map.
maximumSize - Maximum number of entries for this LRU map.
Since:
2.5
Method Detail

createForRecentAccess

public static <K,V> LRULinkedHashMap<K,V> createForRecentAccess(int maximumSize)
Creates a map for the most recently accessed entries. This convenience constructor uses an initial capacity big enough for holding the given maximum number of entries, so it is not appropriate if maximumSize is very large.

Type Parameters:
K - The type of keys in the map.
V - The type of values in the map.
Parameters:
maximumSize - Maximum number of entries for this LRU map.
Returns:
A map holding only the maximumSize most recently accessed entries.
Since:
2.5

createForRecentInserts

public static <K,V> LRULinkedHashMap<K,V> createForRecentInserts(int maximumSize)
Creates a map for the most recently inserted entries. This convenience constructor uses an initial capacity big enough for holding the given maximum number of entries, so it is not appropriate if maximumSize is very large.

Type Parameters:
K - The type of keys in the map.
V - The type of values in the map.
Parameters:
maximumSize - Maximum number of entries for this LRU map.
Returns:
A map holding only the maximumSize most recently inserted entries.
Since:
2.5

getMaximumSize

public int getMaximumSize()
Returns the maximal size allowed for this map. This is the value given to constructors.

Returns:
The maximal size allowed for this map.
Since:
2.5

setMaximumSize

public void setMaximumSize(int max)
Sets the maximal size allowed for this map. If the given size is smaller than the previous one, eldest entries will be immediately removed.

Parameters:
max - The new maximal size.
Throws:
java.lang.IllegalArgumentException - if the given size is negative.
Since:
2.5

removeEldestEntry

protected boolean removeEldestEntry(java.util.Map.Entry<K,V> eldest)
Returns true if this map should remove its eldest entry. The default implementation returns true if the number of entries in this map has reached the maximum number of entries specified at construction time.

Overrides:
removeEldestEntry in class java.util.LinkedHashMap<K,V>
Parameters:
eldest - The least recently inserted entry in the map, or if this is an access-ordered map, the least recently accessed entry. This is the entry that will be removed it this method returns true.
Returns:
true if the eldest entry should be removed from the map; false if it should be retained.


Copyright © 1996-2010 Geotools. All Rights Reserved.