Top Description Fields Constructors Methods
groovy.lang

public final Class ListWithDefault<T>

extends Object
implements List<T>
Class Inheritance
All Implemented Interfaces
java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable
Imports
java.util.ArrayList, .Collection, .Iterator, .List, .ListIterator

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

Field Summary

Modifier and TypeField and Description
private final List<T>
private final Closure<V>
private final boolean

Constructor Summary

AccessConstructor and Description
private
ListWithDefault(List<T> items, boolean lazyDefaultValues, Closure<V> initClosure)

Method Summary

Modifier and TypeMethod and Description
public boolean
add(T
element to be appended to this list
t
)

Implements java.util.List.add.

Appends the specified element to the end of this list (optional operation).
public void
add(int
index at which the specified element is to be inserted
i
,
T
element to be inserted
t
)

Implements java.util.List.add.

Inserts the specified element at the specified position in this list (optional operation).
public boolean
addAll(Collection<? extends T>
collection containing elements to be added to this list
ts
)

Implements java.util.List.addAll.

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
public boolean
addAll(int
index at which to insert the first element from the specified collection
i
,
Collection<? extends T>
collection containing elements to be added to this list
ts
)

Implements java.util.List.addAll.

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
public void
clear()

Implements java.util.List.clear.

Removes all of the elements from this list (optional operation).
public boolean
contains(Object
element whose presence in this list is to be tested
o
)

Implements java.util.List.contains.

Returns true if this list contains the specified element.
public boolean
containsAll(Collection<?>
collection to be checked for containment in this list
objects
)

Implements java.util.List.containsAll.

Returns true if this list contains all of the elements of the specified collection.
public boolean
equals(Object
the object to be compared for equality with this list
obj
)

Overrides java.lang.Object.equals.

Implements java.util.List.equals.

Compares the specified object with this list for equality.
public T

Returns:

the value at the given index or the default value
get
(int
an index (might be greater or equal to size(), or smaller than 0)
index
)

Implements java.util.List.get.

Returns the element at the given index but grows the list if needed.
public T

Returns:

the value at the given index or the default value
getAt
(int
an index (might be greater or equal to size(), or smaller than 0)
index
)

Overwrites subscript operator handling by redirecting to get(int).

private T
getDefaultValue(int idx)

public List<T>
public Closure<V>
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode.

Returns the hash code value for this list.
public int
indexOf(Object
element to search for
o
)

Implements java.util.List.indexOf.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
public boolean
isEmpty()

Implements java.util.List.isEmpty.

Returns true if this list contains no elements.
public boolean
public Iterator<T>
iterator()

Implements java.util.List.iterator.

Returns an iterator over the elements in this list in proper sequence.
public int
lastIndexOf(Object
element to search for
o
)

Implements java.util.List.lastIndexOf.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
public ListIterator<T>
listIterator()

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).
public ListIterator<T>
listIterator(int
index of the first element to be returned from the list iterator (by a call to next)
i
)

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
public static <T> ListWithDefault<T>
newInstance(List<T> items, boolean lazyDefaultValues, Closure<V> initClosure)

private static int
normaliseIndex(int index, int size)

public boolean
remove(Object
element to be removed from this list, if present
o
)

Implements java.util.List.remove.

Removes the first occurrence of the specified element from this list, if it is present (optional operation).
public T
remove(int
the index of the element to be removed
i
)

Implements java.util.List.remove.

Removes the element at the specified position in this list (optional operation).
public boolean
removeAll(Collection<?>
collection containing elements to be removed from this list
objects
)

Implements java.util.List.removeAll.

Removes from this list all of its elements that are contained in the specified collection (optional operation).
public boolean
retainAll(Collection<?>
collection containing elements to be retained in this list
objects
)

Implements java.util.List.retainAll.

Retains only the elements in this list that are contained in the specified collection (optional operation).
public T
set(int
index of the element to replace
i
,
T
element to be stored at the specified position
t
)

