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

com.tinkerpop.pipes.transform.HasCountPipe 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.transform;

import com.tinkerpop.pipes.AbstractPipe;
import com.tinkerpop.pipes.util.FastNoSuchElementException;

/**
 * @author Darrick Wiebe (http://ofallpossibleworlds.wordpress.com)
 */
public class HasCountPipe extends AbstractPipe implements TransformPipe {

    private long minimum;
    private long maximum;
    private long counter = 0;
    private boolean finished = false;

    /**
     * The pipe must emit at least min and not more than max elements.
     *
     * @param min minimum number of elements. Use -1 for no minimum.
     * @param max maximum number of elements. Use -1 for no maximum.
     */
    public HasCountPipe(long min, long max) {
        this.minimum = min;
        this.maximum = max;
    }

    public Boolean processNextStart() {
        if (this.finished) {
            throw FastNoSuchElementException.instance();
        }
        this.finished = true;
        if (this.minimum == -1 && this.maximum == -1)
            return Boolean.TRUE;
        while (true) {
            if (this.starts.hasNext()) {
                if (this.counter == this.maximum) {
                    return Boolean.FALSE;
                }
                this.counter++;
                if (this.minimum != -1 && this.counter == this.minimum && this.maximum == -1) {
                    return Boolean.TRUE;
                }
                this.starts.next();
            } else if (this.maximum != -1 && this.counter >= this.minimum) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        }
    }

    public void reset() {
        super.reset();
        this.counter = 0;
        this.finished = false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy