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

net.java.truecommons.shed.CompoundIterator Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/*
 * Copyright (C) 2005-2012 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */
package net.java.truecommons.shed;

import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * Concatenates two iterators.
 *
 * @param   the type of the iterated elements.
 * @author Christian Schlichtherle
 */
@NotThreadSafe
public final class CompoundIterator implements Iterator {
    private Iterator first, second;

    public CompoundIterator(
            final Iterator first,
            final Iterator second) {
        if (null == (this.first = first)) throw new NullPointerException();
        if (null == (this.second = second)) throw new NullPointerException();
    }

    @Override
    public boolean hasNext() {
        return first.hasNext() || (first != second && (first = second).hasNext());
    }

    @Override
    public E next() {
        try {
            return first.next();
        } catch (NoSuchElementException ex) {
            if (first == second) throw ex;
            return (first = second).next();
        }
    }

    @Override
    public void remove() {
        first.remove();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy