01: /*
02: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package org.mandarax.util;
19:
20: import org.mandarax.kernel.Clause;
21: import org.mandarax.kernel.ClauseSetException;
22:
23: /**
24: * Iterator for clauses. This iterator simple merges other iterators:
25: * it first iterates the first iterator, then the second iterator, and so on.
26: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
27: * @version 3.4 <7 March 05>
28: * @since 1.2
29: */
30: public final class MergedClauseIterator extends AbstractClauseIterator {
31:
32: private ClauseIterator[] slaves = null;
33: private int index = 0;
34: private Clause nextClause = null;
35:
36: /**
37: * Constructor.
38: * @param iterators an array of clause iterators
39: */
40: public MergedClauseIterator(ClauseIterator[] iterators) {
41: super ();
42:
43: slaves = iterators;
44: }
45:
46: /**
47: * Constructor.
48: * @param iterators a collection of clause iterators
49: */
50: public MergedClauseIterator(java.util.Collection iterators) {
51: super ();
52:
53: Object objs = iterators.toArray();
54:
55: slaves = new ClauseIterator[iterators.size()];
56:
57: System.arraycopy(objs, 0, slaves, 0, iterators.size());
58: }
59:
60: /**
61: * Indicates whether there is a next clause.
62: * @return boolean
63: * @throws ClauseSetException
64: */
65: public boolean hasMoreClauses() throws ClauseSetException {
66: nextClause = null;
67:
68: if ((index < slaves.length) && (index > -1)) {
69: if (slaves[index].hasMoreClauses()) {
70: nextClause = slaves[index].nextClause();
71:
72: return true;
73: } else {
74:
75: // go to next slave
76: index = index + 1;
77:
78: return hasMoreClauses();
79: }
80: }
81:
82: return false;
83: }
84:
85: /**
86: * Return the next clause.
87: * @return the next clause.
88: */
89: public Clause nextClause() {
90: return nextClause;
91: }
92: }
|