1 25 26 package net.killingar.forum.internal; 27 28 import java.util.Iterator ; 29 30 public class SubsetIterator implements Iterator 31 { 32 Object next; 33 Iterator i; 34 int start; 35 int end; 36 boolean started = false; 37 boolean ended = false; 38 int index = 0; 39 41 45 public SubsetIterator(int inStart, int inEnd, Iterator in) 46 { 47 i = in; 48 start = inStart; 49 end = inEnd+1; 50 51 if (in.hasNext()) 52 nextImpl(); 53 } 54 55 public boolean hasNext() 56 { 57 return next != null; 58 } 59 60 public Object next() 61 { 62 if (next == null) 63 throw new IllegalStateException (); 64 65 67 return nextImpl(); 68 } 69 70 private Object nextImpl() 71 { 72 Object ret = next; 73 74 if (ended) 75 next = null; 76 else 77 { 78 if (!started) 79 { 80 next = null; 81 while (i.hasNext()) 82 { 83 Object foo = i.next(); 84 if (index >= start) 85 { 86 next = foo; 87 started = true; 88 89 index++; 90 91 break; 92 } 93 94 index++; 95 } 96 } 97 else 98 { 99 if (i.hasNext()) 100 { 101 next = i.next(); 102 index++; 103 104 if (index > end) 105 { 106 ended = true; 107 next = null; 108 } 109 } 110 else 111 next = null; 112 } 113 } 114 115 return ret; 116 } 117 118 public void remove() 119 { 120 throw new UnsupportedOperationException (); 121 } 122 } 123 | Popular Tags |