1 9 package javolution.util; 10 11 import j2me.lang.IllegalStateException; 12 import j2me.util.Iterator; 13 import j2me.util.NoSuchElementException; 14 import javolution.context.RealtimeObject; 15 import javolution.util.FastCollection.Record; 16 17 28 final class FastIterator extends RealtimeObject implements Iterator { 29 30 private static final Factory FACTORY = new Factory() { 31 protected Object create() { 32 return new FastIterator(); 33 } 34 35 protected void cleanup(Object obj) { 36 FastIterator iterator = (FastIterator) obj; 37 iterator._collection = null; 38 iterator._current = null; 39 iterator._next = null; 40 iterator._tail = null; 41 } 42 }; 43 44 private FastCollection _collection; 45 46 private Record _current; 47 48 private Record _next; 49 50 private Record _tail; 51 52 public static FastIterator valueOf(FastCollection collection) { 53 FastIterator iterator = (FastIterator) FastIterator.FACTORY.object(); 54 iterator._collection = collection; 55 iterator._next = collection.head().getNext(); 56 iterator._tail = collection.tail(); 57 return iterator; 58 } 59 60 private FastIterator() { 61 } 62 63 public boolean hasNext() { 64 return (_next != _tail); 65 } 66 67 public Object next() { 68 if (_next == _tail) 69 throw new NoSuchElementException(); 70 _current = _next; 71 _next = _next.getNext(); 72 return _collection.valueOf(_current); 73 } 74 75 public void remove() { 76 if (_current != null) { 77 final Record previous = _current.getPrevious(); 80 _collection.delete(_current); 81 _current = null; 82 _next = previous.getNext(); 83 } else { 84 throw new IllegalStateException (); 85 } 86 } 87 } | Popular Tags |