1 6 7 package com.hp.hpl.jena.util.iterator; 8 9 import java.util.*; 10 11 18 19 public class NiceIterator implements ExtendedIterator 20 { 21 public NiceIterator() 22 { super(); } 23 24 27 public void close() 28 { } 29 30 33 public boolean hasNext() 34 { return false; } 35 36 protected void ensureHasNext() 37 { if (hasNext() == false) throw new NoSuchElementException(); } 38 39 42 public Object next() 43 { return noElements( "empty NiceIterator" ); } 44 45 53 protected Object noElements( String message ) 54 { throw new NoSuchElementException( message ); } 55 56 59 public void remove() 60 { 61 throw new UnsupportedOperationException ( "remove not supported for this iterator" ); 62 } 63 64 67 public Object removeNext() 68 { Object result = next(); remove(); return result; } 69 70 73 74 public static ExtendedIterator andThen( final Iterator a, final Iterator b ) 75 { 76 return new NiceIterator() 77 { 78 private boolean walkingA = true; 79 80 public boolean hasNext() 81 { return (walkingA = a.hasNext()) || b.hasNext(); } 82 83 public Object next() 84 { return (walkingA = a.hasNext()) ? a.next() : b.next(); } 85 86 public void close() 87 { 88 close( a ); 89 close( b ); 90 } 91 92 public void remove() 93 { (walkingA ? a : b).remove(); } 94 }; 95 } 96 97 100 public ExtendedIterator andThen( ClosableIterator other ) 101 { return andThen( this, other ); } 102 103 106 public ExtendedIterator filterKeep( Filter f ) 107 { return new FilterIterator( f, this ); } 108 109 112 public ExtendedIterator filterDrop( final Filter f ) 113 { 114 Filter notF = new Filter() { public boolean accept( Object x ) { return !f.accept( x ); } }; 115 return new FilterIterator( notF, this ); 116 } 117 118 121 public ExtendedIterator mapWith( Map1 map1 ) 122 { return new Map1Iterator( map1, this ); } 123 124 128 public static void close( Iterator it ) 129 { if (it instanceof ClosableIterator) ((ClosableIterator) it).close(); } 130 131 static final private NiceIterator emptyInstance = new NiceIterator(); 132 133 137 static public ExtendedIterator emptyIterator() { 138 return emptyInstance; 139 } 140 141 } 142 143 172 | Popular Tags |