1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.util;
18
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.NoSuchElementException;
23
24 /***
25 * This Iterator iterates over a collection of iterators.
26 * @author Nabib
27 * @param <T>
28 */
29 public class AggregateIterator < T > implements Iterator < T > {
30
31 /***
32 * Collection of removed elements.
33 */
34 private final Collection < ? > removeColl;
35
36 /***
37 * Iterators of iterators.
38 */
39 private final Iterator < Iterator < T > > iterators;
40
41 /***
42 * Current iterator position.
43 */
44 private Iterator < T > currentIterator;
45
46 /***
47 * Next element position.
48 */
49 private T next;
50
51 /***
52 * Current element position.
53 */
54 private T current;
55
56 /***
57 * @param collRemove
58 * collection of removed entries to check against
59 * @param listIterators
60 * collection of iterators
61 */
62 public AggregateIterator(final Collection < ? > collRemove,
63 final List < Iterator < T > > listIterators) {
64 this.removeColl = collRemove;
65 this.iterators = listIterators.iterator();
66 while (this.iterators.hasNext()) {
67 this.currentIterator = getNextIterator();
68 while (this.currentIterator.hasNext()) {
69 next = this.currentIterator.next();
70 if (!removeColl.contains(next)) {
71 return;
72 }
73 }
74 }
75 next = null;
76 }
77
78 /***
79 * {@inheritDoc}
80 */
81 public final boolean hasNext() {
82 return next != null;
83 }
84
85 /***
86 * {@inheritDoc}
87 */
88 public final T next() {
89 if (next == null) {
90 throw new NoSuchElementException();
91 } else {
92 T returnNext = next;
93 current = returnNext;
94 next = null;
95 if (this.currentIterator == null) {
96 throw new NoSuchElementException();
97 }
98
99 while (this.currentIterator.hasNext()) {
100
101 T nextCandidate = this.currentIterator.next();
102 if (removeColl.contains(nextCandidate)) {
103 continue;
104 } else {
105 next = nextCandidate;
106 return returnNext;
107 }
108 }
109 while (this.iterators.hasNext()) {
110 this.currentIterator = this.iterators.next();
111 while (this.currentIterator.hasNext()) {
112
113 T nextCandidate = this.currentIterator.next();
114 if (removeColl.contains(nextCandidate)) {
115 continue;
116 } else {
117 next = nextCandidate;
118 return returnNext;
119 }
120 }
121 }
122 return returnNext;
123 }
124 }
125
126 /***
127 * {@inheritDoc}
128 */
129 public final void remove() {
130 if (current == null) {
131 throw new IllegalStateException();
132 }
133 this.removeColl.remove(current);
134 current = null;
135 }
136
137 /***
138 * Get next Iterator.
139 * @return Iterator
140 */
141 private Iterator < T > getNextIterator() {
142 return iterators.next();
143 }
144
145
146 }