2669 |
27 Sep 06 |
nicklas |
1 |
/* |
2669 |
27 Sep 06 |
nicklas |
$Id$ |
2669 |
27 Sep 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2669 |
27 Sep 06 |
nicklas |
5 |
|
2669 |
27 Sep 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2669 |
27 Sep 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2669 |
27 Sep 06 |
nicklas |
8 |
|
2669 |
27 Sep 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2669 |
27 Sep 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2669 |
27 Sep 06 |
nicklas |
of the License, or (at your option) any later version. |
2669 |
27 Sep 06 |
nicklas |
13 |
|
2669 |
27 Sep 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2669 |
27 Sep 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2669 |
27 Sep 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2669 |
27 Sep 06 |
nicklas |
GNU General Public License for more details. |
2669 |
27 Sep 06 |
nicklas |
18 |
|
2669 |
27 Sep 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2669 |
27 Sep 06 |
nicklas |
21 |
*/ |
2669 |
27 Sep 06 |
nicklas |
22 |
package net.sf.basedb.util; |
2669 |
27 Sep 06 |
nicklas |
23 |
|
2669 |
27 Sep 06 |
nicklas |
24 |
import java.util.Collection; |
2669 |
27 Sep 06 |
nicklas |
25 |
import java.util.Iterator; |
2669 |
27 Sep 06 |
nicklas |
26 |
import java.util.NoSuchElementException; |
2669 |
27 Sep 06 |
nicklas |
27 |
|
2669 |
27 Sep 06 |
nicklas |
28 |
/** |
2669 |
27 Sep 06 |
nicklas |
This class implements the {@link Iterator} interface |
2698 |
04 Oct 06 |
nicklas |
for iterating multiple collections as if it was one big collection. |
2669 |
27 Sep 06 |
nicklas |
31 |
|
2669 |
27 Sep 06 |
nicklas |
@author Nicklas |
2669 |
27 Sep 06 |
nicklas |
@version 2.0 |
2698 |
04 Oct 06 |
nicklas |
@base.modified $Date$ |
2669 |
27 Sep 06 |
nicklas |
35 |
*/ |
2669 |
27 Sep 06 |
nicklas |
36 |
public class NestedIterator<E> |
2669 |
27 Sep 06 |
nicklas |
37 |
implements Iterator<E> |
2669 |
27 Sep 06 |
nicklas |
38 |
{ |
2669 |
27 Sep 06 |
nicklas |
39 |
/** |
2669 |
27 Sep 06 |
nicklas |
The array to iterate. |
2669 |
27 Sep 06 |
nicklas |
41 |
*/ |
2669 |
27 Sep 06 |
nicklas |
42 |
private final Collection<? extends E>[] collections; |
2669 |
27 Sep 06 |
nicklas |
43 |
|
2669 |
27 Sep 06 |
nicklas |
44 |
/** |
2669 |
27 Sep 06 |
nicklas |
The index of the next collection to return. |
2669 |
27 Sep 06 |
nicklas |
46 |
*/ |
2669 |
27 Sep 06 |
nicklas |
47 |
private int index; |
2669 |
27 Sep 06 |
nicklas |
48 |
|
2669 |
27 Sep 06 |
nicklas |
49 |
/** |
2669 |
27 Sep 06 |
nicklas |
The current iterator. |
2669 |
27 Sep 06 |
nicklas |
51 |
*/ |
2669 |
27 Sep 06 |
nicklas |
52 |
private Iterator<? extends E> current; |
2669 |
27 Sep 06 |
nicklas |
53 |
|
2669 |
27 Sep 06 |
nicklas |
54 |
/** |
2669 |
27 Sep 06 |
nicklas |
Create a new <code>ArrayIterator</code> object. |
4040 |
05 Dec 07 |
martin |
@param collections An array of Collection objects. |
2669 |
27 Sep 06 |
nicklas |
57 |
*/ |
6125 |
14 Sep 12 |
nicklas |
58 |
@SafeVarargs |
2669 |
27 Sep 06 |
nicklas |
59 |
public NestedIterator(Collection<? extends E>... collections) |
2669 |
27 Sep 06 |
nicklas |
60 |
{ |
2669 |
27 Sep 06 |
nicklas |
61 |
this.collections = collections; |
2669 |
27 Sep 06 |
nicklas |
62 |
this.current = null; |
2669 |
27 Sep 06 |
nicklas |
63 |
this.index = 0; |
2669 |
27 Sep 06 |
nicklas |
64 |
} |
6127 |
14 Sep 12 |
nicklas |
65 |
@Override |
2669 |
27 Sep 06 |
nicklas |
66 |
public boolean hasNext() |
2669 |
27 Sep 06 |
nicklas |
67 |
{ |
2669 |
27 Sep 06 |
nicklas |
68 |
if (collections == null) return false; |
2669 |
27 Sep 06 |
nicklas |
69 |
boolean next = false; |
2698 |
04 Oct 06 |
nicklas |
70 |
boolean atEnd = false; |
2698 |
04 Oct 06 |
nicklas |
71 |
while (!next && !atEnd) |
2669 |
27 Sep 06 |
nicklas |
72 |
{ |
2669 |
27 Sep 06 |
nicklas |
73 |
if (current == null) |
2669 |
27 Sep 06 |
nicklas |
74 |
{ |
2698 |
04 Oct 06 |
nicklas |
75 |
if (index < collections.length) |
2698 |
04 Oct 06 |
nicklas |
76 |
{ |
2698 |
04 Oct 06 |
nicklas |
77 |
Collection<? extends E> c = collections[index]; |
2698 |
04 Oct 06 |
nicklas |
78 |
if (c != null) current = c.iterator(); |
2698 |
04 Oct 06 |
nicklas |
79 |
index++; |
2698 |
04 Oct 06 |
nicklas |
80 |
} |
2698 |
04 Oct 06 |
nicklas |
81 |
else |
2698 |
04 Oct 06 |
nicklas |
82 |
{ |
2698 |
04 Oct 06 |
nicklas |
83 |
atEnd = true; |
2698 |
04 Oct 06 |
nicklas |
84 |
} |
2669 |
27 Sep 06 |
nicklas |
85 |
} |
2669 |
27 Sep 06 |
nicklas |
86 |
if (current != null) |
2669 |
27 Sep 06 |
nicklas |
87 |
{ |
2669 |
27 Sep 06 |
nicklas |
88 |
next = current.hasNext(); |
2669 |
27 Sep 06 |
nicklas |
89 |
if (!next) current = null; |
2669 |
27 Sep 06 |
nicklas |
90 |
} |
2669 |
27 Sep 06 |
nicklas |
91 |
} |
2669 |
27 Sep 06 |
nicklas |
92 |
return next; |
2669 |
27 Sep 06 |
nicklas |
93 |
} |
6127 |
14 Sep 12 |
nicklas |
94 |
@Override |
2669 |
27 Sep 06 |
nicklas |
95 |
public E next() |
2669 |
27 Sep 06 |
nicklas |
96 |
{ |
2669 |
27 Sep 06 |
nicklas |
97 |
if (!hasNext()) throw new NoSuchElementException(); |
2669 |
27 Sep 06 |
nicklas |
98 |
return current.next(); |
2669 |
27 Sep 06 |
nicklas |
99 |
} |
2669 |
27 Sep 06 |
nicklas |
100 |
/** |
2669 |
27 Sep 06 |
nicklas |
This operation is not supported. |
2669 |
27 Sep 06 |
nicklas |
@throws UnsupportedOperationException Is always thrown |
2669 |
27 Sep 06 |
nicklas |
103 |
*/ |
6127 |
14 Sep 12 |
nicklas |
104 |
@Override |
2669 |
27 Sep 06 |
nicklas |
105 |
public void remove() |
2669 |
27 Sep 06 |
nicklas |
106 |
{ |
2669 |
27 Sep 06 |
nicklas |
107 |
throw new UnsupportedOperationException(); |
2669 |
27 Sep 06 |
nicklas |
108 |
} |
2669 |
27 Sep 06 |
nicklas |
109 |
} |