Package org.ka2ddo.util
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
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 Summary
ModifierConstructorDescriptionprotected
Constructs a new, emptyShareableObjectCache
with the default initial capacity and load factor.protected
ShareableObjectCache
(int initialCapacity) Constructs a new, emptyShareableObjectCache
with the given initial capacity and the default load factor (2.0).protected
ShareableObjectCache
(int initialCapacity, float loadFactor) Constructs a new, emptyShareableObjectCache
with the given initial capacity and the given load factor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Associates the specified value with the specified key in this map.void
clear()
Removes all of the mappings from this map.protected abstract Class<?>
getType()
Get data type of the objects stored in this ShareableObjectCache.Look for the specified key in the cache.boolean
isEmpty()
Returnstrue
if this set contains no entries.iterator()
Returns an iterator over the elements contained in this collection.boolean
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.toString()
Returns a string description 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.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
addAll, contains, containsAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
ShareableObjectCache
protected ShareableObjectCache(int initialCapacity, float loadFactor) Constructs a new, emptyShareableObjectCache
with the given initial capacity and the given load factor.- Parameters:
initialCapacity
- The initial capacity of theShareableObjectCache
loadFactor
- The load factor of theShareableObjectCache
- Throws:
IllegalArgumentException
- if the initial capacity is negative, or if the load factor is nonpositive.
-
ShareableObjectCache
protected ShareableObjectCache(int initialCapacity) Constructs a new, emptyShareableObjectCache
with the given initial capacity and the default load factor (2.0).- Parameters:
initialCapacity
- The initial capacity of theShareableObjectCache
- Throws:
IllegalArgumentException
- if the initial capacity is negative
-
ShareableObjectCache
protected ShareableObjectCache()Constructs a new, emptyShareableObjectCache
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 interfaceCollection<T>
- Specified by:
size
in interfaceSet<T>
- Specified by:
size
in classAbstractCollection<T>
-
isEmpty
public boolean isEmpty()Returnstrue
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 interfaceCollection<T>
- Specified by:
isEmpty
in interfaceSet<T>
- Overrides:
isEmpty
in classAbstractCollection<T>
-
add
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 interfaceCollection<T>
- Specified by:
add
in interfaceSet<T>
- Overrides:
add
in classAbstractCollection<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
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
Removes the entry for a key from this cache if it is present. More formally, if this cache contains an entry for keyk
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 ofnull
does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key tonull
.The map will not contain a mapping for the specified key once the call returns.
- Specified by:
remove
in interfaceCollection<T>
- Specified by:
remove
in interfaceSet<T>
- Overrides:
remove
in classAbstractCollection<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 interfaceCollection<T>
- Specified by:
clear
in interfaceSet<T>
- Overrides:
clear
in classAbstractCollection<T>
-
getType
Get data type of the objects stored in this ShareableObjectCache.- Returns:
- Class of stored objects
-
toString
Returns a string description of this collection.- Overrides:
toString
in classAbstractCollection<T>
- Returns:
- a string description of this collection
-
iterator
Returns an iterator over the elements contained in this collection.
-