sky.monome.util
Class SynchronizedList<T>

java.lang.Object
  extended by sky.monome.util.SynchronizedList<T>
Type Parameters:
T - Type of the contained data.
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>, java.util.RandomAccess, Synchronized

public final class SynchronizedList<T>
extends java.lang.Object
implements java.util.List<T>, java.util.RandomAccess, Synchronized

Unbounded list with strong synchronization.
This list works like an ArrayList since it internally contains an ArrayList.
This class is strongly thread-safe for all its methods. But you should iterate on it with external synchronization based on the internal lock object accessible with the getLockObject() method specified by the Synchronized interface.

Author:
PJ Skyman
See Also:
Synchronized

Constructor Summary
SynchronizedList()
          Constructs an empty synchronized list.
 
Method Summary
 void add(int index, T element)
          Adds the specified object to this list at the specified index.
 boolean add(T e)
          Adds the specified object to this list.
 boolean addAll(java.util.Collection<? extends T> c)
          Adds all objects of the specified collection to this list.
 boolean addAll(int index, java.util.Collection<? extends T> c)
          Adds all objects of the specified collection to this list at the specified index.
 void clear()
          Clears this list.
 boolean contains(java.lang.Object o)
          Indicates whether this list contains the specified object.
 boolean containsAll(java.util.Collection<?> c)
          Indicates whether this list contains all objects of the specified collection at the same time.
 boolean equals(java.lang.Object obj)
          Compares the specified object with this list for equality.
 T get(int index)
          Returns the object stored at the specified index in this list.
 java.lang.Object getLockObject()
          Returns the object over which the internal and external synchronization will operate.
 int hashCode()
          Returns the hashcode of this list.
 int indexOf(java.lang.Object o)
          Returns the index of the specified object in this list.
 boolean isEmpty()
          Indicates whether this list is empty.
 boolean isValidIndex(int index)
          Verifies that the specified index is valid for this list.
 java.util.Iterator<T> iterator()
          Returns an iterator to iterate over this list.
 int lastIndexOf(java.lang.Object o)
          Returns the index of the last occurrence of the specified object in this list.
 java.util.ListIterator<T> listIterator()
          Returns a list iterator to iterate over this list.
 java.util.ListIterator<T> listIterator(int index)
          Returns a list iterator to iterate over this list from the specified index.
 T remove(int index)
          Removes the object at the specified index from this list.
 boolean remove(java.lang.Object o)
          Removes the specified object from this list.
 boolean removeAll(java.util.Collection<?> c)
          Removes all objects from this list that are also contained in the specified collection.
 boolean retainAll(java.util.Collection<?> c)
          Retains only all objects also contained by the specified collection.
 T set(int index, T element)
          Stores the specified object at the specified index.
 int size()
          Returns the size of this list.
 java.util.List<T> subList(int fromIndex, int toIndex)
          Deprecated. Since the sublist will share the same internal data, there will be no consistent synchronization anymore by manipulating the sublist.
 java.lang.Object[] toArray()
          Returns an array of objects contained in this list.
<T> T[]
toArray(T[] a)
          Returns an array containing all of elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
 java.lang.String toString()
          Returns a string representation of this list.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SynchronizedList

public SynchronizedList()
Constructs an empty synchronized list.

Method Detail

add

public boolean add(T e)
Adds the specified object to this list. The object is appended at the tail of this list.

Specified by:
add in interface java.util.Collection<T>
Specified by:
add in interface java.util.List<T>
Parameters:
e - Object to add to this list.
Returns:
true.

add

public void add(int index,
                T element)
Adds the specified object to this list at the specified index.

Specified by:
add in interface java.util.List<T>
Parameters:
index - Index in this list at which the specified object will be inserted.
element - Object to add to this list at the specified index.

addAll

public boolean addAll(java.util.Collection<? extends T> c)
Adds all objects of the specified collection to this list. These objects are appended at the tail of this list.

Specified by:
addAll in interface java.util.Collection<T>
Specified by:
addAll in interface java.util.List<T>
Parameters:
c - Collection from which all objects will be added to this list.
Returns:
true if this list was modified as a result of the call, false otherwise.

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends T> c)
Adds all objects of the specified collection to this list at the specified index.

Specified by:
addAll in interface java.util.List<T>
Parameters:
index - Index in this list at which specified objects will be inserted.
c - Collection from which all objects will be added to this list.
Returns:
true if this list was modified as a result of the call, false otherwise.

clear

public void clear()
Clears this list.

Specified by:
clear in interface java.util.Collection<T>
Specified by:
clear in interface java.util.List<T>

contains

public boolean contains(java.lang.Object o)
Indicates whether this list contains the specified object.

Specified by:
contains in interface java.util.Collection<T>
Specified by:
contains in interface java.util.List<T>
Parameters:
o - Object whose presence in this list is to be tested.
Returns:
true if this list contains the specified object, false otherwise.

containsAll

public boolean containsAll(java.util.Collection<?> c)
Indicates whether this list contains all objects of the specified collection at the same time.

Specified by:
containsAll in interface java.util.Collection<T>
Specified by:
containsAll in interface java.util.List<T>
Parameters:
c - Collection whose presence of all objects in this list is to be tested.
Returns:
true if this list contains all objects of the specified collection at the same time, false otherwise.

equals

public boolean equals(java.lang.Object obj)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain same elements in the same order.

