groovy.lang

## Class ListWithDefault<T>

- java.lang.Object
- - groovy.lang.ListWithDefault<T>

- All Implemented Interfaces:
    Iterable<T>, Collection<T>, List<T>

      public final class ListWithDefault<T>
      extends Object
      implements List<T>

  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

- - ### Method Summary

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

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.List

      `replaceAll, sort, spliterator`

    - ### Methods inherited from interface java.util.Collection

      `parallelStream, removeIf, stream`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach`

- - ### Method Detail

    - #### getDelegate

          public List<T> getDelegate()

    - #### isLazyDefaultValues

          public boolean isLazyDefaultValues()

    - #### getInitClosure

          public Closure getInitClosure()

    - #### newInstance

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

    - #### size

          public int size()

      Specified by:
        `size` in interface `Collection<T>`
      Specified by:
        `size` in interface `List<T>`

    - #### isEmpty

          public boolean isEmpty()

      Specified by:
        `isEmpty` in interface `Collection<T>`
      Specified by:
        `isEmpty` in interface `List<T>`

    - #### contains

          public boolean contains(Object o)

      Specified by:
        `contains` in interface `Collection<T>`
      Specified by:
        `contains` in interface `List<T>`

    - #### iterator

          public Iterator<T> iterator()

      Specified by:
        `iterator` in interface `Iterable<T>`
      Specified by:
        `iterator` in interface `Collection<T>`
      Specified by:
        `iterator` in interface `List<T>`

    - #### toArray

          public Object[] toArray()

      Specified by:
        `toArray` in interface `Collection<T>`
      Specified by:
        `toArray` in interface `List<T>`

    - #### toArray

          public <T> T[] toArray(T[] ts)

      Specified by:
        `toArray` in interface `Collection<T>`
      Specified by:
        `toArray` in interface `List<T>`

    - #### add

          public boolean add(T t)

      Specified by:
        `add` in interface `Collection<T>`
      Specified by:
        `add` in interface `List<T>`

    - #### remove

          public boolean remove(Object o)

      Specified by:
        `remove` in interface `Collection<T>`
      Specified by:
        `remove` in interface `List<T>`

    - #### containsAll

          public boolean containsAll(Collection<?> objects)

      Specified by:
        `containsAll` in interface `Collection<T>`
      Specified by:
        `containsAll` in interface `List<T>`

    - #### addAll

          public boolean addAll(Collection<? extends T> ts)

      Specified by:
        `addAll` in interface `Collection<T>`
      Specified by:
        `addAll` in interface `List<T>`

    - #### addAll

          public boolean addAll(int i,
                                Collection<? extends T> ts)

      Specified by:
        `addAll` in interface `List<T>`

    - #### removeAll

          public boolean removeAll(Collection<?> objects)

      Specified by:
        `removeAll` in interface `Collection<T>`
      Specified by:
        `removeAll` in interface `List<T>`

    - #### retainAll

          public boolean retainAll(Collection<?> objects)

      Specified by:
        `retainAll` in interface `Collection<T>`
      Specified by:
        `retainAll` in interface `List<T>`

    - #### clear

          public void clear()

      Specified by:
        `clear` in interface `Collection<T>`
      Specified by:
        `clear` in interface `List<T>`

    - #### getAt

          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

    - #### get

          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(int)` as it a) possibly modifies the underlying list and b) does NOT throw an `IndexOutOfBoundsException` when `index < 0 || index >= size()`.

      Specified by:
        `get` in interface `List<T>`
      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

    - #### set

          public T set(int i,
                       T t)

      Specified by:
        `set` in interface `List<T>`

    - #### add

          public void add(int i,
                          T t)

      Specified by:
        `add` in interface `List<T>`

    - #### remove

          public T remove(int i)

      Specified by:
        `remove` in interface `List<T>`

    - #### indexOf

          public int indexOf(Object o)

      Specified by:
        `indexOf` in interface `List<T>`

    - #### lastIndexOf

          public int lastIndexOf(Object o)

      Specified by:
        `lastIndexOf` in interface `List<T>`

    - #### listIterator

          public ListIterator<T> listIterator()

      Specified by:
        `listIterator` in interface `List<T>`

    - #### listIterator

          public ListIterator<T> listIterator(int i)

      Specified by:
        `listIterator` in interface `List<T>`

    - #### equals

          public boolean equals(Object obj)

      Specified by:
        `equals` in interface `Collection<T>`
      Specified by:
        `equals` in interface `List<T>`
      Overrides:
        `equals` in class `Object`

    - #### hashCode

          public int hashCode()

      Specified by:
        `hashCode` in interface `Collection<T>`
      Specified by:
        `hashCode` in interface `List<T>`
      Overrides:
        `hashCode` in class `Object`

    - #### subList

          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.
      Specified by:
        `subList` in interface `List<T>`
      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