org.xshare.base.iterator
Class LimitedIterator

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

public final class LimitedIterator
extends java.lang.Object
implements java.util.Iterator

Specialized iterator wrapper to limit the elements returned by another iteration.

Example

  public void printTopFive(PrintStream ps) {
      // Here I will wrap the iterator of a map - sorted
      // by the points of the players - to get max. 5 players
      Iterator i = new LimitedIterator(5, sortedPlayers.iterator());

      while (i.hasNext()) {
        ps.println(i.next());
      }
  }

 

Notes On The Implementation

Version:
$Revision: 1.4 $
Author:
Oliver Fischer
See Also:
OneElementIterator

Constructor Summary
LimitedIterator(int pMaxElements, java.util.Iterator pSourceIterator)
          Creates a new instance of this iterator which returns not more than a given number of elements.
 
Method Summary
 boolean hasNext()
          Checks if there are more elements to return or not.
 java.lang.Object next()
          Returns the next element of the underlying iteration.
 void remove()
          Removes the last element returned by next() from the underlaying iteration by forwarding the remove call to it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LimitedIterator

public LimitedIterator(int pMaxElements,
                       java.util.Iterator pSourceIterator)
Creates a new instance of this iterator which returns not more than a given number of elements.

Throws:
java.lang.IllegalArgumentException - if the limit of objects to return by this iterator is negativ
java.lang.NullPointerException - if the source iterator is null
Method Detail

remove

public void remove()
Removes the last element returned by next() from the underlaying iteration by forwarding the remove call to it.

Specified by:
remove in interface java.util.Iterator
Throws:
java.lang.UnsupportedOperationException - if the source iteration doesn't support this method.

next

public java.lang.Object next()
Returns the next element of the underlying iteration.

Specified by:
next in interface java.util.Iterator
Throws:
java.util.NoSuchElementException - if there are no more elements to return by this method. Use hasNext() to check this.
See Also:
hasNext()

hasNext

public boolean hasNext()
Checks if there are more elements to return or not. To check that, this method checks if the underlying iteration has still more elements to return and if the number of already returned elements is less then the maximum number of elements to return by this iterator.

Specified by:
hasNext in interface java.util.Iterator
Returns:
a boolean value which is true if there are more elements to return. Otherwise false is returned.
See Also:
next()


jKiska Base 0.2.1 Release Esche [http://jkiska.sourceforge.net]