edu.northwestern.at.utils
Class SortedArrayList

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList
              extended by edu.northwestern.at.utils.SortedArrayList
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.List, java.util.RandomAccess

public class SortedArrayList
extends java.util.ArrayList

Sorted array list.

This class extends ArrayList to support sorted lists.

All elements of the list should implement the Comparable interface, or you may supply a custom Comparator instead. If the objects do not support comparable and you do not supply a custom Comparator, the result of the toString() function for each object is used.

The ArrayList methods for adding or settings elements at specific indices are not supported raise an UnsupportedOperationException if used.

See Also:
Serialized Form

Field Summary
protected  java.util.Comparator comparator
          Optional Comparator used for comparing objects in the list.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
SortedArrayList()
          Create empty sorted array list.
SortedArrayList(java.util.Collection collection)
          Create sorted array list from a collection.
SortedArrayList(java.util.Collection collection, java.util.Comparator comparator)
          Create sorted array list from a collection with specified comparator.
SortedArrayList(java.util.Comparator comparator)
          Create empty sorted array list with specified Comparator.
SortedArrayList(int initialCapacity)
          Create empty sorted array list with specified initial capacity.
SortedArrayList(int initialCapacity, java.util.Comparator comparator)
          Create empty sorted array list with specified initial capacity and comparator.
SortedArrayList(java.lang.Object[] array)
          Create sorted array list from an array.
SortedArrayList(java.lang.Object[] array, java.util.Comparator comparator)
          Create sorted array list from an array with specified comparator.
 
Method Summary
 void add(int index, java.lang.Object object)
          Add element at specified index.
 boolean add(java.lang.Object object)
          Add an object to the list.
 boolean addAll(java.util.Collection collection)
          Add all elements of a collection.
 boolean addAll(int index, java.util.Collection collection)
          Add all elements of a collection at a specified index.
 boolean addAll(java.lang.Object[] array)
          Add all elements of an array.
protected  int compare(java.lang.Object object1, java.lang.Object object2)
          Compares two elements.
 boolean contains(java.lang.Object object)
          Determine if list contains a specified value.
 int indexOf(java.lang.Object object)
          Return index of first matching list entry.
 int lastIndexOf(java.lang.Object object)
          Return index of last matching list entry.
 java.lang.Object set(int index, java.lang.Object object)
          Set specified element of list.
 void setComparator(java.util.Comparator comparator)
          Set comparator for list elements.
 
Methods inherited from class java.util.ArrayList
clear, clone, ensureCapacity, get, isEmpty, remove, remove, removeRange, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Field Detail

comparator

protected java.util.Comparator comparator
Optional Comparator used for comparing objects in the list.

Constructor Detail

SortedArrayList

public SortedArrayList()
Create empty sorted array list.


SortedArrayList

public SortedArrayList(int initialCapacity)
Create empty sorted array list with specified initial capacity.


SortedArrayList

public SortedArrayList(java.util.Collection collection)
Create sorted array list from a collection.

Parameters:
collection - The source collection.

SortedArrayList

public SortedArrayList(java.lang.Object[] array)
Create sorted array list from an array.

Parameters:
array - The source array.

SortedArrayList

public SortedArrayList(java.util.Comparator comparator)
Create empty sorted array list with specified Comparator.


SortedArrayList

public SortedArrayList(java.util.Collection collection,
                       java.util.Comparator comparator)
Create sorted array list from a collection with specified comparator.

Parameters:
collection - The source collection.
comparator - The comparator.

SortedArrayList

public SortedArrayList(java.lang.Object[] array,
                       java.util.Comparator comparator)
Create sorted array list from an array with specified comparator.

Parameters:
array - The source array.
comparator - The comparator.

SortedArrayList

public SortedArrayList(int initialCapacity,
                       java.util.Comparator comparator)
Create empty sorted array list with specified initial capacity and comparator.

Method Detail

setComparator

public void setComparator(java.util.Comparator comparator)
Set comparator for list elements.

Parameters:
comparator - The comparator.

add

public boolean add(java.lang.Object object)
Add an object to the list.

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Overrides:
add in class java.util.ArrayList
Parameters:
object - Object to add.
Returns:
True if object added.

add

public void add(int index,
                java.lang.Object object)
Add element at specified index. Not allowed.

Specified by:
add in interface java.util.List
Overrides:
add in class java.util.ArrayList

addAll

public boolean addAll(java.util.Collection collection)
Add all elements of a collection.

Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Overrides:
addAll in class java.util.ArrayList
Parameters:
collection - The source collection.
Returns:
True if all objects added, false otherwise.

addAll

public boolean addAll(java.lang.Object[] array)
Add all elements of an array.

Parameters:
array - The source array.
Returns:
True if all objects added, false otherwise.

addAll

public boolean addAll(int index,
                      java.util.Collection collection)
Add all elements of a collection at a specified index. Not allowed.

Specified by:
addAll in interface java.util.List
Overrides:
addAll in class java.util.ArrayList

contains

public boolean contains(java.lang.Object object)
Determine if list contains a specified value.

Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.List
Overrides:
contains in class java.util.ArrayList
Parameters:
object - The object (value) to find.
Returns:
true if the list contains specified object, false otherwise.

indexOf

public int indexOf(java.lang.Object object)
Return index of first matching list entry.

Specified by:
indexOf in interface java.util.List
Overrides:
indexOf in class java.util.ArrayList
Parameters:
object - The object to find.
Returns:
Index of the first matching object, if any. Returns -1 if no matching object is found.

lastIndexOf

public int lastIndexOf(java.lang.Object object)
Return index of last matching list entry.

Specified by:
lastIndexOf in interface java.util.List
Overrides:
lastIndexOf in class java.util.ArrayList
Parameters:
object - The object to find.
Returns:
Index of the last matching object, if any. Returns -1 if no matching object is found.

set

public java.lang.Object set(int index,
                            java.lang.Object object)
Set specified element of list. Not allowed.

Specified by:
set in interface java.util.List
Overrides:
set in class java.util.ArrayList

compare

protected int compare(java.lang.Object object1,
                      java.lang.Object object2)
Compares two elements.

Parameters:
object1 - First object.
object2 - Second object.
Returns:
Result of comparison.

Assumes the two objects are not null, since null objects cannot added to the list. If no comparator is defined for this SortedArrayList, and one or both of the objects are not comparable, a case-ignoring comparison is performed on the "toString()" values for each object.