com.google.common.collect
Class ForwardingList<E>

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.collect.ForwardingCollection<E>
          extended by com.google.common.collect.ForwardingList<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

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, the ForwadingList subclass should implement the RandomAccess interface.

Author:
Mike Bostock

Constructor Summary
ForwardingList()
           
 
Method Summary
 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(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).
 List<E> subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
 
Methods inherited from class com.google.common.collect.ForwardingCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, 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.List
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Constructor Detail

ForwardingList

public ForwardingList()
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 as ForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.

Specified by:
delegate in class ForwardingCollection<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).

Specified by:
add in interface List<E>
Parameters:
index - index at which the specified element is to be inserted
element - element to be inserted

addAll

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.)

Specified by:
addAll in interface List<E>
Parameters:
index - index at which to insert the first element from the specified collection
elements - collection containing elements to be added to this list
Returns:
true if this list changed as a result of the call

get

public E get(int index)
Description copied from interface: java.util.List
Returns the element at the specified position in this list.

Specified by:
get in interface List<E>
Parameters:
index - index of the element to return
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 index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Specified by:
indexOf in interface List<E>
Parameters:
element - element to search for
Returns:
the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element

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 index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Specified by:
lastIndexOf in interface List<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 interface List<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 to next. An initial call to previous would return the element with the specified index minus one.

Specified by:
listIterator in interface List<E>
Parameters:
index - index of the first element to be returned from the list iterator (by a call to next)
Returns:
a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list

remove

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.

Specified by:
remove in interface List<E>
Parameters:
index - the index of the element to be removed
Returns:
the element previously at the specified position

set

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).

Specified by:
set in interface List<E>
Parameters:
index - index of the element to replace
element - element to be stored at the specified position
Returns:
the element previously at the specified position

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 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.)

Specified by:
subList in interface List<E>
Parameters:
fromIndex - low endpoint (inclusive) of the subList
toIndex - high endpoint (exclusive) of the subList
Returns:
a view of the specified range within this list

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:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class Object
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(), Hashtable

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 hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface List<E>
Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable