public class ObjectPool<T> extends Object
ObjectPool
is a low-memory replacement for a
HashSet
. A HashSet contains a HashMap
which 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 intern
method, but works for
an Object type and whereas the String intern
method is global
an ObjectPool can be used within a context and then garbage collected.
Objects can not removed from an ObjectPool except by calling the
clear
method which removes all objects.
Just as with a HashSet's key objects, objects to be placed into
an ObjectPool must implement the equals
and
hashCode
methods.
This implementation is NOT thread safe.
Modifier and Type | Field and Description |
---|---|
protected static int |
DEFAULT_CAPACITY |
protected static double |
DEFAULT_MAX_LOAD_FACTOR |
protected static double |
DEFAULT_MIN_LOAD_FACTOR |
protected int |
distinct
The number of distinct associations in the map; its "size()".
|
protected static byte |
FREE |
protected int |
freeEntries
The number of table entries in state==FREE.
|
protected static byte |
FULL |
protected int |
highWaterMark |
protected double |
maxLoadFactor
The maximum load factor for the hashtable.
|
protected double |
minLoadFactor
The minimum load factor for the hashtable.
|
protected T[] |
values |
Constructor and Description |
---|
ObjectPool() |
ObjectPool(int initialCapacity) |
ObjectPool(int initialCapacity,
double minLoadFactor,
double maxLoadFactor) |
Modifier and Type | Method and Description |
---|---|
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.
|
protected int |
chooseGrowCapacity(int size,
double minLoad,
double maxLoad) |
protected int |
chooseHighWaterMark(int capacity,
double maxLoad) |
protected int |
chooseLowWaterMark(int capacity,
double minLoad) |
void |
clear()
Removes all objects from the pool but keeps the current size of
the internal storage.
|
boolean |
contains(T key)
Returns true it the Object is already in the ObjectPool and false
otherwise.
|
protected boolean |
equals(T t,
T key) |
protected int |
hash(T key) |
protected int |
indexOfInsertion(T key) |
Iterator<T> |
iterator()
Returns an Iterator of this
ObjectPool . |
protected int |
nextPrime(int desiredCapacity) |
protected void |
rehash(int newCapacity) |
protected void |
setUp(int initialCapacity,
double minLoadFactor,
double maxLoadFactor) |
int |
size()
Return the number of entries in the ObjectPool.
|
void |
trimToSize()
Reduce the size of the internal arrays to a size just big
enough to hold the current set of entries.
|
protected static final byte FREE
protected static final byte FULL
protected static final int DEFAULT_CAPACITY
protected static final double DEFAULT_MIN_LOAD_FACTOR
protected static final double DEFAULT_MAX_LOAD_FACTOR
protected int distinct
protected int highWaterMark
protected double minLoadFactor
protected double maxLoadFactor
protected T[] values
protected int freeEntries
public ObjectPool()
public ObjectPool(int initialCapacity)
public ObjectPool(int initialCapacity, double minLoadFactor, double maxLoadFactor)
public int size()
public void trimToSize()
public boolean contains(T key)
key
- Object to test if member already or not.public T add(T key)
key
- Object to add to poolpublic void clear()
public Iterator<T> iterator()
ObjectPool
. The order of
the Objects returned by the Iterator
can not be
counted on to be in the same order as they were inserted
into the ObjectPool
. The
Iterator
returned does not
support the removal of ObjectPool
members.protected int chooseGrowCapacity(int size, double minLoad, double maxLoad)
protected final int chooseHighWaterMark(int capacity, double maxLoad)
protected final int chooseLowWaterMark(int capacity, double minLoad)
protected int nextPrime(int desiredCapacity)
protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
protected int hash(T key)
protected int indexOfInsertion(T key)
protected void rehash(int newCapacity)
Copyright © 2020 Hitachi Vantara. All rights reserved.