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

com.tinkerpop.pipes.branch.IfThenElsePipe 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.branch;

import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.PipeFunction;

import java.util.Iterator;

/**
 * IfThenElsePipe will run each incoming S through the provided ifFunction.
 * If the ifFunction returns true, then the S object is passed through the thenFunction.
 * Otherwise, the S object is passed through the elseFunction.
 * If the result of the thenFunction and the elseFunction is an iterable/iterator, it is unrolled.
 *
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class IfThenElsePipe extends AbstractPipe {

    private final PipeFunction ifFunction;
    private final PipeFunction thenFunction;
    private final PipeFunction elseFunction;
    private Iterator itty = null;

    public IfThenElsePipe(final PipeFunction ifFunction, final PipeFunction thenFunction, final PipeFunction elseFunction) {
        this.ifFunction = ifFunction;
        this.thenFunction = thenFunction;
        this.elseFunction = elseFunction;
    }

    public E processNextStart() {
        while (true) {
            if (null != this.itty && this.itty.hasNext()) {
                return this.itty.next();
            } else {
                final S s = this.starts.next();
                if (this.ifFunction.compute(s)) {
                    final Object e = this.thenFunction.compute(s);
                    if (e instanceof Iterable) {
                        this.itty = ((Iterable) e).iterator();
                    } else if (e instanceof Iterator) {
                        this.itty = (Iterator) e;
                    } else {
                        return (E) e;
                    }
                } else {
                    final Object e = this.elseFunction.compute(s);
                    if (e instanceof Iterable) {
                        this.itty = ((Iterable) e).iterator();
                    } else if (e instanceof Iterator) {
                        this.itty = (Iterator) e;
                    } else {
                        return (E) e;
                    }
                }
            }
        }
    }

    public void reset() {
        this.itty = null;
        super.reset();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy