public final class ListWithDefault<T> extends Object implements List<T>
List
which automatically grows the list when either get(int)
or
getAt(int)
is called with an index greater than or equal to size()
.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> List<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() |
List<T> |
subList(int fromIndex,
int toIndex)
Returns a view of a portion of this list.
|
Object[] |
toArray() |
<T> T[] |
toArray(T[] ts) |
public boolean isLazyDefaultValues()
public Closure getInitClosure()
public static <T> List<T> newInstance(List<T> items, boolean lazyDefaultValues, Closure initClosure)
public int size()
public boolean isEmpty()
public boolean contains(Object o)
public Object[] toArray()
public <T> T[] toArray(T[] ts)
public boolean add(T t)
public boolean remove(Object o)
public boolean containsAll(Collection<?> objects)
containsAll
in interface Collection<T>
containsAll
in interface List<T>
public boolean addAll(Collection<? extends T> ts)
public boolean addAll(int i, Collection<? extends T> ts)
public boolean removeAll(Collection<?> objects)
public boolean retainAll(Collection<?> objects)
public void clear()
public T getAt(int index)
get(int)
.index
- an index (might be greater or equal to size()
, or smaller than 0)index
or the default valuepublic T get(int index)
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()
.
public int lastIndexOf(Object o)
lastIndexOf
in interface List<T>
public ListIterator<T> listIterator()
listIterator
in interface List<T>
public ListIterator<T> listIterator(int i)
listIterator
in interface List<T>
public boolean equals(Object obj)
public int hashCode()