28 lines
719 B
Java
28 lines
719 B
Java
package be.jeffcheasey88.peeratcode.parser.java;
|
|
|
|
import java.lang.reflect.Array;
|
|
import java.util.Iterator;
|
|
|
|
public class ArrayBuffer<E>{
|
|
|
|
private E[] elements;
|
|
|
|
public ArrayBuffer(java.lang.Class<?> type){
|
|
this.elements = (E[]) Array.newInstance(type, 0);
|
|
}
|
|
|
|
public void add(E e){
|
|
E[] copy = (E[]) Array.newInstance(e.getClass(), this.elements.length+1);
|
|
System.arraycopy(elements, 0, copy, 0, elements.length);
|
|
copy[elements.length] = e;
|
|
this.elements = copy;
|
|
}
|
|
|
|
public void append(java.util.function.Function<E, E> modifier){
|
|
this.elements[elements.length-1] = modifier.apply(this.elements[elements.length-1]);
|
|
}
|
|
|
|
public Iterator<E> iterator(){
|
|
return new ArrayIterator<E>(elements);
|
|
}
|
|
}
|