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

com.puresoltechnologies.streaming.StackedStreamIterator Maven / Gradle / Ivy

package com.puresoltechnologies.streaming;

import java.util.Iterator;
import java.util.Objects;
import java.util.function.Function;

/**
 * This decorator is used to stack multiple iterators, like iterating over
 * directories and then on each directory iterating over the files.
 * 
 * @author Rick-Rainer Ludwig
 *
 * @param 
 *            is the original type of the base iterator.
 * @param 
 *            is the type of this iterator.
 */
public class StackedStreamIterator extends AbstractStreamIterator {

    private final Iterator origin;
    private final Function> creator;
    private Iterator currentIterator = null;

    public StackedStreamIterator(Iterator origin, Function> creator) {
	Objects.requireNonNull(origin, "Origin iterator must not be null");
	Objects.requireNonNull(creator, "Creator must not be null");
	this.origin = origin;
	this.creator = creator;
    }

    @Override
    protected F findNext() {
	while ((currentIterator == null) || (!currentIterator.hasNext())) {
	    if (!origin.hasNext()) {
		currentIterator = null;
		return null;
	    }
	    currentIterator = creator.apply(origin.next());
	}
	return currentIterator.next();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy