Package: groovy.lang

## [Java] Class ListWithDefault<T>

- groovy.lang.ListWithDefault

- All Implemented Interfaces and Traits:
    List

      public final class ListWithDefault<T>
      extends Object
      implements List

  A wrapper for List which automatically grows the list when either get(int) or getAt(int) is called with an index greater than or equal to `size()`.

  Since:
    1.8.7

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public boolean` | `add(T t)`
    `` | `public void` | `add(int i, T t)`
    `` | `public boolean` | `addAll(Collection<? extends T> ts)`
    `` | `public boolean` | `addAll(int i, Collection<? extends T> ts)`
    `` | `public void` | `clear()`
    `` | `public boolean` | `contains(Object o)`
    `` | `public boolean` | `containsAll(Collection<?> objects)`
    `` | `public boolean` | `equals(Object obj)`
    `` | `public T` | `get(int index)` Returns the element at the given index but grows the list if needed.
    `` | `public T` | `getAt(int index)` Overwrites subscript operator handling by redirecting to get(int).
    `` | `public List<T>` | `getDelegate()`
    `` | `public Closure` | `getInitClosure()`
    `` | `public int` | `hashCode()`
    `` | `public int` | `indexOf(Object o)`
    `` | `public boolean` | `isEmpty()`
    `` | `public boolean` | `isLazyDefaultValues()`
    `` | `public Iterator<T>` | `iterator()`
    `` | `public int` | `lastIndexOf(Object o)`
    `` | `public ListIterator<T>` | `listIterator()`
    `` | `public ListIterator<T>` | `listIterator(int i)`
    `<T>` | `public static ListWithDefault<T>` | `newInstance(List<T> items, boolean lazyDefaultValues, Closure initClosure)`
    `` | `public boolean` | `remove(Object o)`
    `` | `public T` | `remove(int i)`
    `` | `public boolean` | `removeAll(Collection<?> objects)`
    `` | `public boolean` | `retainAll(Collection<?> objects)`
    `` | `public T` | `set(int i, T t)`
    `` | `public int` | `size()`
    `` | `public ListWithDefault<T>` | `subList(int fromIndex, int toIndex)` Returns a view of a portion of this list.
    `` | `public Object[]` | `toArray()`
    `<U>` | `public U[]` | `toArray(U[] ts)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll`

- - ### Method Detail

    - #### @Override
      public boolean add(T t)

    - #### @Override
      public void add(int i, T t)

    - #### @Override
      public boolean addAll(Collection<? extends T> ts)

    - #### @Override
      public boolean addAll(int i, Collection<? extends T> ts)

    - #### @Override
      public void clear()

    - #### @Override
      public boolean contains(Object o)

    - #### @Override
      public boolean containsAll(Collection<?> objects)

    - #### @Override
      public boolean equals(Object obj)

    - #### @Override
      public T get(int index)

      Returns the element at the given index but grows the list if needed. If the requested `index` is greater than or equal to `size()`, the list will grow to the new size and a default value calculated using the `initClosure` will be used to populate the missing value and returned.

      If `lazyDefaultValues` is `true` any gaps when growing the list are filled with nulls. Subsequent attempts to retrieve items from the list from those gap index values will, upon finding null, call the `initClosure` to populate the list for the given list value. Hence, when in this mode, nulls cannot be stored in this list. If `lazyDefaultValues` is `false` any gaps when growing the list are filled eagerly by calling the `initClosure` for all gap indexes during list growth. No calls to `initClosure` are made except during list growth and it is ok to store null values in the list when in this mode.

      This implementation breaks the contract of List.get as it a) possibly modifies the underlying list and b) does NOT throw an IndexOutOfBoundsException when `index < 0 || index >= size()`.

      Parameters:
        `index` - an index (might be greater or equal to `size()`, or smaller than 0)
      Returns:
        the value at the given `index` or the default value

    - #### public T getAt(int index)

      Overwrites subscript operator handling by redirecting to get(int).

      Parameters:
        `index` - an index (might be greater or equal to `size()`, or smaller than 0)
      Returns:
        the value at the given `index` or the default value

    - #### public List<T> getDelegate()

    - #### public Closure getInitClosure()

    - #### @Override
      public int hashCode()

    - #### @Override
      public int indexOf(Object o)

    - #### @Override
      public boolean isEmpty()

    - #### public boolean isLazyDefaultValues()

    - #### @Override
      public Iterator<T> iterator()

    - #### @Override
      public int lastIndexOf(Object o)

    - #### @Override
      public ListIterator<T> listIterator()

    - #### @Override
      public ListIterator<T> listIterator(int i)

    - #### <T> public static ListWithDefault<T> newInstance(List<T> items, boolean lazyDefaultValues, Closure initClosure)

    - #### @Override
      public boolean remove(Object o)

    - #### @Override
      public T remove(int i)

    - #### @Override
      public boolean removeAll(Collection<?> objects)

    - #### @Override
      public boolean retainAll(Collection<?> objects)

    - #### @Override
      public T set(int i, T t)

    - #### @Override
      public int size()

    - #### @Override
      public ListWithDefault<T> subList(int fromIndex, int toIndex)

      Returns a view of a portion of this list. This method returns a list with the same lazy list settings as the original list.

      Parameters:
        `fromIndex` - low endpoint of the subList (inclusive)
        `toIndex` - upper endpoint of the subList (exclusive)
      Returns:
        a view of a specified range within this list, keeping all lazy list settings

    - #### @Override
      public Object[] toArray()

    - #### <U> @Override
      public U[] toArray(U[] ts)