org.ka2ddo.util
Class ShareableObjectCache<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<T>
          extended by org.ka2ddo.util.ShareableObjectCache<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>
Direct Known Subclasses:
StringCache

public abstract class ShareableObjectCache<T>
extends java.util.AbstractSet<T>

This class provides an alternative to the Java PermGen heap section used for String.intern(), such that applications won't run out of Java PermGen space while still being able to share constant object declarations.


Constructor Summary
protected ShareableObjectCache()
          Constructs a new, empty ShareableObjectCache with the default initial capacity (16) and load factor (2.0).
protected ShareableObjectCache(int initialCapacity)
          Constructs a new, empty ShareableObjectCache with the given initial capacity and the default load factor (2.0).
protected ShareableObjectCache(int initialCapacity, float loadFactor)
          Constructs a new, empty ShareableObjectCache with the given initial capacity and the given load factor.
 
Method Summary
 boolean add(T key)
          Associates the specified value with the specified key in this map.
 void clear()
          Removes all of the mappings from this map.
protected abstract  java.lang.Class getType()
           
 T internKey(T t)
           
 boolean isEmpty()
          Returns true if this set contains no entries.
 java.util.Iterator<T> iterator()
          Returns an iterator over the elements contained in this collection.
 boolean remove(java.lang.Object key)
          Removes the entry for a key from this cache if it is present.
 int size()
          Returns the number of key-value mappings in this map.
 java.lang.String toString()
          Returns a string representation of this collection.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, contains, containsAll, retainAll, toArray, toArray
 

Constructor Detail

ShareableObjectCache

protected ShareableObjectCache(int initialCapacity,
                               float loadFactor)
Constructs a new, empty ShareableObjectCache with the given initial capacity and the given load factor.

Parameters:
initialCapacity - The initial capacity of the ShareableObjectCache
loadFactor - The load factor of the ShareableObjectCache
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative, or if the load factor is nonpositive.

ShareableObjectCache

protected ShareableObjectCache(int initialCapacity)
Constructs a new, empty ShareableObjectCache with the given initial capacity and the default load factor (2.0).

Parameters:
initialCapacity - The initial capacity of the ShareableObjectCache
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative

ShareableObjectCache

protected ShareableObjectCache()
Constructs a new, empty ShareableObjectCache with the default initial capacity (16) and load factor (2.0).

Method Detail

size

public int size()
Returns the number of key-value mappings in this map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.

Specified by:
size in interface java.util.Collection<T>
Specified by:
size in interface java.util.Set<T>
Specified by:
size in class java.util.AbstractCollection<T>

isEmpty

public boolean isEmpty()
Returns true if this set contains no entries. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.

Specified by:
isEmpty in interface java.util.Collection<T>
Specified by:
isEmpty in interface java.util.Set<T>
Overrides:
isEmpty in class java.util.AbstractCollection<T>

add

public boolean add(T key)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is preserved (since it is a duplicate, by the definition and purpose of this class).

Specified by:
add in interface java.util.Collection<T>
Specified by:
add in interface java.util.Set<T>
Overrides:
add in class java.util.AbstractCollection<T>
Parameters:
key - key with which the specified value is to be associated.
Returns:
boolean true if this was a new entry in the cache

internKey

public T internKey(T t)

remove

public boolean remove(java.lang.Object key)
Removes the entry for a key from this cache if it is present. More formally, if this cache contains an entry for key k such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)

Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. A return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.

The map will not contain a mapping for the specified key once the call returns.

Specified by:
remove in interface java.util.Collection<T>
Specified by:
remove in interface java.util.Set<T>
Overrides:
remove in class java.util.AbstractCollection<T>
Parameters:
key - key whose mapping is to be removed from the map
Returns:
boolean true if an entry formerly existed matching the key

clear

public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.

Specified by:
clear in interface java.util.Collection<T>
Specified by:
clear in interface java.util.Set<T>
Overrides:
clear in class java.util.AbstractCollection<T>

getType

protected abstract java.lang.Class getType()

toString

public java.lang.String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Overrides:
toString in class java.util.AbstractCollection<T>
Returns:
a string representation of this collection

iterator

public java.util.Iterator<T> iterator()
Returns an iterator over the elements contained in this collection.

Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>
Specified by:
iterator in interface java.util.Set<T>
Specified by:
iterator in class java.util.AbstractCollection<T>
Returns:
an iterator over the elements contained in this collection