package NET.worlds.scape; import NET.worlds.console.Main; import NET.worlds.console.MainCallback; public class Recycler implements MainCallback { private static int minCapacity = 50; private Object[] list = new Object[minCapacity]; private int numFilled = 0; private int numAllocked = 0; private int maxAllockedRecently = 0; private int howRecently = 0; Recycler() { Main.register(this); } private void resizeTo(int newCapacity) { assert newCapacity > this.numAllocked; if (newCapacity < minCapacity) { newCapacity = minCapacity; } if (newCapacity < this.list.length && this.list.length < 2 * newCapacity) { while (this.numFilled > newCapacity) { this.list[--this.numFilled] = null; } } else { Object[] newList = new Object[newCapacity]; try { System.arraycopy(this.list, 0, newList, 0, this.numAllocked); } catch (Exception var4) { throw new Error(var4.toString()); } this.list = newList; this.numFilled = this.numAllocked; } } @Override public void mainCallback() { if (this.numAllocked > this.maxAllockedRecently) { this.maxAllockedRecently = this.numAllocked; } if (++this.howRecently == 1000) { int newCapacity = this.maxAllockedRecently + minCapacity; if (newCapacity < this.list.length) { this.resizeTo(newCapacity); } this.howRecently = 0; this.maxAllockedRecently = this.numAllocked; } this.numAllocked = 0; } public Object alloc() { assert Main.isMainThread(); return this.numAllocked == this.numFilled ? null : this.list[this.numAllocked++]; } public void recycle(Object o) { if (this.numFilled == this.list.length) { this.resizeTo(this.list.length * 2); } this.list[this.numFilled++] = o; } }