Package groovy.lang
Class ListWithDefault<T>
- java.lang.Object
-
- groovy.lang.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>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
- Author:
- Andre Steingress
-
-
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, java.util.Collection<? extends T> ts)booleanaddAll(java.util.Collection<? extends T> ts)voidclear()booleancontains(java.lang.Object o)booleancontainsAll(java.util.Collection<?> objects)booleanequals(java.lang.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).inthashCode()intindexOf(java.lang.Object o)booleanisEmpty()java.util.Iterator<T>iterator()intlastIndexOf(java.lang.Object o)java.util.ListIterator<T>listIterator()java.util.ListIterator<T>listIterator(int i)static <T> java.util.List<T>newInstance(java.util.List<T> items, boolean lazyDefaultValues, Closure initClosure)Tremove(int i)booleanremove(java.lang.Object o)booleanremoveAll(java.util.Collection<?> objects)booleanretainAll(java.util.Collection<?> objects)Tset(int i, T t)intsize()java.util.List<T>subList(int fromIndex, int toIndex)Returns a view of a portion of this list.java.lang.Object[]toArray()<T> T[]toArray(T[] ts)
-
-
-
Method Detail
-
newInstance
public static <T> java.util.List<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
public java.util.Iterator<T> iterator()
-
toArray
public java.lang.Object[] toArray()
-
toArray
public <T> T[] toArray(T[] ts)
-
add
public boolean add(T t)
-
remove
public boolean remove(java.lang.Object o)
-
containsAll
public boolean containsAll(java.util.Collection<?> objects)
-
addAll
public boolean addAll(java.util.Collection<? extends T> ts)
-
addAll
public boolean addAll(int i, java.util.Collection<? extends T> ts)- Specified by:
addAllin 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
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. IflazyDefaultValuesistrueany 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 ofList.get(int)as it a) possibly modifies the underlying list and b) does NOT throw anIndexOutOfBoundsExceptionwhenindex < 0 || index >= size().- Specified by:
getin interfacejava.util.List<T>- Parameters:
index- an index (might be greater or equal tosize(), or smaller than 0)- Returns:
- the value at the given
indexor the default value
-
indexOf
public int indexOf(java.lang.Object o)
- Specified by:
indexOfin interfacejava.util.List<T>
-
lastIndexOf
public int lastIndexOf(java.lang.Object o)
- Specified by:
lastIndexOfin interfacejava.util.List<T>
-
listIterator
public java.util.ListIterator<T> listIterator()
- Specified by:
listIteratorin interfacejava.util.List<T>
-
listIterator
public java.util.ListIterator<T> listIterator(int i)
- Specified by:
listIteratorin interfacejava.util.List<T>
-
equals
public boolean equals(java.lang.Object obj)
-
hashCode
public int hashCode()
-
subList
public java.util.List<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:
subListin 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
-
-