OSDN Git Service

change internal process method, which introducing ExtractionResultSet
[stigmata/stigmata.git] / src / main / java / jp / naist / se / stigmata / utils / MultipleIterator.java
1 package jp.naist.se.stigmata.utils;
2
3 /* 
4  * $Id$
5  */
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.List;
10 import java.util.Iterator;
11 import java.util.NoSuchElementException;
12
13 /**
14  * 
15  * @author Haruaki Tamada
16  * @version $Revision$ $Date$
17  */
18 public class MultipleIterator<T> implements Iterator<T>{
19     private List<Iterator<T>> iterators = new ArrayList<Iterator<T>>();
20     private int index = 0;
21     private Iterator<T> current;
22     private boolean finished = false;
23
24     public MultipleIterator(){
25     }
26
27     public MultipleIterator(Iterator<T>[] iteratorArray){
28         for(Iterator<T> iterator: iteratorArray){
29             iterators.add(iterator);
30         }
31     }
32
33     public MultipleIterator(Iterator<Iterator<T>> iterator){
34         while(iterator.hasNext()){
35             iterators.add(iterator.next());
36         }
37     }
38
39     public MultipleIterator(Collection<Iterator<T>> collection){
40         this(collection.iterator());
41     }
42
43     public void add(Iterator<T> iterator){
44         iterators.add(iterator);
45     }
46
47     public void remove(Iterator<T> iterator){
48         iterators.remove(iterator);
49     }
50
51     private void nextIterator(){
52         if(index >= iterators.size()){
53             current = null;
54             finished = true;
55             return;
56         }
57         current = iterators.get(index);
58         index++;
59     }
60
61     public void remove(){
62         current.remove();
63     }
64
65     /**
66      */
67     public boolean hasNext(){
68         if(current == null){
69             nextIterator();
70         }
71         boolean flag = current.hasNext();
72
73         if(!flag){
74             nextIterator();
75         }
76
77         return finished || current.hasNext();
78     }
79
80     /** 
81      */
82     public T next(){
83         if(hasNext()){
84             return current.next();
85         }
86
87         throw new NoSuchElementException();
88     }
89 }