Package net.ssehub.easy.basics.pool
Class Pool<T>
- java.lang.Object
-
- net.ssehub.easy.basics.pool.Pool<T>
-
- Type Parameters:
T- the type of instances to be managed
public class Pool<T> extends java.lang.ObjectImplements a simple pool for explicit memory management of frequently created elements. In order to keep interfaces of instances clean, we delegate creation and clear operation to theIPoolManagerdelegate.- Author:
- Holger Eichelberger
-
-
Field Summary
Fields Modifier and Type Field Description private IPoolManager<T>managerprivate java.util.List<T>pool
-
Constructor Summary
Constructors Constructor Description Pool(IPoolManager<T> manager)Creates a new pool for a given type of instances represented by its pool manager.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleancontainsByRef(T instance)Returns whether the pool already containsinstance.TgetInstance()Returns an instance from the pool or creates a new instance via themanager.voidreleaseInstance(T instance)
-
-
-
Field Detail
-
manager
private IPoolManager<T> manager
-
pool
private java.util.List<T> pool
-
-
Constructor Detail
-
Pool
public Pool(IPoolManager<T> manager)
Creates a new pool for a given type of instances represented by its pool manager.- Parameters:
manager- the pool manager
-
-
Method Detail
-
getInstance
public T getInstance()
Returns an instance from the pool or creates a new instance via themanager.- Returns:
- an instance
-
releaseInstance
public void releaseInstance(T instance)
- Parameters:
instance- the instance to be released (ignored if null)
-
containsByRef
private boolean containsByRef(T instance)
Returns whether the pool already containsinstance.- Parameters:
instance- the instance to look for (by reference)- Returns:
trueif the reference is already there,falseelse
-
-