View Javadoc

    1   package sharin.unlinq;
    2
    3   import java.util.Iterator;
    4
    5   public class ConcatenatedIterator<T> implements Iterator<T> {
    6
    7       private Iterator<T> iterator;
    8
    9       private final Iterator<T> second;
    10
    11      public ConcatenatedIterator(Iterator<T> first, Iterator<T> second) {
    12          this.iterator = first;
    13          this.second = second;
    14      }
    15
    16      public boolean hasNext() {
    17
    18          if (iterator.hasNext()) {
    19              return true;
    20          }
    21
    22          if (iterator == second) {
    23              return false;
    24          }
    25
    26          iterator = second;
    27          return iterator.hasNext();
    28      }
    29
    30      public T next() {
    31          return iterator.next();
    32      }
    33
    34      public void remove() {
    35          throw new UnsupportedOperationException();
    36      }
    37
    38  }