1 10 package com.hp.hpl.jena.util.iterator; 11 12 import java.util.*; 13 14 23 public class UniqueExtendedIterator extends WrappedIterator { 24 25 26 protected HashSet seen = new HashSet(); 27 28 29 protected Object next = null; 30 31 36 public UniqueExtendedIterator(Iterator underlying) { 37 super(underlying); 38 } 39 40 49 public static ExtendedIterator create( Iterator it ) { 50 return (it instanceof UniqueExtendedIterator) ? 51 ((UniqueExtendedIterator) it) : new UniqueExtendedIterator( it ); 52 } 53 54 60 protected Object nextIfNew() { 61 Object value = super.next(); 62 return seen.add( value ) ? value : null; 63 } 64 65 68 public boolean hasNext() { 69 while (next == null && super.hasNext()) next = nextIfNew(); 70 return next != null; 71 } 72 73 76 public Object next() { 77 ensureHasNext(); 78 Object result = next; 79 next = null; 80 return result; 81 } 82 } 83 84 110 111 | Popular Tags |