KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > WrapperIterator


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.util.NoSuchElementException JavaDoc;
35
36 /**
37  * An Iterator that returns the elements of a specified array, or other
38  * iterators etc. The collection of objects returned depends on the
39  * constructor used.<p>
40  *
41  * Based on similar Enumerator code by boucherb@users
42  *
43  * @author fred@users
44  * @version 1.7.2
45  * @since HSQLDB 1.7.2
46  */

47 public class WrapperIterator implements Iterator {
48
49     private static final Object JavaDoc[] emptyelements = new Object JavaDoc[0];
50     private Object JavaDoc[] elements;
51     private int i;
52
53     // chained iterators
54
private boolean chained;
55     private Iterator it1;
56     private Iterator it2;
57
58     /** return only not null elements */
59     private boolean notNull;
60
61     /**
62      * Constructor for an empty iterator. <p>
63      */

64     public WrapperIterator() {
65         this.elements = emptyelements;
66     }
67
68     /**
69      * Constructor for all elements of the specified array. <p>
70      *
71      * @param elements the array of objects to enumerate
72      */

73     public WrapperIterator(Object JavaDoc[] elements) {
74         this.elements = elements;
75     }
76
77     /**
78      * Constructor for not-null elements of specified array. <p>
79      *
80      * @param elements the array of objects to iterate
81      */

82     public WrapperIterator(Object JavaDoc[] elements, boolean notNull) {
83         this.elements = elements;
84         this.notNull = notNull;
85     }
86
87     /**
88      * Constructor for a singleton object iterator
89      *
90      * @param element the single object to iterate
91      */

92     public WrapperIterator(Object JavaDoc element) {
93         this.elements = new Object JavaDoc[]{ element };
94     }
95
96     /**
97      * Constructor for a chained iterator that retuns the elements of the two
98      * specified iterators.
99      *
100      * @param element the single object to iterate
101      */

102     public WrapperIterator(Iterator it1, Iterator it2) {
103
104         this.it1 = it1;
105         this.it2 = it2;
106         chained = true;
107     }
108
109     /**
110      * Tests if this iterator contains more elements. <p>
111      *
112      * @return <code>true</code> if this iterator contains more elements;
113      * <code>false</code> otherwise.
114      */

115     public boolean hasNext() {
116
117         // for chained iterators
118
if (chained) {
119             if (it1 == null) {
120                 if (it2 == null) {
121                     return false;
122                 }
123
124                 if (it2.hasNext()) {
125                     return true;
126                 }
127
128                 it2 = null;
129
130                 return false;
131             } else {
132                 if (it1.hasNext()) {
133                     return true;
134                 }
135
136                 it1 = null;
137
138                 return hasNext();
139             }
140         }
141
142         // for other interators
143
if (elements == null) {
144             return false;
145         }
146
147         for (; notNull && i < elements.length && elements[i] == null; i++) {}
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 JavaDoc next() {
167
168         // for chained iterators
169
if (chained) {
170             if (it1 == null) {
171                 if (it2 == null) {
172                     throw new NoSuchElementException JavaDoc();
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 JavaDoc();
199     }
200
201     public int nextInt() {
202         throw new NoSuchElementException JavaDoc();
203     }
204
205     public long nextLong() {
206         throw new NoSuchElementException JavaDoc();
207     }
208
209     public void remove() {
210         throw new NoSuchElementException JavaDoc();
211     }
212 }
213
Popular Tags