1 7 8 package org.dom4j.tree; 9 10 import java.util.Iterator ; 11 import java.util.NoSuchElementException ; 12 13 24 public abstract class FilterIterator implements Iterator { 25 protected Iterator proxy; 26 27 private Object next; 28 29 private boolean first = true; 30 31 public FilterIterator(Iterator proxy) { 32 this.proxy = proxy; 33 } 34 35 public boolean hasNext() { 36 if (first) { 37 next = findNext(); 38 first = false; 39 } 40 41 return next != null; 42 } 43 44 public Object next() throws NoSuchElementException { 45 if (!hasNext()) { 46 throw new NoSuchElementException (); 47 } 48 49 Object answer = this.next; 50 this.next = findNext(); 51 52 return answer; 53 } 54 55 62 public void remove() { 63 throw new UnsupportedOperationException (); 64 } 65 66 75 protected abstract boolean matches(Object element); 76 77 protected Object findNext() { 78 if (proxy != null) { 79 while (proxy.hasNext()) { 80 Object nextObject = proxy.next(); 81 82 if ((nextObject != null) && matches(nextObject)) { 83 return nextObject; 84 } 85 } 86 87 proxy = null; 88 } 89 90 return null; 91 } 92 } 93 94 130 | Popular Tags |