jodd.util.collection
Class SortedArrayList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<E>
              extended by jodd.util.collection.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>

An extension of ArrayList that insures that all of the items added are sorted. This breaks original list contract!. A binary search method is used to provide a quick way to auto sort this list.Note: Not all methods for adding and removing elements are supported.

See Also:
Serialized Form

Field Summary
protected  java.util.Comparator<E> comparator
           
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
SortedArrayList()
          Constructs a new SortedArrayList expecting elements are comparable.
SortedArrayList(java.util.Collection<? extends E> c)
          Constructs a new SortedArrayList expecting elements are comparable.
SortedArrayList(java.util.Comparator<E> c)
          Constructs a new SortedArrayList.
 
Method Summary
 boolean add(E o)
          Adds an Object to sorted list.
 void add(int index, E element)
           
 boolean addAll(java.util.Collection<? extends E> c)
          Add all of the elements in the given collection to this list.
 boolean addAll(int index, java.util.Collection<? extends E> c)
           
protected  int compare(E k1, E k2)
          Compares two keys using the correct comparison method for this collection.
 int findInsertionPoint(E o)
          Finds the index at which object should be inserted.
protected  int findInsertionPoint(E o, int low, int high)
          Conducts a binary search to find the index where Object should be inserted.
 java.util.Comparator getComparator()
          Returns comparator assigned to this collection, if such exist.
 E set(int index, E element)
           
 
Methods inherited from class java.util.ArrayList
clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, 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 final java.util.Comparator<E> comparator
Constructor Detail

SortedArrayList

public SortedArrayList(java.util.Comparator<E> c)
Constructs a new SortedArrayList.


SortedArrayList

public SortedArrayList()
Constructs a new SortedArrayList expecting elements are comparable.


SortedArrayList

public SortedArrayList(java.util.Collection<? extends E> c)
Constructs a new SortedArrayList expecting elements are comparable.

Method Detail

getComparator

public java.util.Comparator getComparator()
Returns comparator assigned to this collection, if such exist.


add

public boolean add(E o)
Adds an Object to sorted list. Object is inserted at correct place, found using binary search. If the same item exist, it will be put to the end of the range.

This method breaks original list contract since objects are not added at the list end, but in sorted manner.

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>

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Add all of the elements in the given collection to this list.

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>

findInsertionPoint

public int findInsertionPoint(E o)
Finds the index at which object should be inserted.


add

public void add(int index,
                E element)
Specified by:
add in interface java.util.List<E>
Overrides:
add in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - This method not supported.

set

public E set(int index,
             E element)
Specified by:
set in interface java.util.List<E>
Overrides:
set in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - This method not supported.

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends E> c)
Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class java.util.ArrayList<E>
Throws:
java.lang.UnsupportedOperationException - This method not supported.

compare

protected int compare(E k1,
                      E k2)
Compares two keys using the correct comparison method for this collection.


findInsertionPoint

protected int findInsertionPoint(E o,
                                 int low,
                                 int high)
Conducts a binary search to find the index where Object should be inserted.



Copyright © 2003-2012 Jodd Team