blob: 488b70860197cb3ecda2e965e9790d8fc11a315e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package makamys.neodymium.util;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class RecyclingList<T> {
private Supplier<T> constructor;
int nextIndex;
private List<T> list;
public RecyclingList(Supplier<T> constructor) {
this.constructor = constructor;
this.list = new ArrayList<T>();
}
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 remove() {
if(nextIndex == 0) {
throw new IllegalStateException("Tried to remove from empty list");
}
nextIndex--;
}
public boolean isEmpty() {
return nextIndex == 0;
}
public void reset() {
nextIndex = 0;
}
public List<T> getAsList() {
return list.subList(0, nextIndex);
}
}
|