/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.util;


import java.util.Collection;
import java.util.Iterator;

/**
 * Iterator for a set of sets. The iterator iterats
 * over all possible combinations.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.2
 */
public class MultipleCollectionIterator implements Iterator {

    private Collection[] colls      = null;
    private Iterator[]   iterators  = null;
    private Object[]     lastRecord = null;

    /**
     * Constructor.
     * @param collections java.util.Collection[]
     */
    public MultipleCollectionIterator(Collection[] collections) {
        super ();

        colls = collections;

        initialize ();
    }

    /**
     * Indicates whether there is a next object.
     * @return boolean
     */
    public boolean hasNext() {
        return lastNonEmptyIteratorIndex () != -1;
    }

    /**
     * Initialize the object.
     */
    private void initialize() {
        iterators = new Iterator[colls.length];

        for(int i = 0; i < colls.length; i++) {
            iterators[i] = colls[i].iterator ();
        }
    }

    /**
     * Find the last index of an iterator having a next element.
     * Return -1 if there is no such iterator.
     * @return int
     */
    private int lastNonEmptyIteratorIndex() {
        int index = -1;

        for(int i = 0; i < iterators.length; i++) {
            if(iterators[i].hasNext ()) {
                index = i;
            }
        }

        return index;
    }
    /**
     *  Get the next object. The next object is a vector containing exactly one
     *  object from each input collection.
     *  @return java.lang.Object
     */
    public Object next() {
        int index = lastNonEmptyIteratorIndex ();

        // reset iterators
        for(int i = index + 1; i < iterators.length; i++) {
            iterators[i] = colls[i].iterator ();
        }

        // build new object
        Object[] next = new Object[iterators.length];

        if(lastRecord == null) {
            for(int i = 0; i < iterators.length; i++) {
                next[i] = iterators[i].next ();
            }
        } else {
            for(int i = 0; i < iterators.length; i++) {
                if(i < index) {
                    next[i] = lastRecord[i];
                } else {
                    next[i] = (iterators[i].next ());
                }
            }
        }

        lastRecord = next;

        return next;
    }

    /**
     * Remove elements is not supported.
     */
    public void remove() {
        throw new UnsupportedOperationException ();
    }
}
