Top Description Constructors Methods
com.google.common.collect

public abstract Class ForwardingList<E extends @Nullable Object>

extends ForwardingCollection<E extends @Nullable Object>
implements List<E extends @Nullable Object>
Class Inheritance
All Implemented Interfaces
java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable
Known Direct Subclasses
com.google.common.collect.FilteredKeyMultimap.AddRejectingList
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Imports
com.google.common.annotations.GwtCompatible, com.google.errorprone.annotations.CanIgnoreReturnValue, java.util.Collection, .Iterator, .List, .ListIterator, javax.annotation.CheckForNull, org.checkerframework.checker.nullness.qual.Nullable

A list which forwards all its method calls to another list. Subclasses should override one or more methods to modify the behavior of the backing list as desired per the decorator pattern.

This class does not implement java.util.RandomAccess. If the delegate supports random access, the ForwardingList subclass should implement the RandomAccess interface.

Warning

The methods of ForwardingList forward indiscriminately to the methods of the delegate. For example, overriding add alone will not change the behavior of addAll, which can lead to unexpected behavior. In this case, you should override addAll as well, either providing your own implementation, or delegating to the provided standardAddAll method.

default method warning: This class does not forward calls to default methods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on the ForwardingList.

The standard methods and any collection views they return are not guaranteed to be thread-safe, even when all of the methods that they depend on are thread-safe.

Authors
Mike Bostock, Louis Wasserman
Since
2.0

Constructor Summary

AccessConstructor and Description
protected
ForwardingList()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public void
add(int
index at which the specified element is to be inserted
index
,
E
element to be inserted
element
)

Implements java.util.List.add.

Inserts the specified element at the specified position in this list (optional operation).
public boolean
addAll(int
index at which to insert the first element from the specified collection
index
,
Collection<? extends E>
collection containing elements to be added to this list
elements
)

Implements java.util.List.addAll.

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
protected abstract List<E>
delegate()

Implements abstract com.google.common.collect.ForwardingCollection.delegate.

Returns the backing delegate instance that methods are forwarded to.
public boolean
equals(Object
the object to be compared for equality with this list
object
)

Overrides java.lang.Object.equals.

Implements java.util.List.equals, java.util.Collection.equals.

Compares the specified object with this list for equality.
public E
get(int
index of the element to return
index
)

Implements java.util.List.get.

Returns the element at the specified position in this list.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode, java.util.Collection.hashCode.

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

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 int
lastIndexOf(Object
element to search for
element
)

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<E>
listIterator()

Implements java.util.List.listIterator.

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

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 E
remove(int
the index of the element to be removed
index
)

Implements java.util.List.remove.

Removes the element at the specified position in this list (optional operation).
public E
set(int
index of the element to replace
index
,
E
element to be stored at the specified position
element
)

Implements java.util.List.set.

Replaces the element at the specified position in this list with the specified element (optional operation).
protected boolean
standardAdd(E element)

A sensible default implementation of add(Object), in terms of add(int, Object).

protected boolean
standardAddAll(int index, Iterable<? extends E> elements)

A sensible default implementation of addAll(int, Collection), in terms of the add method of listIterator(int).

protected boolean
standardEquals(Object object)

A sensible definition of equals(Object) in terms of size and iterator.

protected int
standardHashCode()

A sensible definition of hashCode in terms of iterator.

protected int
standardIndexOf(Object element)

A sensible default implementation of indexOf, in terms of listIterator().

protected Iterator<E>
standardIterator()

A sensible default implementation of iterator, in terms of listIterator().

protected int
standardLastIndexOf(Object element)

A sensible default implementation of lastIndexOf, in terms of listIterator(int).

protected ListIterator<E>
standardListIterator()

A sensible default implementation of listIterator(), in terms of listIterator(int).

protected ListIterator<E>
standardListIterator(int start)

A sensible default implementation of listIterator(int), in terms of size, get(int), set(int, Object), add(int, Object), and remove(int).

protected List<E>
standardSubList(int fromIndex, int toIndex)

A sensible default implementation of subList(int, int).

public List<E>
subList(int
low endpoint (inclusive) of the subList
fromIndex
,
int
high endpoint (exclusive) of the subList
toIndex
)

Implements java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Inherited from com.google.common.collect.ForwardingCollection:
addaddAllclearcontainscontainsAllisEmptyiteratorremoveremoveAllretainAllsizestandardAddAllstandardClearstandardContainsstandardContainsAllstandardIsEmptystandardRemovestandardRemoveAllstandardRetainAllstandardToArraystandardToArraystandardToStringtoArraytoArray

Constructor Detail

ForwardingListback to summary
protected ForwardingList()