Specified by:
equals in interface java.util.Collection<T>
Specified by:
equals in interface java.util.List<T>
Overrides:
equals in class java.lang.Object
Parameters:
obj - The object to be compared for equality with this list.
Returns:
true if the specified object is equal to this list, false otherwise.

get

public T get(int index)
Returns the object stored at the specified index in this list.

Specified by:
get in interface java.util.List<T>
Parameters:
index - Index of the desired object.
Returns:
The object stored at the specified index in this list.

hashCode

public int hashCode()
Returns the hashcode of this list.

Specified by:
hashCode in interface java.util.Collection<T>
Specified by:
hashCode in interface java.util.List<T>
Overrides:
hashCode in class java.lang.Object
Returns:
The hashcode of this list.

indexOf

public int indexOf(java.lang.Object o)
Returns the index of the specified object in this list.

Specified by:
indexOf in interface java.util.List<T>
Parameters:
o - Object to search for.
Returns:
The index of the specified object in this list.

isEmpty

public boolean isEmpty()
Indicates whether this list is empty.

Specified by:
isEmpty in interface java.util.Collection<T>
Specified by:
isEmpty in interface java.util.List<T>
Returns:
true if this list is empty, false otherwise.

iterator

public java.util.Iterator<T> iterator()
Returns an iterator to iterate over this list.

Specified by:
iterator in interface java.lang.Iterable<T>
Specified by:
iterator in interface java.util.Collection<T>
Specified by:
iterator in interface java.util.List<T>
Returns:
An iterator to iterate over this list.

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Returns the index of the last occurrence of the specified object in this list.

Specified by:
lastIndexOf in interface java.util.List<T>
Parameters:
o - Object to search for.
Returns:
The index of the last occurrence of the specified object in this list.

listIterator

public java.util.ListIterator<T> listIterator()
Returns a list iterator to iterate over this list.

Specified by:
listIterator in interface java.util.List<T>
Returns:
A list iterator to iterate over this list.

listIterator

public java.util.ListIterator<T> listIterator(int index)
Returns a list iterator to iterate over this list from the specified index.

Specified by:
listIterator in interface java.util.List<T>
Parameters:
index - Index from which the iterator will iterate.
Returns:
A list iterator to iterate over this list from the specified index.

remove

public boolean remove(java.lang.Object o)
Removes the specified object from this list. Only the first occurrence of the specified object will be removed.

Specified by:
remove in interface java.util.Collection<T>
Specified by:
remove in interface java.util.List<T>
Parameters:
o - Object to remove from this list.
Returns:
true if this list is modified as a result of the call, false otherwise.

remove

public T remove(int index)
Removes the object at the specified index from this list.

Specified by:
remove in interface java.util.List<T>
Parameters:
index - Index of the object which needs to be removed from this list.
Returns:
The object that is removed from this list.

removeAll

public boolean removeAll(java.util.Collection<?> c)
Removes all objects from this list that are also contained in the specified collection.

Specified by:
removeAll in interface java.util.Collection<T>
Specified by:
removeAll in interface java.util.List<T>
Parameters:
c - Collection from which objects also contained in this list will be removed.
Returns:
true if this list is modified as a result of the call.

retainAll

public boolean retainAll(java.util.Collection<?> c)
Retains only all objects also contained by the specified collection.

Specified by:
retainAll in interface java.util.Collection<T>
Specified by:
retainAll in interface java.util.List<T>
Parameters:
c - Collection from which only objects also contained will be retained.
Returns:
true if this list is modified as a result of the call.

set

public T set(int index,
             T element)
Stores the specified object at the specified index.

Specified by:
set in interface java.util.List<T>
Parameters:
index - Index at which the specified object will be stored.
element - Object to store at the specified index in this list.
Returns:
The object that was stored in this list before the call.

size

public int size()
Returns the size of this list.

Specified by:
size in interface java.util.Collection<T>
Specified by:
size in interface java.util.List<T>
Returns:
The size of this list.

subList

@Deprecated
public java.util.List<T> subList(int fromIndex,
                                            int toIndex)
Deprecated. Since the sublist will share the same internal data, there will be no consistent synchronization anymore by manipulating the sublist.

Returns a sublist of this list built from the specified begin index to the specified end index.

Specified by:
subList in interface java.util.List<T>
Parameters:
fromIndex - Index from which the sublist is built (inclusive).
toIndex - Index to which the sublist is built (exclusive).
Returns:
A sublist of this list.

toArray

public java.lang.Object[] toArray()
Returns an array of objects contained in this list.

Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.List<T>
Returns:
An array of objects contained in this list.

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list 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 this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), 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 list only if the caller knows that the list does not contain any null elements.)

Specified by:
toArray in interface java.util.Collection<T>
Specified by:
toArray in interface java.util.List<T>
Type Parameters:
T - Type of contained elements
Parameters:
a - The array into which elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
An array containing elements of this list.
Throws:
java.lang.ArrayStoreException - If the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
java.lang.NullPointerException - If the specified array is null.

toString

public java.lang.String toString()
Returns a string representation of this list. The returned string contains all elements of this list separated by a comma character.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this list.

isValidIndex

public boolean isValidIndex(int index)
Verifies that the specified index is valid for this list.

Parameters:
index - Index to verify.
Returns:
true if the specified index is in the range [0;size()[, false otherwise.

getLockObject

public java.lang.Object getLockObject()
Returns the object over which the internal and external synchronization will operate. Only implicit and explicit usages of iterator(s) need external synchronization, because all others methods are already internally synchronized.

Specified by:
getLockObject in interface Synchronized
Returns:
The object over which the internal and external synchronization will operate.