001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.util.NoSuchElementException;
034:
035: /**
036: * An Iterator that returns the elements of a specified array, or other
037: * iterators etc. The collection of objects returned depends on the
038: * constructor used.<p>
039: *
040: * Based on similar Enumerator code by boucherb@users
041: *
042: * @author fred@users
043: * @version 1.7.2
044: * @since HSQLDB 1.7.2
045: */
046: public class WrapperIterator implements Iterator {
047:
048: private static final Object[] emptyelements = new Object[0];
049: private Object[] elements;
050: private int i;
051:
052: // chained iterators
053: private boolean chained;
054: private Iterator it1;
055: private Iterator it2;
056:
057: /** return only not null elements */
058: private boolean notNull;
059:
060: /**
061: * Constructor for an empty iterator. <p>
062: */
063: public WrapperIterator() {
064: this .elements = emptyelements;
065: }
066:
067: /**
068: * Constructor for all elements of the specified array. <p>
069: *
070: * @param elements the array of objects to enumerate
071: */
072: public WrapperIterator(Object[] elements) {
073: this .elements = elements;
074: }
075:
076: /**
077: * Constructor for not-null elements of specified array. <p>
078: *
079: * @param elements the array of objects to iterate
080: */
081: public WrapperIterator(Object[] elements, boolean notNull) {
082: this .elements = elements;
083: this .notNull = notNull;
084: }
085:
086: /**
087: * Constructor for a singleton object iterator
088: *
089: * @param element the single object to iterate
090: */
091: public WrapperIterator(Object element) {
092: this .elements = new Object[] { element };
093: }
094:
095: /**
096: * Constructor for a chained iterator that retuns the elements of the two
097: * specified iterators.
098: *
099: * @param element the single object to iterate
100: */
101: public WrapperIterator(Iterator it1, Iterator it2) {
102:
103: this .it1 = it1;
104: this .it2 = it2;
105: chained = true;
106: }
107:
108: /**
109: * Tests if this iterator contains more elements. <p>
110: *
111: * @return <code>true</code> if this iterator contains more elements;
112: * <code>false</code> otherwise.
113: */
114: public boolean hasNext() {
115:
116: // for chained iterators
117: if (chained) {
118: if (it1 == null) {
119: if (it2 == null) {
120: return false;
121: }
122:
123: if (it2.hasNext()) {
124: return true;
125: }
126:
127: it2 = null;
128:
129: return false;
130: } else {
131: if (it1.hasNext()) {
132: return true;
133: }
134:
135: it1 = null;
136:
137: return hasNext();
138: }
139: }
140:
141: // for other interators
142: if (elements == null) {
143: return false;
144: }
145:
146: for (; notNull && i < elements.length && elements[i] == null; i++) {
147: }
148:
149: if (i < elements.length) {
150: return true;
151: } else {
152:
153: // release elements for garbage collection
154: elements = null;
155:
156: return false;
157: }
158: }
159:
160: /**
161: * Returns the next element.
162: *
163: * @return the next element
164: * @throws NoSuchElementException if there is no next element
165: */
166: public Object next() {
167:
168: // for chained iterators
169: if (chained) {
170: if (it1 == null) {
171: if (it2 == null) {
172: throw new NoSuchElementException();
173: }
174:
175: if (it2.hasNext()) {
176: return it2.next();
177: }
178:
179: it2 = null;
180:
181: next();
182: } else {
183: if (it1.hasNext()) {
184: return it1.next();
185: }
186:
187: it1 = null;
188:
189: next();
190: }
191: }
192:
193: // for other itertors
194: if (hasNext()) {
195: return elements[i++];
196: }
197:
198: throw new NoSuchElementException();
199: }
200:
201: public int nextInt() {
202: throw new NoSuchElementException();
203: }
204:
205: public long nextLong() {
206: throw new NoSuchElementException();
207: }
208:
209: public void remove() {
210: throw new NoSuchElementException();
211: }
212: }
|