fc.util
Class SortedCollection

java.lang.Object
  extended by java.util.AbstractCollection
      extended by fc.util.SortedCollection
All Implemented Interfaces:
Iterable, Collection

public class SortedCollection
extends AbstractCollection

Implements a Collection that keeps it's elements in Sorted order. This class is internally backed by a LinkedList, therefore iteration is strongly preferred (and much faster) than random access of it's elements. This class has no equivalent in JDK 1.4 and should be replaced by a future JDK equivalent if available. Currently java.util only provides a sorted Set not a sorted Collection (like this one) that allows duplicate elements.

Note that this implementation is not synchronized. If multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

    List list = Collections.synchronizedList(new LinkedList(...));
 


Constructor Summary
SortedCollection()
          Constructs a new, empty SortedCollection, sorted according to the keys' natural order.
SortedCollection(Collection c)
          Constructs a sorted list containing the elements of the specified collection.
SortedCollection(Comparator c)
          Constructs a new, empty SortedCollection, sorted according to the given comparator.
 
Method Summary
 boolean add(Object obj)
           
 Iterator iterator()
           
static void main(String[] args)
           
 int size()
           
 String toString()
           
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

SortedCollection

public SortedCollection()
Constructs a new, empty SortedCollection, sorted according to the keys' natural order. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, methods that add elements will throw a ClassCastException.


SortedCollection

public SortedCollection(Collection c)
Constructs a sorted list containing the elements of the specified collection. The elements are sorted according to the elements' natural order

Parameters:
c - the collection whose elements are to be placed into this list.
Throws:
NullPointerException - if the specified collection is null.

SortedCollection

public SortedCollection(Comparator c)
Constructs a new, empty SortedCollection, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, methods that add elements will throw a ClassCastException.

Parameters:
c - the comparator that will be used to sort this map. A null value indicates that the keys' natural ordering should be used.
Method Detail

add

public boolean add(Object obj)
Specified by:
add in interface Collection
Overrides:
add in class AbstractCollection

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in class AbstractCollection

size

public int size()
Specified by:
size in interface Collection
Specified by:
size in class AbstractCollection

toString

public String toString()
Overrides:
toString in class AbstractCollection

main

public static void main(String[] args)
                 throws Exception
Throws:
Exception