DESY ACOP Beans Home

com.cosylab.util
Class ObjectList

java.lang.Object
  extended by com.cosylab.util.ObjectList
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable, java.util.Collection, java.util.List
Direct Known Subclasses:
ListenerList

public class ObjectList
extends java.lang.Object
implements java.util.List, java.io.Serializable

A simple resizable threadsafe array. Offers minimal capability and overhead for all operations except toArray(Class). The main cost of the array maintainance is in the addition and removal of the elements. The potential traversals are very fast, because they can occur outside the synchronized blocks of code through normal arrays. The arrays returned by the toArray methods are of the correct runtime type. If, for example, the type of the list was specified to be X, the following cast is permitted:

X[] array = (X[])list.toArray()

Only one type cast is thus needed to obtain the final array for traversals.

Version:
$id$
Author:
Gasper Tkacik, Gasper Pajor
See Also:
Serialized Form

Constructor Summary
ObjectList(java.lang.Class type)
          Creates a new ObjectList object.
 
Method Summary
 void add(int index, java.lang.Object o)
          Adds an object to this list.
 boolean add(java.lang.Object o)
          Adds an object to this list.
 void add(java.lang.Object o, int index)
          Deprecated. replaced by add(int, Object)
 boolean addAll(java.util.Collection c)
          Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
 boolean addAll(int index, java.util.Collection c)
          Inserts all of the elements in the specified Collection into this list, starting at the specified position.
 void clear()
          Empties this list.
 boolean contains(java.lang.Object o)
          Checks if the list contains a given element.
 boolean containsAll(java.util.Collection c)
          Returns true if this collection contains all of the elements in the specified collection.
 java.lang.Object get(int index)
          Returns the object at the specified index
 int indexOf(java.lang.Object o)
          Searches for the first occurence of the given argument, testing for equality using the equals method.
 boolean isEmpty()
          Tests if this list has no elements.
 java.util.Iterator iterator()
          Returns iterator which iterates over elements, which was contained in list at the moment iterator was called.
 int lastIndexOf(java.lang.Object elem)
          Returns the index of the last occurrence of the specified object in this list.
 java.util.ListIterator listIterator()
          Returns a list iterator of the elements in this list, iterator will iterate over the elements, which were in list at the moment iterator was created thus iteration will not fail if list is modified during iteration.
 java.util.ListIterator listIterator(int index)
          Returns iterator which iterates over elements, which was contained in list at the moment iterator was called.
 java.lang.Object remove(int index)
          Removes the element at the specified position in this list (optional operation).
 boolean remove(java.lang.Object o)
          Removes an element from the list.
 boolean removeAll(java.util.Collection c)
          Removes the elements that are contained in the collection.
 boolean retainAll(java.util.Collection c)
          Removes all elements but those contained in the collection.
 java.lang.Object set(int index, java.lang.Object element)
          Sets the element at specified index to specified object.
 int size()
          Returns the size of the list.
 java.util.List subList(int fromIndex, int toIndex)
          Deprecated. This method is not implemented.
 java.lang.Object[] toArray()
          Returns the elements of this list.
 java.lang.Object[] toArray(java.lang.Class type)
          Returns all objects in the list which are subtypes of the class parameter.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array containing all of the elements in this list.
 java.lang.String toString()
          Produces a string rendering of this instance by enumerating all members.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

ObjectList

public ObjectList(java.lang.Class type)
Creates a new ObjectList object.

Parameters:
type - the type of objects this list will hold
Throws:
java.lang.NullPointerException - if the type is null
Method Detail

add

public void add(java.lang.Object o,
                int index)
Deprecated. replaced by add(int, Object)

Adds an object to this list. This list can contain multiple entries of the same object.

Parameters:
o - object to add to the collection
index - where the object should be inserted

add

public boolean add(java.lang.Object o)
Adds an object to this list. This list can contain multiple entries of the same object.

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Parameters:
o - object to add to the collection
Returns:
true
See Also:
List.add(Object)

remove

public boolean remove(java.lang.Object o)
Removes an element from the list. If there is more than one reference to the same object in the array, this method will remove only one reference.

Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.List
Parameters:
o - element to be removed from this list, if present.
Returns:
true if this list contained the specified element.
See Also:
List.remove(java.lang.Object)

size

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

Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.List
Returns:
int list size

toArray

public java.lang.Object[] toArray(java.lang.Class type)
Returns all objects in the list which are subtypes of the class parameter. This necessitates the construction of a new array which must occur in a synchronized block of code. This method therefore takes some time to execute.

Parameters:
type - the type of the objects to look for
Returns:
Object[] an array containing only those elements of the list, which are a subtype of type
Throws:
java.lang.NullPointerException - when type is null
java.lang.IllegalArgumentException - if type is not a subtype of the type specified as the constructor parameter

toArray

public java.lang.Object[] toArray()
Returns the elements of this list. Do not modify this array in any way.. List membership must only be affected through add and remove() methods of this class.

Specified by:
toArray in interface java.util.Collection
Specified by:
toArray in interface java.util.List
Returns:
an array of stored objects, of the run time type specified by the constructor argument

contains

public boolean contains(java.lang.Object o)
Checks if the list contains a given element. Comparison with equals. Uses linear search.

Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.List
Parameters:
o - the object to look for
Returns:
true iff the list contains object o.

get

