Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.util
Class Collections.SynchronizedList  view Collections.SynchronizedList download Collections.SynchronizedList.java

java.lang.Object
  extended byjava.util.Collections.SynchronizedCollection
      extended byjava.util.Collections.SynchronizedList
All Implemented Interfaces:
Collection, java.lang.Iterable, List, java.io.Serializable
Direct Known Subclasses:
Collections.SynchronizedRandomAccessList
Enclosing class:
Collections

static class Collections.SynchronizedList
extends Collections.SynchronizedCollection
implements List

The implementation of Collections.synchronizedList(List) 55 for sequential lists. This class name is required for compatibility with Sun's JDK serializability. Package visible, so that lists such as Vector.subList() can specify which object to synchronize on.


Field Summary
(package private)  Collection c
          The wrapped collection.
(package private)  List list
          The wrapped list; stored both here and in the superclass to avoid excessive casting.
(package private)  java.lang.Object mutex
          The object to synchronize on.
private static long serialVersionUID
          Compatible with JDK 1.4.
 
Constructor Summary
(package private) Collections.SynchronizedList(List l)
          Wrap a given list.
(package private) Collections.SynchronizedList(java.lang.Object sync, List l)
          Called only by trusted code to specify the mutex as well as the list.
 
Method Summary
 void add(int index, java.lang.Object o)
          Insert an element into the underlying list at a given position (optional operation).
 boolean add(java.lang.Object o)
          Adds the object to the underlying collection, first obtaining a lock on the mutex.
 boolean addAll(Collection col)
          Adds the objects in col to the underlying collection, first obtaining a lock on the mutex.
 boolean addAll(int index, Collection c)
          Add the contents of a collection to the underlying list at the given index (optional operation).
 void clear()
          Removes all objects from the underlying collection, first obtaining a lock on the mutex.
 boolean contains(java.lang.Object o)
          Checks for the existence of o within the underlying collection, first obtaining a lock on the mutex.
 boolean containsAll(Collection c1)
          Checks for the existence of each object in cl within the underlying collection, first obtaining a lock on the mutex.
 boolean equals(java.lang.Object o)
          Tests whether the underlying list is equal to the supplied object.
 java.lang.Object get(int index)
          Retrieves the object at the specified index.
 int hashCode()
          Obtains a hashcode for the underlying list, first obtaining a lock on the mutex.
 int indexOf(java.lang.Object o)
          Obtain the first index at which a given object is to be found in the underlying list.
 boolean isEmpty()
          Returns true if there are no objects in the underlying collection.
 Iterator iterator()
          Returns a synchronized iterator wrapper around the underlying collection's iterator.
 int lastIndexOf(java.lang.Object o)
          Obtain the last index at which a given object is to be found in this underlying list.
 ListIterator listIterator()
          Retrieves a synchronized wrapper around the underlying list's list iterator.
 ListIterator listIterator(int index)
          Retrieves a synchronized wrapper around the underlying list's list iterator.
 java.lang.Object remove(int index)
          Remove the element at a given position in the underlying list (optional operation).
 boolean remove(java.lang.Object o)
          Removes the specified object from the underlying collection, first obtaining a lock on the mutex.
 boolean removeAll(Collection col)
          Removes all elements, e, of the underlying collection for which col.contains(e) returns true.
 boolean retainAll(Collection col)
          Retains all elements, e, of the underlying collection for which col.contains(e) returns true.
 java.lang.Object set(int index, java.lang.Object o)
          Replace an element of the underlying list with another object (optional operation).
 int size()
          Retrieves the size of the underlying collection.
 List subList(int fromIndex, int toIndex)
          Obtain a List view of a subsection of the underlying list, from fromIndex (inclusive) to toIndex (exclusive).
 java.lang.Object[] toArray()
          Returns an array containing each object within the underlying collection.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Copies the elements in the underlying collection to the supplied array.
 java.lang.String toString()
          Returns a string representation of the underlying collection.
 
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
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Compatible with JDK 1.4.

See Also:
Constant Field Values

list

final List list
The wrapped list; stored both here and in the superclass to avoid excessive casting. Package visible for use by subclass.


c

final Collection c
The wrapped collection. Package visible for use by subclasses.


mutex

final java.lang.Object mutex
The object to synchronize on. When an instance is created via public methods, it will be this; but other uses like SynchronizedMap.values() must specify another mutex. Package visible for use by subclasses.

Constructor Detail

Collections.SynchronizedList

Collections.SynchronizedList(List l)
Wrap a given list.


Collections.SynchronizedList

Collections.SynchronizedList(java.lang.Object sync,
                             List l)
Called only by trusted code to specify the mutex as well as the list.

Method Detail

add

public void add(int index,
                java.lang.Object o)
Insert an element into the underlying list at a given position (optional operation). This shifts all existing elements from that position to the end one index to the right. This version of add has no return, since it is assumed to always succeed if there is no exception. Before the addition takes place, a lock is obtained on the mutex.

Specified by:
add in interface List

addAll

public boolean addAll(int index,
                      Collection c)
Add the contents of a collection to the underlying list at the given index (optional operation). If the list imposes restraints on what can be inserted, such as no null elements, this should be documented. A lock is obtained on the mutex before any of the elements are added.

Specified by:
addAll in interface List

equals

