Rakesh API

org.rakeshv.utils
Class LinkedHashMap<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.rakeshv.utils.LinkedHashMap<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>
Direct Known Subclasses:
FIFO, LRU

 class LinkedHashMap<K,V>
extends java.util.LinkedHashMap<K,V>
implements Map<K,V>

A sub-class of java.util.LinkedHashMap used to implement a size controlled Map. Implements the removeEldestEntry( Map.Entry ) method to remove the oldest entries if Map.size() > maxEntries.

Copyright 2004-2006 Rakesh Vidyadharan

Version:
$Id: LinkedHashMap.java,v 1.12 2006/03/11 16:30:57 rakesh Exp $
Author:
Rakesh Vidyadharan 2004 September 1

Nested Class Summary
(package private) static class LinkedHashMap.Statistics
          An implementation of LinkedHashMap.Statistics used to track cache performance.
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
(package private) static int DEFAULT_CAPACITY
          The default size for the map.
(package private) static float DEFAULT_LOAD_FACTOR
          The default load-factor value.
(package private) static boolean FIFO_CACHE
          Flag to indicate insertion-order policy.
private static java.util.logging.Logger logger
          An instance of Logger used to log informational messages.
(package private) static boolean LRU_CACHE
          Flag to indicate access-order policy.
private  int maxEntries
          A variable used to keep track of the maximum number of entries to keep in the Map.
private  LinkedHashMap.Statistics statistics
          An instance of LinkedHashMap.Statistics that is used to track usage of the cache.
 
Constructor Summary
LinkedHashMap()
          Default constructor.
LinkedHashMap(int capacity)
          Constructs a new instance with the specified initial capacity.
LinkedHashMap(int capacity, float loadFactor)
          Constructs a new instance with the specified initial capacity and load factor.
LinkedHashMap(int capacity, float loadFactor, boolean accessOrder)
          Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
LinkedHashMap(java.util.Map<K,V> map)
          Constructs an ordered LinkedHashMap instance with the same mappings as the specified map.
 
Method Summary
 void clear()
          Removes all mappings from this map.
 V get(java.lang.Object key)
          Over-ridden to increment the values of LinkedHashMap.Statistics.totalRequests and if necessary, LinkedHashMap.Statistics.successfulRequests.
 int getMaxEntries()
          Returns maxEntries.
 Map.Statistics getStatistics()
          Returns statistics.
private  void init(int capacity)
          Initialise the maxEntries and statistics.
protected  boolean removeEldestEntry(java.util.Map.Entry eldest)
          Returns true if this map should remove its eldest entry.
 void setMaxEntries(int maxEntries)
          Set maxEntries.
 
Methods inherited from class java.util.LinkedHashMap
containsValue
 
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, containsValue, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Field Detail

logger

private static final java.util.logging.Logger logger
An instance of Logger used to log informational messages. All logs are created using the Logger.info() method.


DEFAULT_CAPACITY

static final int DEFAULT_CAPACITY
The default size for the map.

See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR

static final float DEFAULT_LOAD_FACTOR
The default load-factor value.

See Also:
Constant Field Values

LRU_CACHE

static final boolean LRU_CACHE
Flag to indicate access-order policy.

See Also:
Constant Field Values

FIFO_CACHE

static final boolean FIFO_CACHE
Flag to indicate insertion-order policy.

See Also:
Constant Field Values

maxEntries

private volatile int maxEntries
A variable used to keep track of the maximum number of entries to keep in the Map.


statistics

private LinkedHashMap.Statistics statistics
An instance of LinkedHashMap.Statistics that is used to track usage of the cache.

Constructor Detail

LinkedHashMap

LinkedHashMap()
Default constructor. Creates a new Map with using the super class constructor. If this form of the constructor is used, the cache size will not be controlled.


LinkedHashMap

LinkedHashMap(int capacity)
Constructs a new instance with the specified initial capacity.

Parameters:
capacity - The maximum size for this map.

LinkedHashMap

LinkedHashMap(int capacity,
              float loadFactor)
Constructs a new instance with the specified initial capacity and load factor.

Parameters:
capacity - The maximum size for this map.
loadFactor - The load-factor to use.

LinkedHashMap

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

Parameters:
capacity - The maximum size for this map.
loadFactor - The load-factor to use.
accessOrder - The ordering mode - LRU_CACHE for access-order, FIFO_CACHE for insertion-order.

LinkedHashMap

LinkedHashMap(java.util.Map<K,V> map)
Constructs an ordered LinkedHashMap instance with the same mappings as the specified map. The maximum size of this map is set to the size of the specified map.

Parameters:
map - The map to use to create the new instance.
Throws:
java.lang.NullPointerException - If the specified map is null.
Method Detail

init

private void init(int capacity)
Initialise the maxEntries and statistics.


get

public V get(java.lang.Object key)
Over-ridden to increment the values of LinkedHashMap.Statistics.totalRequests and if necessary, LinkedHashMap.Statistics.successfulRequests.

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class java.util.LinkedHashMap<K,V>
Parameters:
key - key whose associated value is to be returned.
Returns:
Object - The value to which this map maps the specified key, or null if the map contains no mapping for this key.

clear

public void clear()
Removes all mappings from this map. Resets statistics.

Specified by:
clear in interface java.util.Map<K,V>
Overrides:
clear in class java.util.LinkedHashMap<K,V>

removeEldestEntry

protected boolean removeEldestEntry(java.util.Map.Entry eldest)
Returns true if this map should remove its eldest entry. This method is invoked by put and putAll after inserting a new entry into the map. Checks the HashMap.size() of the map against maxEntries to determine the return value.

Overrides:
removeEldestEntry in class java.util.LinkedHashMap<K,V>
Parameters:
eldest - The least recently accessed entry. This is the entry that will be removed it this method returns true. If the map was empty prior to the put or putAll invocation resulting in this invocation, this will be the entry that was just inserted; in other words, if the map contains a single entry, the eldest entry is also the newest.

getMaxEntries

public final int getMaxEntries()
Returns maxEntries.

Returns:
int The value/reference of/to maxEntries.

setMaxEntries

public final void setMaxEntries(int maxEntries)
Set maxEntries. This can be used to dynamically change the size of the cache.

Parameters:
maxEntries - The value to set.

getStatistics

public final Map.Statistics getStatistics()
Returns statistics. This may be used by client applications to monitor cache usage.

Returns:
Statistics The value/reference of/to statistics.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan