Source code: org/jdaemon/util/iterator/CompoundIterator.java
1 /*
2 * CompoundIterator.java
3 *
4 * Copyright (C) 2002 Jonathan Essex
5 * This program is distributed under the terms of the Lesser GNU General Public
6 * License (v2 or later) per the included COPYING.txt file, or see www.fsf.org.
7 *
8 * Created on 26 April 2002, 18:38
9 */
10
11 package org.jdaemon.util.iterator;
12
13 import java.util.Iterator;
14
15 /** Takes two iterators and turns them into a single iterator
16 *
17 * Elements in the enumeration A will be returned until there are none
18 * left; After that, elements in the enumeration B will be returned.
19 *
20 * @author Jonathan
21 * @version
22 */
23 public class CompoundIterator implements java.util.Iterator {
24
25 /** Iterator A */
26 private Iterator a;
27 /** Iterator B */
28 private Iterator b;
29
30 /** Creates new CompoundIterator
31 *
32 * @param a Iterator A
33 * @param b Iterator B
34 */
35 public CompoundIterator(Iterator a, Iterator b) {
36 this.a = a;
37 this.b = b;
38 }
39
40 /** Test for more elements
41 *
42 * true if more elements exist in either iterator A or B
43 */
44 public boolean hasNext() {
45 return (a.hasNext() || b.hasNext());
46 }
47
48 /* Get next element
49 *
50 * Returns the next element in enumeration A if more elements exist
51 * in iterator A; Otherwise returns the next element in iterator B
52 */
53 public java.lang.Object next() {
54 return a.hasNext() ? a.next() : b.next();
55 }
56
57 public void remove() {
58 throw new java.lang.UnsupportedOperationException("CompoundIterator does not support remove()");
59 }
60 }