Constructor for use by subclasses.

Method Detail

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

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
index:int

index at which the specified element is to be inserted

element:E

element to be inserted

Annotations
@Override
addAllback to summary
public boolean addAll(int index, Collection<? extends E> elements)

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
index:int

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

elements:Collection<? extends E>

collection containing elements to be added to this list

Returns:boolean

true if this list changed as a result of the call

Annotations
@CanIgnoreReturnValue
@Override
delegateback to summary
protected abstract List<E> delegate()

Implements abstract com.google.common.collect.ForwardingCollection.delegate.

Doc from com.google.common.collect.ForwardingObject.delegate.

Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet#delegate. Concrete subclasses override this method to supply the instance being decorated.

Annotations
@Override
equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Implements java.util.List.equals, java.util.Collection.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
object: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 E get(int index)

Implements java.util.List.get.

Doc from java.util.List.get.

Returns the element at the specified position in this list.

Parameters
index:int

index of the element to return

Returns:E

the element at the specified position in this list

Annotations
@Override
@ParametricNullness
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.List.hashCode, java.util.Collection.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 element)

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
element: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

Annotations
@Override
lastIndexOfback to summary
public int lastIndexOf(Object element)

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
element: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

Annotations
@Override
listIteratorback to summary
public ListIterator<E> 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<E>

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

Annotations
@Override
listIteratorback to summary
public ListIterator<E> listIterator(int index)

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
index:int

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

Returns:ListIterator<E>

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

Annotations
@Override
removeback to summary
public E remove(int index)

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
index:int

the index of the element to be removed

Returns:E

the element previously at the specified position

Annotations
@CanIgnoreReturnValue
@Override
@ParametricNullness
setback to summary
public E set(int index, E element)

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
index:int

index of the element to replace

element:E

element to be stored at the specified position

Returns:E

the element previously at the specified position

Annotations
@CanIgnoreReturnValue
@Override
@ParametricNullness
standardAddback to summary
protected boolean standardAdd(E element)

A sensible default implementation of add(Object), in terms of add(int, Object). If you override add(int, Object), you may wish to override add(Object) to forward to this implementation.

Since
7.0
standardAddAllback to summary
protected boolean standardAddAll(int index, Iterable<? extends E> elements)

A sensible default implementation of addAll(int, Collection), in terms of the add method of listIterator(int). If you override listIterator(int), you may wish to override addAll(int, Collection) to forward to this implementation.

Since
7.0
standardEqualsback to summary
protected boolean standardEquals(Object object)

A sensible definition of equals(Object) in terms of size and iterator. If you override either of those methods, you may wish to override equals(Object) to forward to this implementation.

Since
7.0
standardHashCodeback to summary
protected int standardHashCode()

A sensible definition of hashCode in terms of iterator. If you override iterator, you may wish to override hashCode to forward to this implementation.

Since
7.0
standardIndexOfback to summary
protected int standardIndexOf(Object element)

A sensible default implementation of indexOf, in terms of listIterator(). If you override listIterator(), you may wish to override indexOf to forward to this implementation.

Since
7.0
standardIteratorback to summary
protected Iterator<E> standardIterator()

A sensible default implementation of iterator, in terms of listIterator(). If you override listIterator(), you may wish to override iterator to forward to this implementation.

Since
7.0
standardLastIndexOfback to summary
protected int standardLastIndexOf(Object element)

A sensible default implementation of lastIndexOf, in terms of listIterator(int). If you override listIterator(int), you may wish to override lastIndexOf to forward to this implementation.

Since
7.0
standardListIteratorback to summary
protected ListIterator<E> standardListIterator()

A sensible default implementation of listIterator(), in terms of listIterator(int). If you override listIterator(int), you may wish to override listIterator() to forward to this implementation.

Since
7.0
standardListIteratorback to summary
protected ListIterator<E> standardListIterator(int start)

A sensible default implementation of listIterator(int), in terms of size, get(int), set(int, Object), add(int, Object), and remove(int). If you override any of these methods, you may wish to override listIterator(int) to forward to this implementation.

Since
7.0
standardSubListback to summary
protected List<E> standardSubList(int fromIndex, int toIndex)

A sensible default implementation of subList(int, int). If you override any other methods, you may wish to override subList(int, int) to forward to this implementation.

Since
7.0
subListback to summary
public List<E> subList(int fromIndex, int toIndex)

Implements java.util.List.subList.

Doc from java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Parameters
fromIndex:int

low endpoint (inclusive) of the subList

toIndex:int

high endpoint (exclusive) of the subList

Returns:List<E>

a view of the specified range within this list

Annotations
@Override