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

com.databasesandlife.util.CompositeIterator Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Takes an array of iterators, and returns their results one after another.
 * 
 * @author This source is copyright Adrian Smith and licensed under the LGPL 3.
 * @see Project on GitHub
 */
public class CompositeIterator implements Iterator {
    
    Iterator[] children;
    int nextElementFrom = 0; // set to children.length means finished
    
    public CompositeIterator(Iterator[] children) {
        this.children = children;
        advanceIfNecessary();
    }

    protected void advanceIfNecessary() {
        while (nextElementFrom < children.length && !children[nextElementFrom].hasNext()) nextElementFrom++;
    }

    public boolean hasNext() {
        return nextElementFrom < children.length;
    }
    
    public T next() {
        if ( ! hasNext()) throw new NoSuchElementException();
        var result = children[nextElementFrom].next();
        advanceIfNecessary();
        return result;
    }

    public void remove() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy