OSDN Git Service

bug fixed: invalid finish routine. The routine is copied from MultipleIterator of...
authortama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Wed, 10 Oct 2007 05:26:40 +0000 (05:26 +0000)
committertama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Wed, 10 Oct 2007 05:26:40 +0000 (05:26 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/stigmata/trunk@246 acee48c3-7b26-0410-bdac-b3d0e5314bbc

src/main/java/jp/naist/se/stigmata/utils/MultipleIterator.java

index adf8cd2..73d9600 100755 (executable)
@@ -1,13 +1,13 @@
 package jp.naist.se.stigmata.utils;
 
-/* 
+/*
  * $Id$
  */
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
 import java.util.Iterator;
+import java.util.List;
 import java.util.NoSuchElementException;
 
 /**
@@ -18,16 +18,12 @@ import java.util.NoSuchElementException;
 public class MultipleIterator<T> implements Iterator<T>{
     private List<Iterator<T>> iterators = new ArrayList<Iterator<T>>();
     private int index = 0;
-    private Iterator<T> current;
-    private boolean finished = false;
 
     public MultipleIterator(){
     }
 
-    public MultipleIterator(Iterator<T>[] iteratorArray){
-        for(Iterator<T> iterator: iteratorArray){
-            iterators.add(iterator);
-        }
+    public MultipleIterator(Collection<Iterator<T>> collection){
+        this(collection.iterator());
     }
 
     public MultipleIterator(Iterator<Iterator<T>> iterator){
@@ -36,54 +32,49 @@ public class MultipleIterator<T> implements Iterator<T>{
         }
     }
 
-    public MultipleIterator(Collection<Iterator<T>> collection){
-        this(collection.iterator());
+    public MultipleIterator(Iterator<T>[] iteratorArray){
+        for(Iterator<T> iterator: iteratorArray){
+            iterators.add(iterator);
+        }
     }
 
     public void add(Iterator<T> iterator){
         iterators.add(iterator);
     }
 
-    public void remove(Iterator<T> iterator){
-        iterators.remove(iterator);
-    }
-
-    private void nextIterator(){
-        if(index >= iterators.size()){
-            current = null;
-            finished = true;
-            return;
-        }
-        current = iterators.get(index);
-        index++;
-    }
-
-    public void remove(){
-        current.remove();
-    }
-
-    /**
-     */
     public boolean hasNext(){
-        if(current == null){
-            nextIterator();
-        }
-        boolean flag = current.hasNext();
-
-        if(!flag){
-            nextIterator();
+        while(index < iterators.size()){
+            boolean next = iterators.get(index).hasNext();
+            if(next){
+                return true;
+            }
+            index++;
         }
-
-        return finished || current.hasNext();
+        return false;
     }
 
     /** 
      */
     public T next(){
-        if(hasNext()){
-            return current.next();
+        if(!iterators.get(index).hasNext()){
+            index++;
+            while(index < iterators.size()){
+                if(iterators.get(index).hasNext()){
+                    return iterators.get(index).next();
+                }
+                index++;
+            }
+            throw new NoSuchElementException();
         }
 
-        throw new NoSuchElementException();
+        return iterators.get(index).next();
+    }
+
+    public void remove(){
+        iterators.get(index).remove();
+    }
+
+    public void remove(Iterator<T> iterator){
+        iterators.remove(iterator);
     }
 }