Class ListWithDefault<T>
- All Implemented Interfaces:
java.lang.Iterable<T>
,java.util.Collection<T>
,java.util.List<T>
public final class ListWithDefault<T>
extends java.lang.Object
implements java.util.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()
.- Since:
- 1.8.7
-
Method Summary
Modifier and Type Method Description void
add(int i, T t)
boolean
add(T t)
boolean
addAll(int i, java.util.Collection<? extends T> ts)
boolean
addAll(java.util.Collection<? extends T> ts)
void
clear()
boolean
contains(java.lang.Object o)
boolean
containsAll(java.util.Collection<?> objects)
boolean
equals(java.lang.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 toget(int)
.java.util.List<T>
getDelegate()
Closure
getInitClosure()
int
hashCode()
int
indexOf(java.lang.Object o)
boolean
isEmpty()
boolean
isLazyDefaultValues()
java.util.Iterator<T>
iterator()
int
lastIndexOf(java.lang.Object o)
java.util.ListIterator<T>
listIterator()
java.util.ListIterator<T>
listIterator(int i)
static <T> ListWithDefault<T>
newInstance(java.util.List<T> items, boolean lazyDefaultValues, Closure initClosure)
T
remove(int i)
boolean
remove(java.lang.Object o)
boolean
removeAll(java.util.Collection<?> objects)
boolean
retainAll(java.util.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.java.lang.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.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.lang.Iterable
forEach
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Method Details
-
getDelegate
-
isLazyDefaultValues
public boolean isLazyDefaultValues() -
getInitClosure
-
newInstance
public static <T> ListWithDefault<T> newInstance(java.util.List<T> items, boolean lazyDefaultValues, Closure initClosure) -
size
public int size() -
isEmpty
public boolean isEmpty() -
contains
public boolean contains(java.lang.Object o) -
iterator
-
toArray
public java.lang.Object[] toArray() -
toArray
public <T> T[] toArray(T[] ts) -
add
-
remove
public boolean remove(java.lang.Object o) -
containsAll
public boolean containsAll(java.util.Collection<?> objects) -
addAll
-
addAll
- Specified by:
addAll
in interfacejava.util.List<T>
-
removeAll
public boolean removeAll(java.util.Collection<?> objects) -
retainAll
public boolean retainAll(java.util.Collection<?> objects) -
clear
public void clear() -
getAt
Overwrites subscript operator handling by redirecting toget(int)
.- Parameters:
index
- an index (might be greater or equal tosize()
, or smaller than 0)- Returns:
- the value at the given
index
or the default value
-
get
Returns the element at the given index but grows the list if needed. If the requestedindex
is greater than or equal tosize()
, the list will grow to the new size and a default value calculated using theinitClosure
will be used to populate the missing value and returned.If
lazyDefaultValues
istrue
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 theinitClosure
to populate the list for the given list value. Hence, when in this mode, nulls cannot be stored in this list. IflazyDefaultValues
isfalse
any gaps when growing the list are filled eagerly by calling theinitClosure
for all gap indexes during list growth. No calls toinitClosure
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 anIndexOutOfBoundsException
whenindex < 0 || index >= size()
.- Specified by:
get
in interfacejava.util.List<T>
- Parameters:
index
- an index (might be greater or equal tosize()
, or smaller than 0)- Returns:
- the value at the given
index
or the default value
-
set
- Specified by:
set
in interfacejava.util.List<T>
-
add
- Specified by:
add
in interfacejava.util.List<T>
-
remove
- Specified by:
remove
in interfacejava.util.List<T>
-
indexOf
public int indexOf(java.lang.Object o)- Specified by:
indexOf
in interfacejava.util.List<T>
-
lastIndexOf
public int lastIndexOf(java.lang.Object o)- Specified by:
lastIndexOf
in interfacejava.util.List<T>
-
listIterator
- Specified by:
listIterator
in interfacejava.util.List<T>
-
listIterator
- Specified by:
listIterator
in interfacejava.util.List<T>
-
equals
public boolean equals(java.lang.Object obj) -
hashCode
public int hashCode() -
subList
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 interfacejava.util.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
-