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

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.collect.ForwardingCollection<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>
Direct Known Subclasses:
ForwardingList, ForwardingMultiset, ForwardingQueue, ForwardingSet

public abstract class ForwardingCollection<E>
extends ForwardingObject
implements Collection<E>

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

Author:
Kevin Bourrillion
See Also:
ForwardingObject, Serialized Form

Constructor Summary
protected ForwardingCollection(Collection<E> delegate)
          Constructs a forwarding collection that forwards to the provided delegate.
 
Method Summary
 boolean add(E element)
           
 boolean addAll(Collection<? extends E> collection)
           
 void clear()
           
 boolean contains(Object object)
           
 boolean containsAll(Collection<?> collection)
           
protected static boolean containsAllImpl(Collection<?> c, Collection<?> d)
          Returns true if the specified collection c contains all of the elements in the specified collection d.
protected static boolean containsImpl(Collection<?> c, Object o)
          Returns true if the specified collection c contains the element o.
protected  Collection<E> delegate()
          Returns the backing delegate object.
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 boolean remove(Object object)
           
 boolean removeAll(Collection<?> collection)
           
protected static boolean removeAllImpl(Collection<?> c, Collection<?> d)
          Removes from the specified collection c all of its elements that are contained in the specified collection d (optional operation).
protected static boolean removeImpl(Collection<?> c, Object o)
          Removes from the specified collection c the specified element o (optional operation).
 boolean retainAll(Collection<?> c)
           
protected static boolean retainAllImpl(Collection<?> c, Collection<?> d)
          Retains only the elements in the specified collection c that are contained in the specified collection d (optional operation).
 int size()
           
 Object[] toArray()
           
<T> T[]
toArray(T[] array)
           
protected static Object[] toArrayImpl(Collection<?> c)
          Returns an array containing all of the elements in the specified collection.
protected static
<T> T[]
toArrayImpl(Collection<?> c, T[] array)
          Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array.
protected static String toStringImpl(Collection<?> c)
          Returns a string representation of the specified collection.
 
Methods inherited from class com.google.common.collect.ForwardingObject
toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

ForwardingCollection

protected ForwardingCollection(Collection<E> delegate)
Constructs a forwarding collection that forwards to the provided delegate.

Method Detail

delegate

protected Collection<E> delegate()
Description copied from class: ForwardingObject
Returns the backing delegate object. This method should be overridden to specify the correct return type. For example:
  @SuppressWarnings("unchecked")
  @Override protected Foo delegate() {
    return (Foo) super.delegate();
  }
This method should always return the same delegate instance that was passed to the constructor.

Overrides:
delegate in class ForwardingObject

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>

size

public int size()
Specified by:
size in interface Collection<E>

removeAll

public boolean removeAll(Collection<?> collection)
Specified by:
removeAll in interface Collection<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>

contains

public boolean contains(Object object)
Specified by:
contains in interface Collection<E>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>

toArray

public <T> T[] toArray(T[] array)
Specified by:
toArray in interface Collection<E>

add

public boolean add(E element)
Specified by:
add in interface Collection<E>

remove

public boolean remove(Object object)
Specified by:
remove in interface Collection<E>

containsAll

public boolean containsAll(Collection<?> collection)
Specified by:
containsAll in interface Collection<E>

addAll

public boolean addAll(Collection<? extends E> collection)
Specified by:
addAll in interface Collection<E>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>

toArrayImpl

protected static Object[] toArrayImpl(Collection<?> c)
Returns an array containing all of the elements in the specified collection. This method returns the elements in the order they are returned by the collection's iterator. The returned array is "safe" in that no references to it are maintained by the collection. The caller is thus free to modify the returned array.

Parameters:
c - the collection for which to return an array of elements
See Also:
AbstractCollection.toArray()

toArrayImpl

protected static <T> T[] toArrayImpl(Collection<?> c,
                                     T[] array)
Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.

If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.

This method returns the elements in the order they are returned by the collection's iterator.

Parameters:
c - the collection for which to return an array of elements
array - the array into which elements of the collection are to be stored, if it is big enough; otherwise a new array of the same runtime type is allocated for this purpose
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in the specified collection
See Also:
AbstractCollection.toArray(Object[])

toStringImpl

protected static String toStringImpl(Collection<?> c)
Returns a string representation of the specified collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Parameters:
c - the collection for which to return a string representation
See Also:
AbstractCollection.toString()

containsImpl

protected static boolean containsImpl(Collection<?> c,
                                      @Nullable
                                      Object o)
Returns true if the specified collection c contains the element o.

This method iterates over the specified collection c, checking each element returned by the iterator in turn to see if it equals the provided object o. If any element is equal, true is returned, otherwise false is returned.

Parameters:
c - a collection which might contain the element.
o - an element that might be contained by c

containsAllImpl

protected static boolean containsAllImpl(Collection<?> c,
                                         Collection<?> d)
Returns true if the specified collection c contains all of the elements in the specified collection d.

This method iterates over the specified collection d, checking each element returned by the iterator in turn to see if it is contained in the specified collection c. If all elements are so contained, true is returned, otherwise false.

Parameters:
c - a collection which might contain all elements in d
d - a collection whose elements might be contained by c
See Also:
AbstractCollection.containsAll(Collection)

removeImpl

protected static boolean removeImpl(Collection<?> c,
                                    @Nullable
                                    Object o)
Removes from the specified collection c the specified element o (optional operation).

This method iterates over the collection c, checking each element returned by the iterator in turn to see if it equal to the provided element o. If they are equal, it is removed from the collection c with the iterator's remove method

Parameters:
c - the collection from which to remove
o - an element to remove from the collectoin
Returns:
true if collection c changed as a result
Throws:
UnsupportedOperationException - if c's iterator does not support the remove method and c contains one or more elements in common with d

removeAllImpl

protected static boolean removeAllImpl(Collection<?> c,
                                       Collection<?> d)
Removes from the specified collection c all of its elements that are contained in the specified collection d (optional operation).

This method iterates over the collection c, checking each element returned by the iterator in turn to see if it is contained in the collection d. If it is so contained, it is removed from the collection c with the iterator's remove method.

Parameters:
c - the collection from which to remove
d - a collection of elements to remove from collection c
Returns:
true if collection c changed as a result
Throws:
UnsupportedOperationException - if c's iterator does not support the remove method and c contains one or more elements in common with d
See Also:
AbstractCollection.removeAll(Collection)

retainAllImpl

protected static boolean retainAllImpl(Collection<?> c,
                                       Collection<?> d)
Retains only the elements in the specified collection c that are contained in the specified collection d (optional operation). In other words, removes from the collection c all of its elements that are not contained in the collection d.

This implementation iterates over the collection c, checking each element returned by the iterator in turn to see if it is contained in the collection d. If it is not so contained, it is removed from the collection c with the iterator's remove method.

Parameters:
c - the collection from which to remove
d - a collection of elements to retain in collection c
Returns:
true if collection c changed as a result
Throws:
UnsupportedOperationException - if c's iterator does not support the remove method and c contains one or more elements not present in the d