Implements java.util.List.set.

Replaces the element at the specified position in this list with the specified element (optional operation).
public int
size()

Implements java.util.List.size.

Returns the number of elements in this list.
public ListWithDefault<T>

Returns:

a view of a specified range within this list, keeping all lazy list settings
subList
(int
low endpoint of the subList (inclusive)
fromIndex
,
int
upper endpoint of the subList (exclusive)
toIndex
)

Implements java.util.List.subList.

Returns a view of a portion of this list.
public Object[]
toArray()

Implements java.util.List.toArray.

Returns an array containing all of the elements in this list in proper sequence (from first to last element).
public <T> T[]
toArray(T[]
the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
ts
)

Implements java.util.List.toArray.

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateback to summary
private final List<T> delegate
initClosureback to summary
private final Closure<V> initClosure
lazyDefaultValuesback to summary
private final boolean lazyDefaultValues

Constructor Detail

ListWithDefaultback to summary
private ListWithDefault(List<T> items, boolean lazyDefaultValues, Closure<V> initClosure)

Method Detail

addback to summary
public boolean add(T t)

Implements java.util.List.add.

Doc from java.util.List.add.

Appends the specified element to the end of this list (optional operation).

Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.

Parameters
t:T

element to be appended to this list

Returns:boolean

true (as specified by Collection#add)

addback to summary
public void add(int i, T t)

Implements java.util.List.add.

Doc from java.util.List.add.

Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters
i:int

index at which the specified element is to be inserted

t:T

element to be inserted

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

Implements java.util.List.addAll.

Doc from java.util.List.addAll.

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters
ts:Collection<? extends T>

collection containing elements to be added to this list

Returns:boolean

true if this list changed as a result of the call

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

Implements java.util.List.addAll.

Doc from java.util.List.addAll.

Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters
i:int

index at which to insert the first element from the specified collection

ts:Collection<? extends T>

collection containing elements to be added to this list

Returns:boolean

true if this list changed as a result of the call

clearback to summary
public void clear()

Implements java.util.List.clear.

Doc from java.util.List.clear.

Removes all of the elements from this list (optional operation). The list will be empty after this call returns.

containsback to summary
public boolean contains(Object o)

Implements java.util.List.contains.

Doc from java.util.List.contains.

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).

Parameters
o:Object

element whose presence in this list is to be tested

Returns:boolean

true if this list contains the specified element

containsAllback to summary
public boolean containsAll(Collection<?> objects)

Implements java.util.List.containsAll.

Doc from java.util.List.containsAll.

Returns true if this list contains all of the elements of the specified collection.

Parameters
objects:Collection<?>

collection to be checked for containment in this list

Returns:boolean

true if this list contains all of the elements of the specified collection

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Implements java.util.List.equals.

Doc from java.util.List.equals.

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if Objects.equals(e1, e2).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Parameters
obj:Object

the object to be compared for equality with this list

Returns:boolean

true if the specified object is equal to this list

Annotations
@Override
getback to summary
public T get(int index)

Implements java.util.List.get.

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

Parameters
index:int

an index (might be greater or equal to size(), or smaller than 0)

Returns:T

the value at the given index or the default value

getAtback to summary
public T getAt(int index)

Overwrites subscript operator handling by redirecting to get(int).

Parameters
index:int

an index (might be greater or equal to size(), or smaller than 0)

Returns:T

the value at the given index or the default value

getDefaultValueback to summary
private T getDefaultValue(int idx)
Annotations
@SuppressWarnings:unchecked
getDelegateback to summary
public List<T> getDelegate()
getInitClosureback to summary
public Closure<V> getInitClosure()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode.

Doc from java.util.List.hashCode.

Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:

int hashCode = 1;
    for (E e : list)
        hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object#hashCode.
Returns:int

the hash code value for this list

Annotations
@Override
indexOfback to summary
public int indexOf(Object o)

