/** * 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(); }
/** * Shifts the array to delete space starting at a specified index. * * @param index the index where the space should be deleted * @param count the number of values to delete */ @Override public void delete(final int index, final int count) { int oldSize = size(); if (index < 0 || index > oldSize) { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } if (index < 0 || index + count > oldSize) { throw new IllegalArgumentException("Invalid range: index=" + index + ", count=" + count + ", size=" + oldSize); } setSize(oldSize - count); if (index + count < oldSize) { final ArrayType array = getArray(); System.arraycopy(array, index + count, array, index, oldSize - index - count); } }
@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 setSize(final int size) { ensureCapacity(size); this.size = size; }
/** Gets the current capacity of the backing array. */ @Override public int capacity() { final ArrayType array = getArray(); return array == null ? 0 : Array.getLength(array); }
/** Checks that the index is less than the size of the array. */ protected void checkBounds(final int index) { if (index >= size()) { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } }
@Override public void clear() { setSize(0); }
/** * Shifts the array to delete space starting at a specified index. * * @param index the index where the space should be deleted * @param count the number of values to delete */ @Override public void delete(final int index, final int count) { int oldSize = size(); if (index < 0 || index > oldSize) { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } if (index < 0 || index + count > oldSize) { throw new IllegalArgumentException("Invalid range: index=" + index + ", count=" + count + ", size=" + oldSize); } setSize(oldSize - count); if (index + count < oldSize) { final ArrayType array = getArray(); System.arraycopy(array, index + count, array, index, oldSize - index - count); } }
@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)); }
/** * Constructs an extensible array of primitive type elements, backed by a * fixed-size array. * * @param type the class of the primitive type * @param size the initial size */ public AbstractPrimitiveArray(final Class<BaseType> type, final int size) { this.type = type; this.size = size; ensureCapacity(size); }
/** Gets the current capacity of the backing array. */ @Override public int capacity() { final ArrayType array = getArray(); return array == null ? 0 : Array.getLength(array); }
/** Checks that the index is less than the size of the array. */ protected void checkBounds(final int index) { if (index >= size()) { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } }
@Override public void clear() { setSize(0); }
/** * Shifts the array to insert space at a specified index. * * @param index the index where the space should be inserted * @param count the number of values to insert */ @Override public void insert(final int index, final int count) { int oldSize = size(); if (index < 0 || index > oldSize) { throw new ArrayIndexOutOfBoundsException("Invalid index value"); } if (count > Integer.MAX_VALUE - oldSize) { // insertion would push array over the maximum size throw new IllegalArgumentException("Too many elements"); } if (count <= 0) { throw new IllegalArgumentException("Count must be positive"); } setSize(oldSize + count); if (index < oldSize) { final ArrayType array = getArray(); System.arraycopy(array, index, array, index + count, oldSize - index); } }