Class 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, 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(int, E) alone will not change the behavior of addAll(int, java.util.Collection<? extends E>), 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.

    Since:
    2.0
    Author:
    Mike Bostock, Louis Wasserman
    • Constructor Detail

      • ForwardingList

        protected ForwardingList()
        Constructor for use by subclasses.
    • Method Detail

      • delegate

        protected abstract List<Edelegate()
        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

        @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.)
        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 Objects.equals(o, 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 Objects.equals(o, 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<ElistIterator()
        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<ElistIterator​(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

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

        @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).
        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<EsubList​(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​(@NullableDecl
                              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 return true.
        • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
        • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
        • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
        • For any non-null reference value x, x.equals(null) should return false.

        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(), HashMap
      • 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 by HashMap.

        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 in equals 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 the hashCode 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 the hashCode 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 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), System.identityHashCode(java.lang.Object)