Implements java.util.List.indexOf.

Doc from java.util.List.indexOf.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

Parameters
o:Object

element to search for

Returns:int

the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element

isEmptyback to summary
public boolean isEmpty()

Implements java.util.List.isEmpty.

Doc from java.util.List.isEmpty.

Returns true if this list contains no elements.

Returns:boolean

true if this list contains no elements

isLazyDefaultValuesback to summary
public boolean isLazyDefaultValues()
iteratorback to summary
public Iterator<T> iterator()

Implements java.util.List.iterator.

Doc from java.util.List.iterator.

Returns an iterator over the elements in this list in proper sequence.

Returns:Iterator<T>

an iterator over the elements in this list in proper sequence

lastIndexOfback to summary
public int lastIndexOf(Object o)

Implements java.util.List.lastIndexOf.

Doc from java.util.List.lastIndexOf.

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

Parameters
o:Object

element to search for

Returns:int

the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element

listIteratorback to summary
public ListIterator<T> listIterator()

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence).

Returns:ListIterator<T>

a list iterator over the elements in this list (in proper sequence)

listIteratorback to summary
public ListIterator<T> listIterator(int i)

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

Parameters
i:int

index of the first element to be returned from the list iterator (by a call to next)

Returns:ListIterator<T>

a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list

newInstanceback to summary
public static <T> ListWithDefault<T> newInstance(List<T> items, boolean lazyDefaultValues, Closure<V> initClosure)
normaliseIndexback to summary
private static int normaliseIndex(int index, int size)
removeback to summary
public boolean remove(Object o)

Implements java.util.List.remove.

Doc from java.util.List.remove.

Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that Objects.equals(o, get(i)) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Parameters
o:Object

element to be removed from this list, if present

Returns:boolean

true if this list contained the specified element

removeback to summary
public T remove(int i)

Implements java.util.List.remove.

Doc from java.util.List.remove.

Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters
i:int

the index of the element to be removed

Returns:T

the element previously at the specified position

removeAllback to summary
public boolean removeAll(Collection<?> objects)

Implements java.util.List.removeAll.

Doc from java.util.List.removeAll.

Removes from this list all of its elements that are contained in the specified collection (optional operation).

Parameters
objects:Collection<?>

collection containing elements to be removed from this list

Returns:boolean

true if this list changed as a result of the call

retainAllback to summary
public boolean retainAll(Collection<?> objects)

Implements java.util.List.retainAll.

Doc from java.util.List.retainAll.

Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.

Parameters
objects:Collection<?>

collection containing elements to be retained in this list

Returns:boolean

true if this list changed as a result of the call

setback to summary
public T set(int i, T t)

Implements java.util.List.set.

Doc from java.util.List.set.

Replaces the element at the specified position in this list with the specified element (optional operation).

Parameters
i:int

index of the element to replace

t:T

element to be stored at the specified position

Returns:T

the element previously at the specified position

sizeback to summary
public int size()

Implements java.util.List.size.

Doc from java.util.List.size.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this list

subListback to summary
public ListWithDefault<T> subList(int fromIndex, int toIndex)

Implements java.util.List.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.

Parameters
fromIndex:int

low endpoint of the subList (inclusive)

toIndex:int

upper endpoint of the subList (exclusive)

Returns:ListWithDefault<T>

a view of a specified range within this list, keeping all lazy list settings

toArrayback to summary
public Object[] toArray()

Implements java.util.List.toArray.

Doc from java.util.List.toArray.

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Returns:Object[]

an array containing all of the elements in this list in proper sequence

toArrayback to summary
public <T> T[] toArray(T[] ts)

Implements java.util.List.toArray.

Doc from java.util.List.toArray.

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:

String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
Parameters
<T>

Doc from java.util.Collection.toArray. the component type of the array to contain the collection

ts:T[]

the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Returns:T[]

an array containing the elements of this list