All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unit16.z.Pool Maven / Gradle / Ivy

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();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy