Top Description Fields Constructors Methods
org.apache.commons.collections4.list

public Class GrowthList<E>

extends AbstractSerializableListDecorator<E>
Class Inheritance
Imports
java.util.ArrayList, .Collection, .Collections, .List

Decorates another List to make it seamlessly grow when indices larger than the list size are used on add and set, avoiding most IndexOutOfBoundsExceptions.

This class avoids errors by growing when a set or add method would normally throw an IndexOutOfBoundsException. Note that IndexOutOfBoundsException IS returned for invalid negative indices.

Trying to set or add to an index larger than the size will cause the list to grow (using null elements). Clearly, care must be taken not to use excessively large indices, as the internal list will grow to match.

Trying to use any method other than add or set with an invalid index will call the underlying list and probably result in an IndexOutOfBoundsException.

Take care when using this list with null values, as null is the value added when growing the list.

All sub-lists will access the underlying list directly, and will throw IndexOutOfBoundsExceptions.

This class differs from LazyList because here growth occurs on set and add, where LazyList grows on get. However, they can be used together by decorating twice.

Since
3.2
See Also
LazyList

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public
GrowthList()

Constructor that uses an ArrayList internally.

public
GrowthList(final int
the initial size of the ArrayList
initialSize
)

Constructor that uses an ArrayList internally.

protected
GrowthList(final List<E>
the list to decorate, must not be null
list
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
public void
add(final int
the index to add at
index
,
final E
the object to add at the specified index
element
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.add.

Implements java.util.List.add.

Decorate the add method to perform the growth behaviour.
public boolean

Returns:

true if the list changed
addAll
(final int
the index to add at
index
,
final Collection<? extends E>
the collection to add at the specified index
coll
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.addAll.

Implements java.util.List.addAll.

Decorate the addAll method to perform the growth behaviour.
public static <
the type of the elements in the list
E
>
GrowthList<E>

Returns:

a new growth list
growthList
(final List<E>
the list to decorate, must not be null
list
)

Factory method to create a growth list.

public E

Returns:

the object previously at that index
set
(final int
the index to set
index
,
final E
the object to set at the specified index
element
)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.set.

Implements java.util.List.set.

Decorate the set method to perform the growth behaviour.

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.collections4.list.AbstractSerializableListDecorator.serialVersionUID.

Serialization version

Constructor Detail

GrowthListback to summary
public GrowthList()

Constructor that uses an ArrayList internally.

GrowthListback to summary
public GrowthList(final int initialSize)

Constructor that uses an ArrayList internally.

Parameters
initialSize:int

the initial size of the ArrayList

Exceptions
IllegalArgumentException:
if initial size is invalid
GrowthListback to summary
protected GrowthList(final List<E> list)

Constructor that wraps (not copies).

Parameters
list:List<E>

the list to decorate, must not be null

Exceptions
NullPointerException:
if list is null

Method Detail

addback to summary
public void add(final int index, final E element)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.add.

Implements java.util.List.add.

Decorate the add method to perform the growth behaviour.

If the requested index is greater than the current size, the list will grow to the new size. Indices between the old size and the requested size will be filled with null.

If the index is less than the current size, the value will be added to the underlying list directly. If the index is less than zero, the underlying list is called, which will probably throw an IndexOutOfBoundsException.

Parameters
index:int

the index to add at

element:E

the object to add at the specified index

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the underlying list doesn't implement set
ClassCastException:
if the underlying list rejects the element
IllegalArgumentException:
if the underlying list rejects the element
addAllback to summary
public boolean addAll(final int index, final Collection<? extends E> coll)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.addAll.

Implements java.util.List.addAll.

Decorate the addAll method to perform the growth behaviour.

If the requested index is greater than the current size, the list will grow to the new size. Indices between the old size and the requested size will be filled with null.

If the index is less than the current size, the values will be added to the underlying list directly. If the index is less than zero, the underlying list is called, which will probably throw an IndexOutOfBoundsException.

Parameters
index:int

the index to add at

coll:Collection<? extends E>

the collection to add at the specified index

Returns:boolean

true if the list changed

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the underlying list doesn't implement set
ClassCastException:
if the underlying list rejects the element
IllegalArgumentException:
if the underlying list rejects the element
growthListback to summary
public static <E> GrowthList<E> growthList(final List<E> list)

Factory method to create a growth list.

Parameters
<E>
the type of the elements in the list
list:List<E>

the list to decorate, must not be null

Returns:GrowthList<E>

a new growth list

Exceptions
NullPointerException:
if list is null
Since
4.0
setback to summary
public E set(final int index, final E element)

Overrides org.apache.commons.collections4.list.AbstractListDecorator.set.

Implements java.util.List.set.

Decorate the set method to perform the growth behaviour.

If the requested index is greater than the current size, the list will grow to the new size. Indices between the old size and the requested size will be filled with null.

If the index is less than the current size, the value will be set onto the underlying list directly. If the index is less than zero, the underlying list is called, which will probably throw an IndexOutOfBoundsException.

Parameters
index:int

the index to set

element:E

the object to set at the specified index

Returns:E

the object previously at that index

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the underlying list doesn't implement set
ClassCastException:
if the underlying list rejects the element
IllegalArgumentException:
if the underlying list rejects the element