public boolean equals(java.lang.Object o)
Tests whether the underlying list is equal to the supplied object. The object is deemed to be equal if it is also a List of equal size and with the same elements (i.e. each element, e1, in list, l1, and each element, e2, in l2, must return true for e1 == null ? e2 == null : e1.equals(e2). Before the comparison is made, a lock is obtained on the mutex.

Specified by:
equals in interface List

get

public java.lang.Object get(int index)
Retrieves the object at the specified index. A lock is obtained on the mutex before the list is accessed.

Specified by:
get in interface List

hashCode

public int hashCode()
Obtains a hashcode for the underlying list, first obtaining a lock on the mutex. The calculation of the hashcode is detailed in the documentation for the List interface.

Specified by:
hashCode in interface List

indexOf

public int indexOf(java.lang.Object o)
Obtain the first index at which a given object is to be found in the underlying list. A lock is obtained on the mutex before the list is accessed.

Specified by:
indexOf in interface List

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Obtain the last index at which a given object is to be found in this underlying list. A lock is obtained on the mutex before the list is accessed.

Specified by:
lastIndexOf in interface List

listIterator

public ListIterator listIterator()
Retrieves a synchronized wrapper around the underlying list's list iterator. A lock is obtained on the mutex before the list iterator is retrieved.

Specified by:
listIterator in interface List

listIterator

public ListIterator listIterator(int index)
Retrieves a synchronized wrapper around the underlying list's list iterator. A lock is obtained on the mutex before the list iterator is retrieved. The iterator starts at the index supplied, leading to the element at that index being the first one returned by next(). Calling previous() from this initial position returns index - 1.

Specified by:
listIterator in interface List

remove

public java.lang.Object remove(int index)
Remove the element at a given position in the underlying list (optional operation). All remaining elements are shifted to the left to fill the gap. A lock on the mutex is obtained before the element is removed.

Specified by:
remove in interface List

set

public java.lang.Object set(int index,
                            java.lang.Object o)
Replace an element of the underlying list with another object (optional operation). A lock is obtained on the mutex before the element is replaced.

Specified by:
set in interface List

subList

public List subList(int fromIndex,
                    int toIndex)
Obtain a List view of a subsection of the underlying list, from fromIndex (inclusive) to toIndex (exclusive). If the two indices are equal, the sublist is empty. The returned list should be modifiable if and only if this list is modifiable. Changes to the returned list should be reflected in this list. If this list is structurally modified in any way other than through the returned list, the result of any subsequent operations on the returned list is undefined. A lock is obtained on the mutex before the creation of the sublist. The returned list is also synchronized, using the same mutex.

Specified by:
subList in interface List

add

public boolean add(java.lang.Object o)
Adds the object to the underlying collection, first obtaining a lock on the mutex.

Specified by:
add in interface Collection

addAll

public boolean addAll(Collection col)
Adds the objects in col to the underlying collection, first obtaining a lock on the mutex.

Specified by:
addAll in interface Collection

clear

public void clear()
Removes all objects from the underlying collection, first obtaining a lock on the mutex.

Specified by:
clear in interface Collection

contains

public boolean contains(java.lang.Object o)
Checks for the existence of o within the underlying collection, first obtaining a lock on the mutex.

Specified by:
contains in interface Collection

containsAll

public boolean containsAll(Collection c1)
Checks for the existence of each object in cl within the underlying collection, first obtaining a lock on the mutex.

Specified by:
containsAll in interface Collection

isEmpty

public boolean isEmpty()
Returns true if there are no objects in the underlying collection. A lock on the mutex is obtained before the check is performed.

Specified by:
isEmpty in interface Collection

iterator

public Iterator iterator()
Returns a synchronized iterator wrapper around the underlying collection's iterator. A lock on the mutex is obtained before retrieving the collection's iterator.

Specified by:
iterator in interface Collection

remove

public boolean remove(java.lang.Object o)
Removes the specified object from the underlying collection, first obtaining a lock on the mutex.

Specified by:
remove in interface Collection

removeAll

public boolean removeAll(Collection col)
Removes all elements, e, of the underlying collection for which col.contains(e) returns true. A lock on the mutex is obtained before the operation proceeds.

Specified by:
removeAll in interface Collection

retainAll

public boolean retainAll(Collection col)
Retains all elements, e, of the underlying collection for which col.contains(e) returns true. That is, every element that doesn't exist in col is removed. A lock on the mutex is obtained before the operation proceeds.

Specified by:
retainAll in interface Collection

size

public int size()
Retrieves the size of the underlying collection. A lock on the mutex is obtained before the collection is accessed.

Specified by:
size in interface Collection

toArray

public java.lang.Object[] toArray()
Returns an array containing each object within the underlying collection. A lock is obtained on the mutex before the collection is accessed.

Specified by:
toArray in interface Collection

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
Copies the elements in the underlying collection to the supplied array. If a.length < size(), a new array of the same run-time type is created, with a size equal to that of the collection. If a.length > size(), then the elements from 0 to size() - 1 contain the elements from this collection. The following element is set to null to indicate the end of the collection objects. However, this only makes a difference if null is not a permitted value within the collection. Before the copying takes place, a lock is obtained on the mutex.

Specified by:
toArray in interface Collection

toString

public java.lang.String toString()
Returns a string representation of the underlying collection. A lock is obtained on the mutex before the string is created.