Class ShareableObjectCache<T>

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

public abstract class ShareableObjectCache<T> extends 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.
Author:
Andrew Pavlin, KA2DDO
  • Constructor Details

    • 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:
      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:
      IllegalArgumentException - if the initial capacity is negative
    • ShareableObjectCache

      protected ShareableObjectCache()
      Constructs a new, empty ShareableObjectCache with the default initial capacity and load factor.
  • Method Details

    • 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 Collection<T>
      Specified by:
      size in interface Set<T>
      Specified by:
      size in class 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 Collection<T>
      Specified by:
      isEmpty in interface Set<T>
      Overrides:
      isEmpty in class 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 Collection<T>
      Specified by:
      add in interface Set<T>
      Overrides:
      add in class 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)
      Look for the specified key in the cache.
      Parameters:
      t - T hashable object
      Returns:
      cached equal-value of t (or t itself if never listed in cache before)
    • remove

      public boolean remove(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 Collection<T>
      Specified by:
      remove in interface Set<T>
      Overrides:
      remove in class 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 Collection<T>
      Specified by:
      clear in interface Set<T>
      Overrides:
      clear in class AbstractCollection<T>
    • getType

      protected abstract Class<?> getType()
      Get data type of the objects stored in this ShareableObjectCache.
      Returns:
      Class of stored objects
    • toString

      public String toString()
      Returns a string description of this collection.
      Overrides:
      toString in class AbstractCollection<T>
      Returns:
      a string description of this collection
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the elements contained in this collection.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface Set<T>
      Specified by:
      iterator in class AbstractCollection<T>
      Returns:
      an iterator over the elements contained in this collection