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

com.tinkerpop.pipes.sideeffect.StorePipe 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.AbstractPipe;
import com.tinkerpop.pipes.PipeFunction;

import java.util.Collection;

/**
 * The StoragePipe yields its input, however, each input is added to the provided collection.
 * If a PipeFunction is provided then it process the object and stores the result of that process.
 * This pipe is similar to AggregatePipe except that is does not aggregate everything up to the current step.
 * Instead, it fills the storage collection as objects pass through it.
 *
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class StorePipe extends AbstractPipe implements SideEffectPipe {

    private Collection storage;
    private PipeFunction preStoreFunction = null;

    public StorePipe(final Collection storage) {
        this.storage = storage;
    }

    /**
     * The provided PipeFunction will process the object prior to inserting it into the storage collection.
     *
     * @param storage          the storage collection
     * @param preStoreFunction a function to process an object with prior to insertion into the collection
     */
    public StorePipe(final Collection storage, final PipeFunction preStoreFunction) {
        this(storage);
        this.preStoreFunction = preStoreFunction;
    }

    protected S processNextStart() {
        final S s = this.starts.next();
        if (null != this.preStoreFunction) {
            this.storage.add(this.preStoreFunction.compute(s));
        } else {
            this.storage.add(s);
        }
        return s;
    }

    public Collection getSideEffect() {
        return this.storage;
    }

    public void reset() {
        try {
            this.storage = this.storage.getClass().getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        super.reset();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy