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 forListwhich automatically grows the list when eitherget(int)orgetAt(int)is called with an index greater than or equal tosize().- Since:
- 1.8.7
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int i, T t)booleanadd(T t)booleanaddAll(int i, Collection<? extends T> ts)booleanaddAll(Collection<? extends T> ts)voidclear()booleancontains(Object o)booleancontainsAll(Collection<?> objects)booleanequals(Object obj)Tget(int index)Returns the element at the given index but grows the list if needed.TgetAt(int index)Overwrites subscript operator handling by redirecting toget(int).List<T>getDelegate()ClosuregetInitClosure()inthashCode()intindexOf(Object o)booleanisEmpty()booleanisLazyDefaultValues()Iterator<T>iterator()intlastIndexOf(Object o)ListIterator<T>listIterator()ListIterator<T>listIterator(int i)static <T> ListWithDefault<T>newInstance(List<T> items, boolean lazyDefaultValues, Closure initClosure)Tremove(int i)booleanremove(Object o)booleanremoveAll(Collection<?> objects)booleanretainAll(Collection<?> objects)Tset(int i, T t)intsize()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.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
-
-
-
Method Detail
-
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()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(Object o)
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] ts)
-
add
public boolean add(T t)
-
remove
public boolean remove(Object o)
-
containsAll
public boolean containsAll(Collection<?> objects)
- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceList<T>
-
addAll
public boolean addAll(Collection<? extends T> ts)
-
addAll
public boolean addAll(int i, Collection<? extends T> ts)
-
removeAll
public boolean removeAll(Collection<?> objects)
-
retainAll
public boolean retainAll(Collection<?> objects)
-
clear
public void clear()
-
getAt
public T getAt(int index)
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
indexor the default value
-
get
public T get(int index)
Returns the element at the given index but grows the list if needed. If the requestedindexis greater than or equal tosize(), the list will grow to the new size and a default value calculated using theinitClosurewill be used to populate the missing value and returned.If
lazyDefaultValuesistrueany 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 theinitClosureto populate the list for the given list value. Hence, when in this mode, nulls cannot be stored in this list. IflazyDefaultValuesisfalseany gaps when growing the list are filled eagerly by calling theinitClosurefor all gap indexes during list growth. No calls toinitClosureare 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 anIndexOutOfBoundsExceptionwhenindex < 0 || index >= size().
-
lastIndexOf
public int lastIndexOf(Object o)
- Specified by:
lastIndexOfin interfaceList<T>
-
listIterator
public ListIterator<T> listIterator()
- Specified by:
listIteratorin interfaceList<T>
-
listIterator
public ListIterator<T> listIterator(int i)
- Specified by:
listIteratorin interfaceList<T>
-
equals
public boolean equals(Object obj)
-
hashCode
public int hashCode()
-
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.
-
-