Package mondrian.util
Class ObjectPool<T>
- java.lang.Object
-
- mondrian.util.ObjectPool<T>
-
public class ObjectPool<T> extends Object
AnObjectPoolis a low-memory replacement for aHashSet. A HashSet contains aHashMapwhich in turn has an array of Entry objects, the Entry objects themselves, and the key and value objects. An ObjectPool has simply an array of objects and the objects themselves which server as both key and value.This is like the String
internmethod, but works for an Object type and whereas the Stringinternmethod is global an ObjectPool can be used within a context and then garbage collected. Objects can not removed from an ObjectPool except by calling theclearmethod which removes all objects.Just as with a HashSet's key objects, objects to be placed into an ObjectPool must implement the
equalsandhashCodemethods.This implementation is NOT thread safe.
- Author:
- Richard Emberson
-
-
Field Summary
Fields Modifier and Type Field Description protected static intDEFAULT_CAPACITYprotected static doubleDEFAULT_MAX_LOAD_FACTORprotected static doubleDEFAULT_MIN_LOAD_FACTORprotected intdistinctThe number of distinct associations in the map; its "size()".protected static byteFREEprotected intfreeEntriesThe number of table entries in state==FREE.protected static byteFULLprotected inthighWaterMarkprotected doublemaxLoadFactorThe maximum load factor for the hashtable.protected doubleminLoadFactorThe minimum load factor for the hashtable.protected T[]values
-
Constructor Summary
Constructors Constructor Description ObjectPool()ObjectPool(int initialCapacity)ObjectPool(int initialCapacity, double minLoadFactor, double maxLoadFactor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tadd(T key)Adds an object to the ObjectPool if it is not already in the pool or returns the object that is already in the pool that matches the object being added.protected intchooseGrowCapacity(int size, double minLoad, double maxLoad)protected intchooseHighWaterMark(int capacity, double maxLoad)protected intchooseLowWaterMark(int capacity, double minLoad)voidclear()Removes all objects from the pool but keeps the current size of the internal storage.booleancontains(T key)Returns true it the Object is already in the ObjectPool and false otherwise.protected booleanequals(T t, T key)protected inthash(T key)protected intindexOfInsertion(T key)Iterator<T>iterator()Returns an Iterator of thisObjectPool.protected intnextPrime(int desiredCapacity)protected voidrehash(int newCapacity)protected voidsetUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)intsize()Return the number of entries in the ObjectPool.voidtrimToSize()Reduce the size of the internal arrays to a size just big enough to hold the current set of entries.
-
-
-
Field Detail
-
FREE
protected static final byte FREE
- See Also:
- Constant Field Values
-
FULL
protected static final byte FULL
- See Also:
- Constant Field Values
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
DEFAULT_MIN_LOAD_FACTOR
protected static final double DEFAULT_MIN_LOAD_FACTOR
- See Also:
- Constant Field Values
-
DEFAULT_MAX_LOAD_FACTOR
protected static final double DEFAULT_MAX_LOAD_FACTOR
- See Also:
- Constant Field Values
-
distinct
protected int distinct
The number of distinct associations in the map; its "size()".
-
highWaterMark
protected int highWaterMark
-
minLoadFactor
protected double minLoadFactor
The minimum load factor for the hashtable.
-
maxLoadFactor
protected double maxLoadFactor
The maximum load factor for the hashtable.
-
values
protected T[] values
-
freeEntries
protected int freeEntries
The number of table entries in state==FREE.
-
-
Method Detail
-
size
public int size()
Return the number of entries in the ObjectPool.- Returns:
- number of entries.
-
trimToSize
public void trimToSize()
Reduce the size of the internal arrays to a size just big enough to hold the current set of entries. Generally, this should only be called after all entries have been added. Calling this causes a new, smaller array to be allocated, the objects are copied to the new array and then the old array is free to be garbage collected; there is a small time when both arrays are in memory.
-
contains
public boolean contains(T key)
Returns true it the Object is already in the ObjectPool and false otherwise.- Parameters:
key- Object to test if member already or not.- Returns:
- true is already member
-
add
public T add(T key)
Adds an object to the ObjectPool if it is not already in the pool or returns the object that is already in the pool that matches the object being added.- Parameters:
key- Object to add to pool- Returns:
- Equivalent object, if it exists, otherwise key
-
clear
public void clear()
Removes all objects from the pool but keeps the current size of the internal storage.
-
iterator
public Iterator<T> iterator()
Returns an Iterator of thisObjectPool. The order of the Objects returned by theIteratorcan not be counted on to be in the same order as they were inserted into theObjectPool. TheIteratorreturned does not support the removal ofObjectPoolmembers.
-
chooseGrowCapacity
protected int chooseGrowCapacity(int size, double minLoad, double maxLoad)
-
chooseHighWaterMark
protected final int chooseHighWaterMark(int capacity, double maxLoad)
-
chooseLowWaterMark
protected final int chooseLowWaterMark(int capacity, double minLoad)
-
nextPrime
protected int nextPrime(int desiredCapacity)
-
setUp
protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
-
hash
protected int hash(T key)
-
indexOfInsertion
protected int indexOfInsertion(T key)
-
rehash
protected void rehash(int newCapacity)
-
-