org.garret.consus
Class POList

java.lang.Object
  extended by org.garret.consus.Persistent
      extended by org.garret.consus.POCollection
          extended by org.garret.consus.POList
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.lang.Iterable, java.util.List
Direct Known Subclasses:
POSortedList

public class POList
extends POCollection
implements java.util.List, java.lang.Cloneable

Resizable-array implementation of the List interface. Implements all optional list operations and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that of the LinkedList implementation.

Each POList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to a POList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

An application can increase the capacity of an POList instance, before adding a large number of elements, by using the ensureCapacity operation. This may reduce the amount of incremental reallocation.


Field Summary
 
Fields inherited from class org.garret.consus.Persistent
db, oid, PO_DELETED, PO_LOADED, PO_NEW, PO_STUB, state, type
 
Constructor Summary
POList()
          Constructs an empty list.
POList(java.util.Collection c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
POList(int initialCapacity)
          Constructs an empty list with the specified initial capacity.
 
Method Summary
 void add(int index, java.lang.Object element)
          Inserts the specified element at the specified position in this list.
 boolean add(java.lang.Object o)
          Appends the specified element to the end of this list.
 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()
          Removes all of the elements from this list.
 java.lang.Object clone()
          Returns a shallow copy of this POList instance.
 boolean contains(java.lang.Object elem)
          Returns true if this list contains the specified element.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this POList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
 java.lang.Object get(int index)
          Returns the element from the specified position in this list.
 int indexOf(java.lang.Object elem)
          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 an iterator of the elements in this list in the proper sequence.
 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 an iterator of the elements in this list (in proper sequence).
 java.util.ListIterator listIterator(int index)
          Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
 boolean loadClosure()
          POList doesn't imlicitly loads its components.
 java.lang.Object remove(int index)
          Removes the element at the specified position in this list.
 void removeRange(int fromIndex, int toIndex)
          Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
 java.lang.Object set(int index, java.lang.Object element)
          Replaces the element at the specified position in this list with the specified element.
 int size()
          Returns the number of elements in this list.
 java.util.List subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this list in the correct order.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array containing all of the elements in this list in the correct order.
 void trimToSize()
          Trims the capacity of this POList instance to be the list's current size.
 
Methods inherited from class org.garret.consus.POCollection
containsAll, equals, hashCode, remove, removeAll, retainAll, toString
 
Methods inherited from class org.garret.consus.Persistent
assignOID, become, beginUpdate, endUpdate, get, get, getOid, getOid, getRef, getRef, isDeleted, isLoaded, isNew, load, loadComponents, lock, remove, save, store, store, unget
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, remove, removeAll, retainAll
 

Constructor Detail

POList

public POList()
Constructs an empty list.


POList

public POList(java.util.Collection c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The POList instance has an initial capacity of 110% of the size of the specified collection.


POList

public POList(int initialCapacity)
Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity - the initial capacity of the list.
Method Detail

add

public void add(int index,
                java.lang.Object element)
Inserts the specified element at the specified position in this list. 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 java.util.List
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).

add

public boolean add(java.lang.Object o)
Appends the specified element to the end of this list.

Specified by:
add in interface java.util.List
Overrides:
add in class POCollection
Parameters:
o - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

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. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)

Specified by:
addAll in interface java.util.List
Overrides:
addAll in class POCollection
Parameters:
c - elements to be inserted into this list.
Returns:
true if this collection changed as a result of the call.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index > size()).
See Also:
POCollection.add(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.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index > size()).

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.

Specified by:
clear in interface java.util.List
Overrides:
clear in class POCollection

clone

public java.lang.Object clone()
Returns a shallow copy of this POList instance. (The elements themselves are not copied.)

Overrides:
clone in class java.lang.Object
Returns:
a clone of this POList instance.

contains

public boolean contains(java.lang.Object elem)
Returns true if this list contains the specified element.

Specified by:
contains in interface java.util.List
Overrides:
contains in class POCollection
Parameters:
elem - element whose presence in this List is to be tested.
Returns:
true if this collection contains the specified element.

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this POList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity.

get

public java.lang.Object get(int index)
Returns the element from the specified position in this list.

Specified by:
get in interface java.util.List
Parameters:
index - index of element to return.
Returns:
the element at the specified position in this list.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).

