package com.tinkerpop.pipes.util;

import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.SingleIterator;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class HasNextPipe<S> extends AbstractPipe<S, Boolean> {

    private final Pipe<S, ?> pipe;

    public HasNextPipe(final Pipe<S, ?> pipe) {
        this.pipe = pipe;
    }

    public Boolean processNextStart() {
        final S s = this.starts.next();
        this.pipe.setStarts(new SingleIterator<S>(s));
        if (this.pipe.hasNext()) {
            this.pipe.next();
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}

@@ -148,14 +148,85 @@
 tor;
+%0Aimport java.util.NoSuchElementException;%0Aimport java.util.Iterator;
 %0A%0A/**%0A
+ *%0A
  * @
@@ -279,16 +279,84 @@
 ez.com)%0A
+ * @author Darrick Wiebe (http://ofallpossibleworlds.wordpress.com)%0A
  */%0Apubl
@@ -437,91 +437,724 @@
 nal 
-Pipe%3CS, ?%3E pipe;%0A%0A    public HasNextPipe(final Pipe%3CS, ?%3E pipe) %7B%0A        this.pipe
+boolean hasOtherPipe;%0A    private Pipe%3CS, ?%3E pipe;%0A    private Iterator iterator;%0A    private boolean finished = false;%0A%0A    /**%0A     * Initialize this pipe to apply its conditions to its source pipe.%0A     **/%0A    public HasNextPipe() %7B%0A        this.hasOtherPipe = false;%0A    %7D%0A%0A    /**%0A     * Initialize this pipe to apply its conditions to the given pipe. Each%0A     * element in the source pipe will be fed into the given pipe%0A     * individually.%0A     *%0A     * @param pipe The pipe that must emit the correct number of elements. The%0A     * pipe will be reset between each element.%0A     **/%0A    public HasNextPipe(Pipe%3CS, ?%3E pipe) %7B%0A        this.hasOtherPipe = true;%0A        this.pipe = pipe;%0A        this.iterator
  = p
@@ -1201,24 +1201,61 @@
 xtStart() %7B%0A
+        if (this.hasOtherPipe) %7B%0A    
         fina
@@ -1274,32 +1274,67 @@
 .starts.next();%0A
+            this.pipe.reset();%0A    
         this.pip
@@ -1372,32 +1372,39 @@
 S%3E(s));%0A        
+%7D else 
 if (this.pipe.ha
@@ -1396,20 +1396,203 @@
 f (this.
-pipe
+finished) %7B%0A            throw new NoSuchElementException();%0A        %7D else %7B%0A            this.iterator = this.starts;%0A            this.finished = true;%0A        %7D%0A        if (this.iterator
 .hasNext
@@ -1614,20 +1614,24 @@
    this.
-pipe
+iterator
 .next();
@@ -1730,11 +1730,98 @@
  %7D%0A    %7D
+%0A%0A    public void reset() %7B%0A        super.reset();%0A        this.finished = false;%0A    %7D
 %0A%7D%0A

Enable the HasNextPipe to be used by adding it to the end of a pipe.