View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.ordi.iterator;
19  
20  import java.util.NoSuchElementException;
21  
22  /**
23   * This iterator is responsible to aggregate multiple data sets as unique.
24   * 
25   * @author vassil
26   */
27  public class AggregationIteratorImpl<E> implements CloseableIterator<E> {
28  
29      private CloseableIterator<E>[] iters;
30  
31      private boolean isClosed = false;
32  
33      private int currentIterator = 0;
34  
35      public AggregationIteratorImpl(CloseableIterator<E>... iters) {
36          if (iters == null) {
37              throw new IllegalArgumentException();
38          }
39          this.iters = iters;
40      }
41  
42      public void remove() {
43          iters[currentIterator].remove();
44      }
45  
46      public E next() {
47          if (isClosed) {
48              throw new NoSuchElementException("The iterator have been closed!");
49          }
50          return iters[currentIterator].next();
51      }
52  
53      public boolean hasNext() {
54          if (isClosed || currentIterator >= iters.length)
55              return false;
56          if (iters[currentIterator].hasNext() == false) {
57              currentIterator++;
58              return hasNext();
59          }
60          return true;
61      }
62  
63      public void close() {
64          for (int i = 0; i < iters.length; i++) {
65              iters[currentIterator].close();
66          }
67      }
68  }