ArrayBuilder { @SuppressWarnings({"unchecked"}) public ArrayBuilder<T> add(T... elements) { if (elements == null) return this; if (array == null) { array = elements; return this; } T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + elements.length); System.arraycopy(array, 0, newArray, 0, array.length); System.arraycopy(elements, 0, newArray, array.length, elements.length); array = newArray; return this; } ArrayBuilder();  ArrayBuilder(T... elements); T[] get(); @SuppressWarnings({"unchecked"}) ArrayBuilder<T> add(T... elements); @SuppressWarnings({"unchecked"}) ArrayBuilder<T> addNonNulls(T... elements);  }
@Test public void testAdd() throws Exception { assertEquals(new ArrayBuilder<Integer>().add(1, 2).add(3).add(4, 5, 6).get(), new Integer[] {1, 2, 3, 4, 5, 6}); assertEquals(new ArrayBuilder<Integer>().add(1, 2).addNonNulls(3, null, 4, null).get(), new Integer[] {1, 2, 3, 4}); }