org.hd.d.pg2k.svrCore
Class MemoryTools.SimpleLRUMap<K,V>

java.lang.Object
  extended by org.hd.d.pg2k.svrCore.MemoryTools.SimpleLRUMap<K,V>
Enclosing class:
MemoryTools

public static final class MemoryTools.SimpleLRUMap<K,V>
extends java.lang.Object

Simplified thread-safe map with fixed capacity that discards excess items in LRU (Least Recently Used) order. This provides a map with a fixed upper size. Attempts to insert more items than the capacity will result in old items being removed in LRU order, thus leaving the newest items in the cache.

Each put() or get() makes its key/value pair the most-recently used and thus the last to be removed from the map of current key/value pairs when a series of put()s forces the map to overflow.

Thread-safe.

A lock can be held on instances of this object to make compound operations atomic.

Does not support the full Map interface.


Field Summary
private static float defaultLoadFactor
          Default load factor.
private  java.util.LinkedHashMap<K,V> lhm
          Underlying (non-thread-safe) LinkedHashMap on which this is based; never null.
private  java.lang.String name
          Optional name for diagnostics; can be null.
 
Constructor Summary
MemoryTools.SimpleLRUMap(int maxCapacity, float loadFactor, java.lang.String name)
          Create an instance with given (positive) maximum size and load factor.
MemoryTools.SimpleLRUMap(int maxCapacity, java.lang.String name)
          Create an instance with given (positive) maximum size and the default load factor.
 
Method Summary
 void clear()
          Clear the map.
 V get(K key)
          Get an entry from the map, making it the Most Recently Used; null if no such element.
 java.util.Map<K,V> getCopy()
          Take a copy of the map contents, which does not alter LRU; never null.
 V put(K key, V value)
          Put an entry in the map, making it the Most Recently Used, returning previous value if any.
 V remove(K key)
          Remove an entry from the map and return the removed value, if any, else null.
 int size()
          Return the number of entries in the map; non-negative.
 java.lang.String toString()
          Provide a human-readable summary of status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultLoadFactor

private static final float defaultLoadFactor
Default load factor.

See Also:
Constant Field Values

lhm

private final java.util.LinkedHashMap<K,V> lhm
Underlying (non-thread-safe) LinkedHashMap on which this is based; never null.


name

private final java.lang.String name
Optional name for diagnostics; can be null.

Constructor Detail

MemoryTools.SimpleLRUMap

public MemoryTools.SimpleLRUMap(int maxCapacity,
                                java.lang.String name)
Create an instance with given (positive) maximum size and the default load factor.


MemoryTools.SimpleLRUMap

public MemoryTools.SimpleLRUMap(int maxCapacity,
                                float loadFactor,
                                java.lang.String name)
Create an instance with given (positive) maximum size and load factor.

Method Detail

get

public V get(K key)
Get an entry from the map, making it the Most Recently Used; null if no such element.


put

public V put(K key,
             V value)
Put an entry in the map, making it the Most Recently Used, returning previous value if any.


remove

public V remove(K key)
Remove an entry from the map and return the removed value, if any, else null.


clear

public void clear()
Clear the map.


getCopy

public java.util.Map<K,V> getCopy()
Take a copy of the map contents, which does not alter LRU; never null.


size

public int size()
Return the number of entries in the map; non-negative.


toString

public java.lang.String toString()
Provide a human-readable summary of status. Useful for debugging/tuning, for example.

Overrides:
toString in class java.lang.Object

DHD Multimedia Gallery V1.53.0

Copyright (c) 1996-2009, Damon Hart-Davis. All rights reserved.