1 19 package org.netbeans.modules.xml.catalog.lib; 20 21 import java.util.*; 22 23 29 public final class FilterIterator implements Iterator { 30 31 private final Iterator peer; 32 private final Filter filter; 33 34 37 private Object next; 38 39 public FilterIterator(Iterator it, Filter filter) { 40 if (it == null || filter == null) 41 throw new IllegalArgumentException ("null not allowed"); peer = it; 43 this.filter = filter; 44 } 45 46 public void remove() { 47 throw new UnsupportedOperationException (); 48 } 49 50 53 public Object next() { 54 if (hasNext()) { 55 Object ret = next; 56 next = null; 57 return ret; 58 } else { 59 throw new NoSuchElementException(); 60 } 61 } 62 63 66 public boolean hasNext() { 67 if (next != null) return true; 68 69 while (peer.hasNext()) { 70 next = peer.next(); 71 if (filter.accept(next)) return true; 72 } 73 next = null; 74 return false; 75 } 76 77 public static interface Filter { 78 public boolean accept(Object obj); 79 } 80 } 81 | Popular Tags |