KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > iterator > CompositeIterator


1 /* CompositeIterator
2 *
3 * $Id: CompositeIterator.java,v 1.1.18.1 2007/01/13 01:31:40 stack-sf Exp $
4 *
5 * Created on Mar 3, 2004
6 *
7 * Copyright (C) 2004 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */

25 package org.archive.util.iterator;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 /**
32  * An iterator that's built up out of any number of other iterators.
33  * @author gojomo
34  */

35 public class CompositeIterator implements Iterator JavaDoc {
36     ArrayList JavaDoc<Iterator JavaDoc> iterators = new ArrayList JavaDoc<Iterator JavaDoc>();
37     Iterator JavaDoc currentIterator;
38     int indexOfCurrentIterator = -1;
39
40     /**
41      * Moves to the next (non empty) iterator. Returns false if there are no
42      * more (non empty) iterators, true otherwise.
43      * @return false if there are no more (non empty) iterators, true otherwise.
44      */

45     private boolean nextIterator() {
46         if (++indexOfCurrentIterator < iterators.size()) {
47             currentIterator = (Iterator JavaDoc) iterators.get(indexOfCurrentIterator);
48             // If the new iterator was empty this will move us to the next one.
49
return hasNext();
50         } else {
51             currentIterator = null;
52             return false;
53         }
54     }
55
56     /* (non-Javadoc)
57      * @see java.util.Iterator#hasNext()
58      */

59     public boolean hasNext() {
60         if(currentIterator!=null && currentIterator.hasNext()) {
61             // Got more
62
return true;
63         } else {
64             // Have got more if we can queue up a new iterator.
65
return nextIterator();
66         }
67     }
68
69     /* (non-Javadoc)
70      * @see java.util.Iterator#next()
71      */

72     public Object JavaDoc next() {
73         if(hasNext()) {
74             return currentIterator.next();
75         } else {
76             throw new NoSuchElementException JavaDoc();
77         }
78     }
79
80     /* (non-Javadoc)
81      * @see java.util.Iterator#remove()
82      */

83     public void remove() {
84         throw new UnsupportedOperationException JavaDoc();
85     }
86
87     /**
88      * Create an empty CompositeIterator. Internal
89      * iterators may be added later.
90      */

91     public CompositeIterator() {
92         super();
93     }
94
95     /**
96      * Convenience method for concatenating together
97      * two iterators.
98      * @param i1
99      * @param i2
100      */

101     public CompositeIterator(Iterator JavaDoc i1, Iterator JavaDoc i2) {
102         this();
103         add(i1);
104         add(i2);
105     }
106
107     /**
108      * Add an iterator to the internal chain.
109      *
110      * @param i an iterator to add.
111      */

112     public void add(Iterator JavaDoc i) {
113         iterators.add(i);
114     }
115
116 }
117
Popular Tags