edu.northwestern.at.utils

## Class SortedArrayList<E>

- java.lang.Object
- - java.util.AbstractCollection<E>
  - - java.util.AbstractList<E>
    - - java.util.ArrayList<E>
      - - edu.northwestern.at.utils.SortedArrayList<E>

- All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.RandomAccess

      public class SortedArrayList<E>
      extends java.util.ArrayList<E>

  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

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected java.util.Comparator<E>` | `comparator` Optional Comparator used for comparing objects in the list.

    - ### Fields inherited from class java.util.AbstractList

      `modCount`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SortedArrayList()` Create empty sorted array list.
    `SortedArrayList(java.util.Collection<? extends E> collection)` Create sorted array list from a collection.
    `SortedArrayList(java.util.Collection<? extends E> collection, java.util.Comparator<E> comparator)` Create sorted array list from a collection with specified comparator.
    `SortedArrayList(java.util.Comparator<E> comparator)` Create empty sorted array list with specified Comparator.
    `SortedArrayList(E[] array)` Create sorted array list from an array.
    `SortedArrayList(E[] array, java.util.Comparator<E> comparator)` Create sorted array list from an array with specified comparator.
    `SortedArrayList(int initialCapacity)` Create empty sorted array list with specified initial capacity.
    `SortedArrayList(int initialCapacity, java.util.Comparator<E> comparator)` Create empty sorted array list with specified initial capacity and comparator.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `add(E object)` Add an object to the list.
    `void` | `add(int index, E object)` Add element at specified index.
    `boolean` | `addAll(java.util.Collection<? extends E> collection)` Add all elements of a collection.
    `boolean` | `addAll(E[] array)` Add all elements of an array.
    `boolean` | `addAll(int index, java.util.Collection<? extends E> collection)` Add all elements of a collection at a specified index.
    `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.
    `E` | `set(int index, E object)` Set specified element of list.
    `void` | `setComparator(java.util.Comparator<E> comparator)` Set comparator for list elements.
    `protected void` | `sort()` Sort list.

    - ### Methods inherited from class java.util.ArrayList

      `clear, clone, ensureCapacity, get, isEmpty, iterator, listIterator, listIterator, remove, remove, removeAll, removeRange, retainAll, size, subList, toArray, toArray, trimToSize`

    - ### Methods inherited from class java.util.AbstractList

      `equals, hashCode`

    - ### Methods inherited from class java.util.AbstractCollection

      `containsAll, 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`

- - ### Field Detail

    - #### comparator

          protected java.util.Comparator<E> 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<? extends E> collection)

      Create sorted array list from a collection.
      Parameters:
        `collection` - The source collection.

    - #### SortedArrayList

          public SortedArrayList(E[] array)

      Create sorted array list from an array.
      Parameters:
        `array` - The source array.

    - #### SortedArrayList

          public SortedArrayList(java.util.Comparator<E> comparator)

      Create empty sorted array list with specified Comparator.

    - #### SortedArrayList

          public SortedArrayList(java.util.Collection<? extends E> collection,
                         java.util.Comparator<E> comparator)

      Create sorted array list from a collection with specified comparator.
      Parameters:
        `collection` - The source collection.
        `comparator` - The comparator.

    - #### SortedArrayList

          public SortedArrayList(E[] array,
                         java.util.Comparator<E> 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<E> comparator)

      Create empty sorted array list with specified initial capacity and comparator.

  - ### Method Detail

    - #### setComparator

          public void setComparator(java.util.Comparator<E> comparator)

      Set comparator for list elements.
      Parameters:
        `comparator` - The comparator.

    - #### add

          public boolean add(E object)

      Add an object to the list.
      Specified by:
        `add` in interface `java.util.Collection<E>`
      Specified by:
        `add` in interface `java.util.List<E>`
      Overrides:
        `add` in class `java.util.ArrayList<E>`
      Parameters:
        `object` - Object to add.
      Returns:
        True if object added.

    - #### add

          public void add(int index,
                 E object)

      Add element at specified index. Not allowed.
      Specified by:
        `add` in interface `java.util.List<E>`
      Overrides:
        `add` in class `java.util.ArrayList<E>`

    - #### addAll

          public boolean addAll(java.util.Collection<? extends E> collection)

      Add all elements of a collection.
      Specified by:
        `addAll` in interface `java.util.Collection<E>`
      Specified by:
        `addAll` in interface `java.util.List<E>`
      Overrides:
        `addAll` in class `java.util.ArrayList<E>`
      Parameters:
        `collection` - The source collection.
      Returns:
        True if all objects added, false otherwise.

    - #### addAll

          public boolean addAll(E[] 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<? extends E> collection)

      Add all elements of a collection at a specified index. Not allowed.
      Specified by:
        `addAll` in interface `java.util.List<E>`
      Overrides:
        `addAll` in class `java.util.ArrayList<E>`

    - #### contains

          public boolean contains(java.lang.Object object)

      Determine if list contains a specified value.
      Specified by:
        `contains` in interface `java.util.Collection<E>`
      Specified by:
        `contains` in interface `java.util.List<E>`
      Overrides:
        `contains` in class `java.util.ArrayList<E>`
      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<E>`
      Overrides:
        `indexOf` in class `java.util.ArrayList<E>`
      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<E>`
      Overrides:
        `lastIndexOf` in class `java.util.ArrayList<E>`
      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 E set(int index,
              E object)

      Set specified element of list. Not allowed.
      Specified by:
        `set` in interface `java.util.List<E>`
      Overrides:
        `set` in class `java.util.ArrayList<E>`

    - #### 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.

    - #### sort

          protected void sort()

      Sort list.