KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > util > FastIterator


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

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 /**
18  * <p> This class represents an iterator over a {@link Fastcollection).
19  * Iterations are thread-safe if the collections records are not removed
20  * or inserted at arbitrary position (appending/prepending is fine).</p>
21  *
22  * <p> Iterators are allocated on the stack when executing in a
23  * {@link PoolContext javolution.context.PoolContext}.</p>
24  *
25  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
26  * @version 3.7, March 17, 2005
27  */

28 final class FastIterator extends RealtimeObject implements Iterator {
29
30     private static final Factory FACTORY = new Factory() {
31         protected Object JavaDoc create() {
32             return new FastIterator();
33         }
34
35         protected void cleanup(Object JavaDoc 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 JavaDoc 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             // Uses the previous record (not affected by the remove)
78
// to set the next record.
79
final Record previous = _current.getPrevious();
80             _collection.delete(_current);
81             _current = null;
82             _next = previous.getNext();
83         } else {
84             throw new IllegalStateException JavaDoc();
85         }
86     }
87 }
Popular Tags