org.geotools.util
Class SoftValueHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.geotools.util.SoftValueHashMap<K,V>
Type Parameters:
K - The type of keys in the map.
V - The type of values in the map.
All Implemented Interfaces:
java.util.Map<K,V>

public class SoftValueHashMap<K,V>
extends java.util.AbstractMap<K,V>

A hash map implementation that uses soft references, leaving memory when an entry is not used anymore and memory is low.

This map implementation actually maintains some of the first entries as hard references. Only oldest entries are retained by soft references, in order to avoid too aggressive garbage collection. The amount of entries to retain by hard reference is specified at construction time.

This map is thread-safe. It accepts the null key, but doesn't accepts null values. Usage of value, key or entry collections are supported except for direct usage of their iterators, which may throw ConcurrentModificationException randomly depending on the garbage collector activity.

Since:
2.3
Version:
$Id: SoftValueHashMap.java 30792 2008-06-23 19:19:58Z desruisseaux $
Author:
Simone Giannecchini, Martin Desruisseaux

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
SoftValueHashMap()
          Creates a map with the default hard references count.
SoftValueHashMap(int hardReferencesCount)
          Creates a map with the specified hard references count.
 
Method Summary
 void clear()
          Removes all mappings from this map.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more keys to this value.
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          Returns a set view of the mappings contained in this map.
 boolean equals(java.lang.Object object)
          Compares the specified object with this map for equality.
 V get(java.lang.Object key)
          Returns the value to which this map maps the specified key.
 int hashCode()
          Returns the hash code value for this map.
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map<? extends K,? extends V> map)
          Copies all of the mappings from the specified map to this map.
 V remove(java.lang.Object key)
          Removes the mapping for this key from this map if present.
 int size()
          Returns the number of entries in this map.
 java.lang.String toString()
          Returns a string representation of this map.
 
Methods inherited from class java.util.AbstractMap
clone, isEmpty, keySet, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SoftValueHashMap

public SoftValueHashMap()
Creates a map with the default hard references count.


SoftValueHashMap

public SoftValueHashMap(int hardReferencesCount)
Creates a map with the specified hard references count.

Parameters:
hardReferencesCount - The maximal number of hard references to keep.
Method Detail

size

public int size()
Returns the number of entries in this map.

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

containsKey

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

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

containsValue

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

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

get

public V get(java.lang.Object key)
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key, or the value has been garbage collected.

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

put

public V put(K key,
             V value)
Associates the specified value with the specified key in this map.

Specified by:
put in interface java.util.Map<K,V>
Overrides:
put in class java.util.AbstractMap<K,V>
Parameters:
key - Key with which the specified value is to be associated.
value - Value to be associated with the specified key. The value can't be null.
Returns:
Previous value associated with specified key, or null if there was no mapping for key.

putAll

public void putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map.

Specified by:
putAll in interface java.util.Map<K,V>
Overrides:
putAll in class java.util.AbstractMap<K,V>
Parameters:
map - Mappings to be stored in this map.

remove

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

Specified by:
remove in interface java.util.Map<K,V>
Overrides:
remove in class java.util.AbstractMap<K,V>
Parameters:
key - Key whose mapping is to be removed from the map.
Returns:
previous value associated with specified key, or null if there was no entry for key.

clear

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

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

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns a set view of the mappings contained in this map.

Specified by:
entrySet in interface java.util.Map<K,V>
Specified by:
entrySet in class java.util.AbstractMap<K,V>

equals

public boolean equals(java.lang.Object object)
Compares the specified object with this map for equality.

Specified by:
equals in interface java.util.Map<K,V>
Overrides:
equals in class java.util.AbstractMap<K,V>
Parameters:
object - The object to compare with this map for equality.

hashCode

public int hashCode()
Returns the hash code value for this map.

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

toString

public java.lang.String toString()
Returns a string representation of this map.

Overrides:
toString in class java.util.AbstractMap<K,V>


Copyright © 1996-2010 Geotools. All Rights Reserved.