Class ResizingArrayStorage<T>

  • Type Parameters:
    T - element class.
    All Implemented Interfaces:
    CapacityManagement

    public final class ResizingArrayStorage<T>
    extends Object
    implements CapacityManagement
    Class that provides a resizable array storage of a certain type.
    • Field Detail

      • DEFAULT_INITIAL_CAPACITY

        public static final int DEFAULT_INITIAL_CAPACITY
        The default initial capacity of the array storage.
        See Also:
        Constant Field Values
      • array

        public T[] array
    • Constructor Detail

      • ResizingArrayStorage

        public ResizingArrayStorage​(Class<? super T> arrayClazz)
        Constructor. Creates an array storage with a default initial capacity of DEFAULT_INITIAL_CAPACITY.
        Parameters:
        arrayClazz - the class of the storage array.
      • ResizingArrayStorage

        public ResizingArrayStorage​(Class<? super T> arrayClazz,
                                    int initialCapacity)
        Constructor. Creates an array with the specified initial capacity.
        Parameters:
        arrayClazz - the class of the storage array.
        initialCapacity - the initial capacity.
      • ResizingArrayStorage

        public ResizingArrayStorage​(ResizingArrayStorage<T> other)
        Copy-constructor which (shallowly) clones the storage of the other ResizingArrayStorage.
        Parameters:
        other - the other storage whose data should be (shallowly) cloned
    • Method Detail

      • ensureCapacity

        public boolean ensureCapacity​(int minCapacity)
        Description copied from interface: CapacityManagement
        Ensures that the internal storage has room for at least the provided number of elements.
        Specified by:
        ensureCapacity in interface CapacityManagement
        Parameters:
        minCapacity - the minimal number of elements the storage should have room for.
        Returns:
        true iff the internal storage had to be resized, false otherwise.
      • ensureAdditionalCapacity

        public boolean ensureAdditionalCapacity​(int additionalCapacity)
        Description copied from interface: CapacityManagement
        Ensures that the internal storage has room for at least the provided number of additional elements.

        Calling this method is equivalent to calling the above CapacityManagement.ensureCapacity(int) with an argument of size() + additionalCapacity.

        Specified by:
        ensureAdditionalCapacity in interface CapacityManagement
        Parameters:
        additionalCapacity - the number of additional elements the storage should have room for.
        Returns:
        true iff the internal storage had to be resized, false otherwise.
      • hintNextCapacity

        public void hintNextCapacity​(int nextCapacityHint)
        Description copied from interface: CapacityManagement
        Gives a hint regarding the capacity that should be reserved when resizing the internal storage for the next time. This method acts like a "lazy" CapacityManagement.ensureCapacity(int), i.e. it reserves the specified capacity at the time the next resizing of the internal storage is performed.

        This method is useful when a not too imprecise upper bound on the elements that will in consequence be added is known. Since the actual number of elements added may be lower than the specified upper bound, a resizing that would have been performed by CapacityManagement.ensureCapacity(int) might not be necessary.

        Specified by:
        hintNextCapacity in interface CapacityManagement
        Parameters:
        nextCapacityHint - the next capacity hint.
      • shrink

        public boolean shrink​(int maxCapacity)
        Shrinks the storage to the specified maximum capacity.

        If the current capacity is less or equal to the specified capacity, nothing happens.

        Parameters:
        maxCapacity - the maximal number of elements the storage array has to provide room for.
        Returns:
        true iff the storage array had to be resized, false otherwise.
      • setAll

        public void setAll​(T value)
        Sets all the elements in the array to the specified value.
        Parameters:
        value - the value.