/** * Constructs an extensible array of primitive type elements, backed by the * given fixed-size array. * * @param array the array to wrap * @param type the class of the primitive type */ public AbstractPrimitiveArray(final Class<BaseType> type, final ArrayType array) { this.type = type; setArray(array); size = capacity(); }
/** * Constructs an extensible array of primitive type elements, backed by the * given fixed-size array. * * @param array the array to wrap * @param type the class of the primitive type */ public AbstractPrimitiveArray(final Class<BaseType> type, final ArrayType array) { this.type = type; setArray(array); size = capacity(); }
@Override public void ensureCapacity(final int minCapacity) { final int oldCapacity = capacity(); if (minCapacity <= oldCapacity) return; // no need to grow // grow the array by up to 50% (plus a small constant) final int growth = Math.min(oldCapacity / 2 + 16, maximumGrowth); final int newCapacity; if (growth > Integer.MAX_VALUE - oldCapacity) { // growth would push array over the maximum array size newCapacity = Integer.MAX_VALUE; } else newCapacity = oldCapacity + growth; // ensure the array grows by at least the requested minimum capacity final int newLength = Math.max(minCapacity, newCapacity); // copy the data into a new array setArray(copyArray(newLength)); }
@Override public void ensureCapacity(final int minCapacity) { final int oldCapacity = capacity(); if (minCapacity <= oldCapacity) return; // no need to grow // grow the array by up to 50% (plus a small constant) final int growth = Math.min(oldCapacity / 2 + 16, maximumGrowth); final int newCapacity; if (growth > Integer.MAX_VALUE - oldCapacity) { // growth would push array over the maximum array size newCapacity = Integer.MAX_VALUE; } else newCapacity = oldCapacity + growth; // ensure the array grows by at least the requested minimum capacity final int newLength = Math.max(minCapacity, newCapacity); // copy the data into a new array setArray(copyArray(newLength)); }