Bag implements AnyBag<Bag<T>> { @Override public void ensureCapacity(int capacity) { if (capacity < 0) throw new NegativeArraySizeException(String.valueOf(capacity)); if (this.buffer.length >= capacity) return; int newCapacity = Bag.nextPowerOfTwo(capacity); Object[] newBuffer = new Object[newCapacity]; System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); this.buffer = newBuffer; } Bag(); @Override void copyFrom(Bag<T> from); @Override void copyFrom(Bag<T> from, boolean clearExceeding); @Override void copyFrom(Bag<T> from, int length); @Override void copyFrom(Bag<T> from, int length, boolean clearExceeding); @Override void copyFrom(Bag<T> from, int fromOffset, int length); @Override void copyFrom(Bag<T> from, int fromOffset, int length, boolean clearExceeding); @Override void copyPartFrom(Bag<T> from, int fromOffset, int toOffset, int length); @Override void ensureCapacity(int capacity); T get(int index); void set(int index, T value); @Override void clear(); @Override void clear(Mask mask); @Experimental
    public Object[] buffer; }
@Test public void testNegativeArraySizeException() { Bag<T> bag = new Bag<T>(); for (int i = 0; i < 32; i++) { try { bag.ensureCapacity(-(1 << i)); } catch (NegativeArraySizeException ex) { if (ex.getClass() == NegativeArraySizeException.class) continue; } fail("NegativeArraySizeException expected for capacity " + (-(1 << i))); } }