com.unit16.z.Pool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unit16-z Show documentation
Show all versions of unit16-z Show documentation
A collection of utilities for representing and manipulating time series and such.
package com.unit16.z;
import java.util.ArrayList;
/**
* @param type of cached element
*/
public abstract class Pool {
private final ArrayList cache_ = new ArrayList<>(1);
/**
* @param val to be returned to the pool
*/
public final void recycle(D val)
{
assert !cache_.contains(val);
cache_.add(val);
}
/**
* @return an element from the pool, creating one if the pool is empty.
*/
public final D draw()
{
if (cache_.isEmpty())
{
return create();
}
else
{
return cache_.remove(cache_.size() - 1);
}
}
/**
* Factory method for new objects.
* @return a newly instantiated object
*/
protected abstract D create();
}