Class ForwardingList<E>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingCollection<E>
-
- com.google.common.collect.ForwardingList<E>
-
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
@GwtCompatible public abstract class ForwardingList<E> extends ForwardingCollection<E> implements List<E>
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
RandomAccess
. If the delegate supports random access, theForwardingList
subclass should implement theRandomAccess
interface.Warning: The methods of
ForwardingList
forward indiscriminately to the methods of the delegate. For example, overridingadd(int, E)
alone will not change the behavior ofaddAll(int, java.util.Collection<? extends E>)
, which can lead to unexpected behavior. In this case, you should overrideaddAll
as well, either providing your own implementation, or delegating to the providedstandardAddAll
method.default
method warning: This class does not forward calls todefault
methods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on theForwardingList
.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.- Since:
- 2.0
- Author:
- Mike Bostock, Louis Wasserman
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ForwardingList()
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).boolean
addAll(int index, Collection<? extends E> elements)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).protected abstract List<E>
delegate()
Returns the backing delegate instance that methods are forwarded to.boolean
equals(@Nullable Object object)
Indicates whether some other object is "equal to" this one.E
get(int index)
Returns the element at the specified position in this list.int
hashCode()
Returns a hash code value for the object.int
indexOf(Object element)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.int
lastIndexOf(Object element)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.ListIterator<E>
listIterator()
Returns a list iterator over the elements in this list (in proper sequence).ListIterator<E>
listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.E
remove(int index)
Removes the element at the specified position in this list (optional operation).E
set(int index, E element)
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 ofForwardingCollection.add(Object)
, in terms ofadd(int, Object)
.protected boolean
standardAddAll(int index, Iterable<? extends E> elements)
A sensible default implementation ofaddAll(int, Collection)
, in terms of theadd
method oflistIterator(int)
.protected boolean
standardEquals(@Nullable Object object)
A sensible definition ofequals(Object)
in terms ofForwardingCollection.size()
andForwardingCollection.iterator()
.protected int
standardHashCode()
A sensible definition ofhashCode()
in terms ofForwardingCollection.iterator()
.protected int
standardIndexOf(@Nullable Object element)
A sensible default implementation ofindexOf(java.lang.Object)
, in terms oflistIterator()
.protected Iterator<E>
standardIterator()
A sensible default implementation ofForwardingCollection.iterator()
, in terms oflistIterator()
.protected int
standardLastIndexOf(@Nullable Object element)
A sensible default implementation oflastIndexOf(java.lang.Object)
, in terms oflistIterator(int)
.protected ListIterator<E>
standardListIterator()
A sensible default implementation oflistIterator()
, in terms oflistIterator(int)
.protected ListIterator<E>
standardListIterator(int start)
A sensible default implementation oflistIterator(int)
, in terms ofForwardingCollection.size()
,get(int)
,set(int, Object)
,add(int, Object)
, andremove(int)
.protected List<E>
standardSubList(int fromIndex, int toIndex)
A sensible default implementation ofsubList(int, int)
.List<E>
subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specifiedfromIndex
, inclusive, andtoIndex
, exclusive.-
Methods inherited from class com.google.common.collect.ForwardingCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, standardAddAll, standardClear, standardContains, standardContainsAll, standardIsEmpty, standardRemove, standardRemoveAll, standardRetainAll, standardToArray, standardToArray, standardToString, toArray, toArray
-
Methods inherited from class com.google.common.collect.ForwardingObject
toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
-
-
-
Constructor Detail
-
ForwardingList
protected ForwardingList()
Constructor for use by subclasses.
-
-
Method Detail
-
delegate
protected abstract List<E> delegate()
Description copied from class:ForwardingObject
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 asForwardingSet.delegate()
. Concrete subclasses override this method to supply the instance being decorated.- Specified by:
delegate
in classForwardingCollection<E>
-
add
public void add(int index, E element)
Description copied from interface:java.util.List
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).
-
addAll
@CanIgnoreReturnValue public boolean addAll(int index, Collection<? extends E> elements)
Description copied from interface:java.util.List
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.)
-
get
public E get(int index)
Description copied from interface:java.util.List
Returns the element at the specified position in this list.
-
indexOf
public int indexOf(Object element)
Description copied from interface:java.util.List
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 indexi
such thatObjects.equals(o, get(i))
, or -1 if there is no such index.
-
lastIndexOf
public int lastIndexOf(Object element)
Description copied from interface:java.util.List
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 indexi
such thatObjects.equals(o, get(i))
, or -1 if there is no such index.- Specified by:
lastIndexOf
in interfaceList<E>
- Parameters:
element
- element to search for- Returns:
- the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
-
listIterator
public ListIterator<E> listIterator()
Description copied from interface:java.util.List
Returns a list iterator over the elements in this list (in proper sequence).- Specified by:
listIterator
in interfaceList<E>
- Returns:
- a list iterator over the elements in this list (in proper sequence)
-
listIterator
public ListIterator<E> listIterator(int index)
Description copied from interface:java.util.List
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 tonext
. An initial call toprevious
would return the element with the specified index minus one.- Specified by:
listIterator
in interfaceList<E>
- Parameters:
index
- index of the first element to be returned from the list iterator (by a call tonext
)- Returns:
- a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
-
remove
@CanIgnoreReturnValue public E remove(int index)
Description copied from interface:java.util.List
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.
-
set
@CanIgnoreReturnValue public E set(int index, E element)
Description copied from interface:java.util.List
Replaces the element at the specified position in this list with the specified element (optional operation).
-
subList
public List<E> subList(int fromIndex, int toIndex)
Description copied from interface:java.util.List
Returns a view of the portion of this list between the specifiedfromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
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:
Similar idioms may be constructed forlist.subList(from, to).clear();
indexOf
andlastIndexOf
, and all of the algorithms in theCollections
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.)
-
equals
public boolean equals(@Nullable Object object)
Description copied from class:java.lang.Object
Indicates whether some other object is "equal to" this one.The
equals
method implements an equivalence relation on non-null object references:- It is reflexive: for any non-null reference value
x
,x.equals(x)
should returntrue
. - It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
The
equals
method for classObject
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceList<E>
- Overrides:
equals
in classObject
- Parameters:
object
- the reference object with which to compare.- Returns:
true
if this object is the same as the obj argument;false
otherwise.- See Also:
Object.hashCode()
,HashMap
- It is reflexive: for any non-null reference value
-
hashCode
public int hashCode()
Description copied from class:java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided byHashMap
.The general contract of
hashCode
is:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
Object.equals(java.lang.Object)
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class
Object
does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)- Specified by:
hashCode
in interfaceCollection<E>
- Specified by:
hashCode
in interfaceList<E>
- Overrides:
hashCode
in classObject
- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
-
standardAdd
protected boolean standardAdd(E element)
A sensible default implementation ofForwardingCollection.add(Object)
, in terms ofadd(int, Object)
. If you overrideadd(int, Object)
, you may wish to overrideForwardingCollection.add(Object)
to forward to this implementation.- Since:
- 7.0
-
standardAddAll
protected boolean standardAddAll(int index, Iterable<? extends E> elements)
A sensible default implementation ofaddAll(int, Collection)
, in terms of theadd
method oflistIterator(int)
. If you overridelistIterator(int)
, you may wish to overrideaddAll(int, Collection)
to forward to this implementation.- Since:
- 7.0
-
standardIndexOf
protected int standardIndexOf(@Nullable Object element)
A sensible default implementation ofindexOf(java.lang.Object)
, in terms oflistIterator()
. If you overridelistIterator()
, you may wish to overrideindexOf(java.lang.Object)
to forward to this implementation.- Since:
- 7.0
-
standardLastIndexOf
protected int standardLastIndexOf(@Nullable Object element)
A sensible default implementation oflastIndexOf(java.lang.Object)
, in terms oflistIterator(int)
. If you overridelistIterator(int)
, you may wish to overridelastIndexOf(java.lang.Object)
to forward to this implementation.- Since:
- 7.0
-
standardIterator
protected Iterator<E> standardIterator()
A sensible default implementation ofForwardingCollection.iterator()
, in terms oflistIterator()
. If you overridelistIterator()
, you may wish to overrideForwardingCollection.iterator()
to forward to this implementation.- Since:
- 7.0
-
standardListIterator
protected ListIterator<E> standardListIterator()
A sensible default implementation oflistIterator()
, in terms oflistIterator(int)
. If you overridelistIterator(int)
, you may wish to overridelistIterator()
to forward to this implementation.- Since:
- 7.0
-
standardListIterator
@Beta protected ListIterator<E> standardListIterator(int start)
A sensible default implementation oflistIterator(int)
, in terms ofForwardingCollection.size()
,get(int)
,set(int, Object)
,add(int, Object)
, andremove(int)
. If you override any of these methods, you may wish to overridelistIterator(int)
to forward to this implementation.- Since:
- 7.0
-
standardSubList
@Beta protected List<E> standardSubList(int fromIndex, int toIndex)
A sensible default implementation ofsubList(int, int)
. If you override any other methods, you may wish to overridesubList(int, int)
to forward to this implementation.- Since:
- 7.0
-
standardEquals
@Beta protected boolean standardEquals(@Nullable Object object)
A sensible definition ofequals(Object)
in terms ofForwardingCollection.size()
andForwardingCollection.iterator()
. If you override either of those methods, you may wish to overrideequals(Object)
to forward to this implementation.- Since:
- 7.0
-
standardHashCode
@Beta protected int standardHashCode()
A sensible definition ofhashCode()
in terms ofForwardingCollection.iterator()
. If you overrideForwardingCollection.iterator()
, you may wish to overridehashCode()
to forward to this implementation.- Since:
- 7.0
-
-