All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.gremlin.process.util.AbstractStep Maven / Gradle / Ivy

package com.tinkerpop.gremlin.process.util;

import com.tinkerpop.gremlin.process.Step;
import com.tinkerpop.gremlin.process.Traversal;
import com.tinkerpop.gremlin.process.Traverser;
import com.tinkerpop.gremlin.structure.Graph;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public abstract class AbstractStep implements Step {

    protected String as;
    protected Traversal traversal;
    public ExpandableStepIterator starts;
    protected Traverser nextEnd;
    protected boolean available;

    protected Step previousStep = EmptyStep.instance();
    protected Step nextStep = EmptyStep.instance();

    public AbstractStep(final Traversal traversal) {
        this.traversal = traversal;
        this.starts = new ExpandableStepIterator((Step) this);
        this.as = Graph.Key.hide(Integer.toString(this.traversal.getSteps().size()));
    }

    public void addStarts(final Iterator> starts) {
        this.starts.add((Iterator) starts);
    }

    public void setPreviousStep(final Step step) {
        this.previousStep = step;
    }

    public Step getPreviousStep() {
        return this.previousStep;
    }

    public void setNextStep(final Step step) {
        this.nextStep = step;
    }

    public Step getNextStep() {
        return this.nextStep;
    }

    public void setAs(final String as) {
        this.as = as;
    }

    public String getAs() {
        return this.as;
    }

    public Traverser next() {
        if (this.available) {
            this.available = false;
            return this.nextEnd;
        } else {
            final Traverser traverser = this.processNextStart();
            traverser.setFuture(this.nextStep.getAs());
            return traverser;
        }
    }

    public boolean hasNext() {
        if (this.available)
            return true;
        else {
            try {
                this.nextEnd = this.processNextStart();
                this.nextEnd.setFuture(this.nextStep.getAs());
                this.available = true;
                return true;
            } catch (final NoSuchElementException e) {
                this.available = false;
                return false;
            }
        }
    }

    public  Traversal getTraversal() {
        return this.traversal;
    }

    protected abstract Traverser processNextStart() throws NoSuchElementException;

    public String toString() {
        return TraversalHelper.makeStepString(this);
    }

    public Object clone() throws CloneNotSupportedException {
        final AbstractStep step = (AbstractStep) super.clone();
        step.starts = new ExpandableStepIterator(step);
        step.previousStep = EmptyStep.instance();
        step.nextStep = EmptyStep.instance();
        step.available = false;
        step.nextEnd = null;
        return step;
    }
}