001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.util;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022:
023: /**
024: * Iterator for a set of sets. The iterator iterats
025: * over all possible combinations.
026: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
027: * @version 3.4 <7 March 05>
028: * @since 1.2
029: */
030: public class MultipleCollectionIterator implements Iterator {
031:
032: private Collection[] colls = null;
033: private Iterator[] iterators = null;
034: private Object[] lastRecord = null;
035:
036: /**
037: * Constructor.
038: * @param collections java.util.Collection[]
039: */
040: public MultipleCollectionIterator(Collection[] collections) {
041: super ();
042:
043: colls = collections;
044:
045: initialize();
046: }
047:
048: /**
049: * Indicates whether there is a next object.
050: * @return boolean
051: */
052: public boolean hasNext() {
053: return lastNonEmptyIteratorIndex() != -1;
054: }
055:
056: /**
057: * Initialize the object.
058: */
059: private void initialize() {
060: iterators = new Iterator[colls.length];
061:
062: for (int i = 0; i < colls.length; i++) {
063: iterators[i] = colls[i].iterator();
064: }
065: }
066:
067: /**
068: * Find the last index of an iterator having a next element.
069: * Return -1 if there is no such iterator.
070: * @return int
071: */
072: private int lastNonEmptyIteratorIndex() {
073: int index = -1;
074:
075: for (int i = 0; i < iterators.length; i++) {
076: if (iterators[i].hasNext()) {
077: index = i;
078: }
079: }
080:
081: return index;
082: }
083:
084: /**
085: * Get the next object. The next object is a vector containing exactly one
086: * object from each input collection.
087: * @return java.lang.Object
088: */
089: public Object next() {
090: int index = lastNonEmptyIteratorIndex();
091:
092: // reset iterators
093: for (int i = index + 1; i < iterators.length; i++) {
094: iterators[i] = colls[i].iterator();
095: }
096:
097: // build new object
098: Object[] next = new Object[iterators.length];
099:
100: if (lastRecord == null) {
101: for (int i = 0; i < iterators.length; i++) {
102: next[i] = iterators[i].next();
103: }
104: } else {
105: for (int i = 0; i < iterators.length; i++) {
106: if (i < index) {
107: next[i] = lastRecord[i];
108: } else {
109: next[i] = (iterators[i].next());
110: }
111: }
112: }
113:
114: lastRecord = next;
115:
116: return next;
117: }
118:
119: /**
120: * Remove elements is not supported.
121: */
122: public void remove() {
123: throw new UnsupportedOperationException();
124: }
125: }
|