indexOf

public int indexOf(java.lang.Object elem)
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:
elem - 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)

isEmpty

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

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

iterator

public java.util.Iterator iterator()
Returns an iterator of the elements in this list in the proper sequence.

This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's size(), get(int), and remove(int) methods.

Note that the iterator returned by this method will throw an UnsupportedOperationException in response to its remove method, unless the list's remove(int) method is overridden.

This implementation can be made to throw runtime exceptions in the face of concurrent modification, as described in the specification for the (protected) modCount field.

Specified by:
iterator in interface java.util.List
Specified by:
iterator in class POCollection
Returns:
an iterator over the elements in this list in proper sequence.

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.

listIterator

public java.util.ListIterator listIterator()
Returns an iterator of the elements in this list (in proper sequence). This implementation returns listIterator(0).

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

listIterator

public java.util.ListIterator listIterator(int index)
Returns a list iterator of 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 the nextElement method. An initial call to the previousElement method would return the element with the specified index minus one.

This implementation returns a straightforward implementation of the ListIterator interface, that extends the implementation of the Iterator interface returned by the iterator() method. The ListIterator implementation relies on the backing list's get(int), set(int, Object), add(int, Object) and remove(int) methods.

Note that the list iterator returned by this implementation will throw an UnsupportedOperationException in response to its remove, set and add methods, unless the list's remove(int), set(int, Object), and add(int, Object) methods are overridden.

This implementation can be made to throw runtime exceptions in the face of concurrent modification, as described in the specification for the (protected) modCount field.

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 (in proper sequence), starting at the specified position in the list.
Throws:
java.lang.IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index > size()).

loadClosure

public boolean loadClosure()
POList doesn't imlicitly loads its components.

Overrides:
loadClosure in class Persistent
Returns:
true to load all graph with referenced objects or false for loading stubs instead of referenced object.

remove

public java.lang.Object remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Specified by:
remove in interface java.util.List
Parameters:
index - the index of the element to removed.
Returns:
the element that was removed from the list.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

removeRange

public void removeRange(int fromIndex,
                        int toIndex)
Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

Parameters:
fromIndex - index of first element to be removed.
toIndex - index after last element to be removed.

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Replaces the element at the specified position in this list with the specified element.

Specified by:
set in interface java.util.List
Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

size

public final int size()
Returns the number of elements in this list.

Specified by:
size in interface java.util.List
Specified by:
size in class POCollection
Returns:
the number of elements in this list.

subList

public java.util.List subList(int fromIndex,
                              int toIndex)
Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive. The returned list is backed by this list, so 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 operating on 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(); 
n 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 the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

This implementation returns a list that subclasses AbstractList. The subclass stores, in private fields, the offset of the subList within the backing list, the size of the subList (which can change over its lifetime), and the expected modCount value of the backing list.

The subclass's set(int, Object), get(int), add(int, Object), remove(int), addAll(int, Collection) and removeRange(int, int) methods all delegate to the corresponding methods on the backing abstract list, after bounds-checking the index and adjusting for the offset. The addAll(Collection c) method merely returns addAll(size, c).

The listIterator(int) method returns a "wrapper object" over a list iterator on the backing list, which is created with the corresponding method on the backing list. The iterator method merely returns listIterator(), and the size method merely returns the subclass's size field.

All methods first check to see if the actual modCount of the backing list is equal to its expected value, and throw a ConcurrentModificationException if it is not.

Specified by:
subList in interface java.util.List
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.
Throws:
java.lang.IndexOutOfBoundsException - endpoint index value out of range (fromIndex < 0 || toIndex > size)
java.lang.IllegalArgumentException - endpoint indices out of order (fromIndex > toIndex)

toArray

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

Specified by:
toArray in interface java.util.List
Overrides:
toArray in class POCollection
Returns:
an array containing all of the elements in this list in the correct order.

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
Returns an array containing all of the elements in this list in the correct order. The runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits into 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.List
Overrides:
toArray in class POCollection
Parameters:
a - the array into which the 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 the elements of the list.
Throws:
java.lang.ArrayStoreException - if the runtime type of a is not a supertype of the runtime type of every element in this list.

trimToSize

public void trimToSize()
Trims the capacity of this POList instance to be the list's current size. An application can use this operation to minimize the storage of an POList instance.