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

com.tinkerpop.pipes.sideeffect.OptionalPipe Maven / Gradle / Ivy

Go to download

Pipes is a dataflow framework written in Java that enables the splitting, merging, filtering, and transformation of data from input to output. Computations are expressed using a combinator model and are evaluated in a memory-efficient, lazy fashion.

There is a newer version: 2.6.0
Show newest version
package com.tinkerpop.pipes.sideeffect;

import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.util.AbstractMetaPipe;
import com.tinkerpop.pipes.util.MetaPipe;
import com.tinkerpop.pipes.util.PipeHelper;
import com.tinkerpop.pipes.util.iterators.SingleExpandableIterator;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * OptionalPipe will compute the incoming object within the internal pipe.
 * It is similar to BackFilterPipe, except that no filtering occurs.
 * Moreover, it is a SideEffectPipe in that it is only useful for its side-effect (internal pipe) behavior.
 * However, getSideEffect() simply returns null as there is no internal data structure.
 *
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class OptionalPipe extends AbstractMetaPipe implements SideEffectPipe, MetaPipe {

    private final Pipe pipe;
    private final SingleExpandableIterator expando = new SingleExpandableIterator();

    public OptionalPipe(final Pipe pipe) {
        this.pipe = pipe;
        this.pipe.setStarts(this.expando);
    }

    public S processNextStart() {
        final S s = this.starts.next();
        this.expando.add(s);
        try {
            while (true) {
                this.pipe.next();
            }
        } catch (final NoSuchElementException e) {
        }
        return s;
    }

    /**
     * The side effect is the behavior of the internal pipe which is not a gettable data structure.
     *
     * @return will return null
     */
    public Object getSideEffect() {
        return null;
    }

    public String toString() {
        return PipeHelper.makePipeString(this, this.pipe);
    }

    public List getPipes() {
        return (List) Arrays.asList(this.pipe);
    }

}