View Javadoc

1   /* CompositeIterator
2   *
3   * $Id: CompositeIterator.java 4644 2006-09-20 22:40:21Z paul_jack $
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;
28  import java.util.Iterator;
29  import java.util.NoSuchElementException;
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 {
36      ArrayList<Iterator> iterators = new ArrayList<Iterator>();
37      Iterator 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) 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 next() {
73          if(hasNext()) {
74              return currentIterator.next();
75          } else {
76              throw new NoSuchElementException();
77          }
78      }
79  
80      /* (non-Javadoc)
81       * @see java.util.Iterator#remove()
82       */
83      public void remove() {
84          throw new UnsupportedOperationException();
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 i1, Iterator 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 i) {
113         iterators.add(i);
114     }
115 
116 }