1 5 package com.opensymphony.webwork.util; 6 7 import com.opensymphony.xwork.Action; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 14 20 public class MergeIteratorFilter extends IteratorFilterSupport implements Iterator , Action { 21 23 List iterators = new ArrayList (); 24 25 List sources = new ArrayList (); 27 int idx = 0; 28 29 31 public void setSource(Object anIterator) { 33 sources.add(anIterator); 34 } 35 36 public String execute() { 38 for (int i = 0; i < sources.size(); i++) { 40 Object source = sources.get(i); 41 iterators.add(getIterator(source)); 42 } 43 44 return SUCCESS; 45 } 46 47 public boolean hasNext() { 49 while (iterators.size() > 0) { 50 if (((Iterator ) iterators.get(idx)).hasNext()) { 51 return true; 52 } else { 53 iterators.remove(idx); 54 55 if (iterators.size() > 0) { 56 idx = idx % iterators.size(); 57 } 58 } 59 } 60 61 return false; 62 } 63 64 public Object next() { 65 try { 66 return ((Iterator ) iterators.get(idx)).next(); 67 } finally { 68 idx = (idx + 1) % iterators.size(); 69 } 70 } 71 72 public void remove() { 73 throw new UnsupportedOperationException ("Remove is not supported in MergeIteratorFilter."); 74 } 75 } 76 | Popular Tags |