1 5 package com.opensymphony.webwork.util; 6 7 import com.opensymphony.xwork.Action; 8 import org.apache.commons.logging.LogFactory; 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 13 14 20 public class SubsetIteratorFilter extends IteratorFilterSupport implements Iterator , Action { 21 23 Iterator iterator; 24 Object source; 25 int count = -1; 26 int currentCount = 0; 27 28 int start = 0; 30 31 33 public void setCount(int aCount) { 34 this.count = aCount; 35 } 36 37 public void setSource(Object anIterator) { 39 source = anIterator; 40 } 41 42 public void setStart(int aStart) { 43 this.start = aStart; 44 } 45 46 public String execute() { 48 if (source == null) { 49 LogFactory.getLog(SubsetIteratorFilter.class.getName()).warn("Source is null returning empty set."); 50 51 return ERROR; 52 } 53 54 source = getIterator(source); 56 57 if (source instanceof Iterator ) { 59 iterator = (Iterator ) source; 60 61 for (int i = 0; (i < start) && iterator.hasNext(); i++) { 63 iterator.next(); 64 } 65 } else if (source.getClass().isArray()) { 66 ArrayList list = new ArrayList (((Object []) source).length); 67 Object [] objects = (Object []) source; 68 int len = objects.length; 69 70 if (count != -1) { 71 len -= count; 72 } 73 74 for (int j = start; j < len; j++) { 75 list.add(objects[j]); 76 } 77 78 count = -1; iterator = list.iterator(); 80 } 81 82 if (iterator == null) { 83 throw new IllegalArgumentException ("Source is not an iterator:" + source); 84 } 85 86 return SUCCESS; 87 } 88 89 public boolean hasNext() { 91 return (iterator == null) ? false : (iterator.hasNext() && ((count == -1) || (currentCount < count))); 92 } 93 94 public Object next() { 95 currentCount++; 96 97 return iterator.next(); 98 } 99 100 public void remove() { 101 iterator.remove(); 102 } 103 } 104 | Popular Tags |