org.xshare.base.iterator
Class SortingIterator

java.lang.Object
  |
  +--org.xshare.base.iterator.SortingIterator
All Implemented Interfaces:
java.util.Iterator

public class SortingIterator
extends java.lang.Object
implements java.util.Iterator

Sorts the elements of a given Collection or Iterator according to a users specification.

In many situations you have to process the elements of a collection or elements provided by an iterator in a specific order - but the source of the elements provides them not in the order you need. This class helps you to process the elements of the source in the order you need by specifing a custom Comparator.

Example...

You have a presorted list with users and you want to print a list with all of them sorted by name.

 private LinkedList usersSortedByAge;

 private printUsersSortedByName(Iterator source) {
      SortingIterator si = new SortingIterator(source,
          getComparatorForByName());

      while (userItr.hasNext()) {
          System.out.println("User: " + userItr.next());
      }
 }

Notes On the Implementation

Version:
$Revision: 1.3 $
Author:
Oliver Fischer
See Also:
RangeIterator, ReverseIterator, Comparator

Constructor Summary
SortingIterator(java.util.Collection pCollection, java.util.Comparator pCustomComparator)
           
SortingIterator(java.util.Iterator pSourceIterator, java.util.Comparator pCustomComparator)
           
 
Method Summary
 boolean hasNext()
          Checks if the sorted iteration has more elements to return or not.
 java.lang.Object next()
          Returns the next element according to the internal sorting of the maintained elements.
 void remove()
          This method is not supported since this class is only a proxy for a underlying collection with an different representation of its elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SortingIterator

public SortingIterator(java.util.Iterator pSourceIterator,
                       java.util.Comparator pCustomComparator)

SortingIterator

public SortingIterator(java.util.Collection pCollection,
                       java.util.Comparator pCustomComparator)
Method Detail

remove

public void remove()
This method is not supported since this class is only a proxy for a underlying collection with an different representation of its elements.

Specified by:
remove in interface java.util.Iterator
Throws:
java.lang.UnsupportedOperationException - if this method is called.

next

public java.lang.Object next()
Returns the next element according to the internal sorting of the maintained elements.

Specified by:
next in interface java.util.Iterator
Returns:
an Object representing the next element to return according to the internal sorting of the elements.
Throws:
NoSuchElementsException - if hasNext() returns false, that means that there are no elements left to return.
See Also:
hasNext()

hasNext

public boolean hasNext()
Checks if the sorted iteration has more elements to return or not.

Specified by:
hasNext in interface java.util.Iterator
Returns:
true if there are more elements to return. Otherwise false is returned.


jKiska Base 0.10 [http://jkiska.sourceforge.net]