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

jtransc.game.util.StackPool Maven / Gradle / Ivy

package jtransc.game.util;

import java.util.ArrayList;

public class StackPool {
	public interface Generator {
		T generate(int index);
	}

	final int capacity;
	final Generator generator;
	private ArrayList values = new ArrayList();
	private int index = 0;

	public StackPool(int capacity, Generator generator) {
		this.capacity = capacity;
		this.generator = generator;
		ensure(capacity);
	}

	private void ensure(int count) {
		while (values.size() < count) values.add(generator.generate(values.size()));
	}

	public int getLength() {
		return index;
	}

	public T push() {
		ensure(index);
		return values.get(index++);
	}

	public T pop() {
		return values.get(--index);
	}

	public void clear() {
		index = 0;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy