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

com.oracle.coherence.common.collections.ChainedIterator Maven / Gradle / Ivy

There is a newer version: 24.09
Show newest version
/*
 * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.oracle.coherence.common.collections;


import java.util.Iterator;


/**
 * Provide an Iterator which iterates over the contents of multiple Iterators.
 *
 * @author cp  1998.08.07, 2010.12.08
 */
public class ChainedIterator
        extends AbstractStableIterator
    {
    // ----- constructors ---------------------------------------------------

    /**
    * Construct an enumerator that will first enumerate the first Iterator
    * and then will enumerate the second Iterator as if they were together
    * a single Iterator.
    *
    * @param iterFirst   the first Iterator
    * @param iterSecond  the second Iterator
    */
    public ChainedIterator(Iterator iterFirst, Iterator iterSecond)
        {
        this(new Iterator[] {iterFirst, iterSecond});
        }

    /**
    * Construct an enumerator that will first enumerate the Iterators
    * passed in the array as if they were together a single enumerator.
    *
    * @param aIter  an array of Iterators
    */
    public ChainedIterator(Iterator[] aIter)
        {
        m_aIter = aIter;
        }


    // ----- Iterator interface ---------------------------------------------

    /**
    * {@inheritDoc}
    */
    @Override
    public T next()
        {
        T oNext = super.next();
        m_iterPrevious = m_iterCurrent;
        return oNext;
        }


    // ----- internal -------------------------------------------------------

    /**
    * {@inheritDoc}
    */
    @Override
    protected void advance()
        {
        Iterator iter = m_iterCurrent;

        if (iter == null || !iter.hasNext())
            {
            Iterator[] aIter  = m_aIter;
            int           cIters = aIter.length;
            int           iNext  = m_iNextIter;
            do
                {
                if (iNext >= cIters)
                    {
                    return;
                    }

                iter = aIter[iNext++];
                }
            while (!iter.hasNext());

            m_iNextIter   = iNext;
            m_iterCurrent = iter;
            }

        setNext(iter.next());
        }

    /**
    * {@inheritDoc}
    */
    @Override
    protected void remove(T oPrev)
        {
        Iterator iter = m_iterPrevious;
        if (iter == null)
            {
            throw new IllegalStateException();
            }
        iter.remove();
        }


    // ----- data members ---------------------------------------------------

    /**
    * The Iterators.
    */
    private final Iterator[] m_aIter;

    /**
    * The next Iterator (index into m_aIter) to iterate.
    */
    private int m_iNextIter;

    /**
    * The current Iterator.
    */
    private Iterator m_iterCurrent;

    /**
    * The previous Iterator.
    */
    private Iterator m_iterPrevious;
    }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy