Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: Java  |  size: 0.73 KB  |  hits: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class PushBackIterator<E extends Comparable<E>> implements Iterator<E> {
  2.  
  3.     private Iterator<E> it;
  4.     private E stackedElement;
  5.  
  6.     PushBackIterator(Iterator<E> i) {
  7.         it = i;
  8.     }
  9.  
  10.     public void remove() {
  11.         it.remove();
  12.     }
  13.  
  14.     public boolean hasNext() {
  15.         return stackedElement instanceof Comparable || it.hasNext();
  16.     }
  17.  
  18.     public E next() {
  19.         if(stackedElement instanceof Comparable) {
  20.             E tmp = stackedElement;
  21.             stackedElement = null;
  22.             return tmp;
  23.         }
  24.  
  25.         return it.next();
  26.     }
  27.  
  28.     /**
  29.      * Pushes back an element to the iterator
  30.      * @param e
  31.      */
  32.     public void pushBack(E e) {
  33.         stackedElement = e;
  34.     }
  35. }