package makamys.neodymium.util; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; public class RecyclingList { private Supplier constructor; int nextIndex; private List list; public RecyclingList(Supplier constructor) { this.constructor = constructor; this.list = new ArrayList(); } public T get(int i) { while(list.size() <= i) { list.add(constructor.get()); } return list.get(i); } public T next() { return get(nextIndex++); } public void reset() { nextIndex = 0; } public List getAsList() { return list.subList(0, nextIndex); } }