public java.lang.Object get(int index)
Returns the object at the specified index

Specified by:
get in interface java.util.List
Parameters:
index - the index of the requested object
Returns:
the object at the specified index.

indexOf

public int indexOf(java.lang.Object o)
Searches for the first occurence of the given argument, testing for equality using the equals method.

Specified by:
indexOf in interface java.util.List
Parameters:
o - an object.
Returns:
the index of the first occurrence of the argument in this list; returns -1 if the object is not found.
See Also:
Object.equals(Object)

toString

public java.lang.String toString()
Produces a string rendering of this instance by enumerating all members. Contract of Object.toString.

Overrides:
toString in class java.lang.Object
Returns:
String all members

clear

public void clear()
Empties this list. References to contained elementsw are released.

Specified by:
clear in interface java.util.Collection
Specified by:
clear in interface java.util.List

iterator

public java.util.Iterator iterator()
Returns iterator which iterates over elements, which was contained in list at the moment iterator was called. Iteration does not fail, if elements are added or removed during the iteration.

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.List
Returns:
non-failing iterator with copy of elements

isEmpty

public boolean isEmpty()
Tests if this list has no elements.

Specified by:
isEmpty in interface java.util.Collection
Specified by:
isEmpty in interface java.util.List
Returns:
true if this list has no elements; false otherwise.

remove

public java.lang.Object remove(int index)
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 java.util.List
Parameters:
index - the index of the element to removed.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException - DOCUMENT ME!

add

public void add(int index,
                java.lang.Object o)
Adds an object to this list. This list can contain multiple entries of the same object.

Specified by:
add in interface java.util.List
Parameters:
index - where the object should be inserted
o - object to add to the collection
Throws:
java.lang.NullPointerException
java.lang.ClassCastException - if the RTT of o is not assignable to the type specified in the constructor
See Also:
List.add(int, Object)

lastIndexOf

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

Specified by:
lastIndexOf in interface java.util.List
Parameters:
elem - the desired element.
Returns:
the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
See Also:
List.lastIndexOf(java.lang.Object)

addAll

public boolean addAll(int index,
                      java.util.Collection c)
Inserts all of the elements in the specified Collection into this list, starting at the specified position. 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 the list in the order that they are returned by the specified Collection's iterator.

Specified by:
addAll in interface java.util.List
Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
See Also:
List.addAll(int, java.util.Collection)

addAll

public boolean addAll(java.util.Collection c)
Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.

Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Parameters:
c - the elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
See Also:
List.addAll(java.util.Collection)

containsAll

public boolean containsAll(java.util.Collection c)
Returns true if this collection contains all of the elements in the specified collection.

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

Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.List
Parameters:
c - collection to be checked for containment in this collection.
Returns:
true if this collection contains all of the elements in the specified collection.
See Also:
contains(Object), List.containsAll(java.util.Collection)

removeAll

public boolean removeAll(java.util.Collection c)
Removes the elements that are contained in the collection.

Specified by:
removeAll in interface java.util.Collection
Specified by:
removeAll in interface java.util.List
Parameters:
c - the collection of elements to be removed
Returns:
true if at least one element is removed
See Also:
List.removeAll(java.util.Collection)

retainAll

public boolean retainAll(java.util.Collection c)
Removes all elements but those contained in the collection.

Specified by:
retainAll in interface java.util.Collection
Specified by:
retainAll in interface java.util.List
Parameters:
c - the collection of elements to keep
Returns:
true if at lest one element is removed
See Also:
List.retainAll(java.util.Collection)

subList

public java.util.List subList(int fromIndex,
                              int toIndex)
Deprecated. This method is not implemented.

DOCUMENT ME!

Specified by:
subList in interface java.util.List
Parameters:
fromIndex - DOCUMENT ME!
toIndex - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.lang.UnsupportedOperationException - when invoked
See Also:
List.subList(int, int)

listIterator

public java.util.ListIterator listIterator(int index)
Returns iterator which iterates over elements, which was contained in list at the moment iterator was called. Iteration does not fail, if elements are added or removed during the iteration.

Specified by:
listIterator in interface java.util.List
Parameters:
index - index of the first element to be returned from the list iterator (by a call to the next method).
Returns:
a list iterator of the elements in this list, starting at the specified position in the list.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index > size()).
See Also:
List.listIterator(int)

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Sets the element at specified index to specified object.

Specified by:
set in interface java.util.List
Parameters:
index - the index of object to set
element - the object to be set to the index
Returns:
the element previously at the specified position
Throws:
java.lang.NullPointerException - DOCUMENT ME!
java.lang.ClassCastException - DOCUMENT ME!
See Also:
List.set(int, Object)

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
Returns an array containing all of the elements in this list.

Specified by:
toArray in interface java.util.Collection
Specified by:
toArray in interface java.util.List
Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array is allocated for this purpose.
Returns:
an array containing the elements of this list.
Throws:
java.lang.NullPointerException - if the specified array is null.
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.
See Also:
List.toArray(java.lang.Object[])

listIterator

public java.util.ListIterator listIterator()
Returns a list iterator of the elements in this list, iterator will iterate over the elements, which were in list at the moment iterator was created thus iteration will not fail if list is modified during iteration.

Specified by:
listIterator in interface java.util.List
Returns:
a list iterator of the elements in this list
See Also:
List.listIterator(int)


Copyright © 2010. All Rights Reserved.