Fix the thread safe problem
[IRC.git] / Robust / src / ClassLibrary / MGC / gnu / ArrayListIterator.java
1 public class ArrayListIterator implements Iterator {
2   private int pos;
3   private int size;
4   private int last;
5   private ArrayList list;
6
7   public ArrayListIterator(ArrayList list) {
8     this.list = list;
9     this.pos = 0;
10     this.size = this.list.size();
11     this.last = -1;
12   }
13
14   /**
15    * Tests to see if there are any more objects to
16    * return.
17    *
18    * @return True if the end of the list has not yet been
19    *         reached.
20    */
21   public boolean hasNext()
22   {
23     return pos < size;
24   }
25
26   /**
27    * Retrieves the next object from the list.
28    *
29    * @return The next object.
30    * @throws NoSuchElementException if there are
31    *         no more objects to retrieve.
32    * @throws ConcurrentModificationException if the
33    *         list has been modified elsewhere.
34    */
35   public Object next()
36   {
37     if (pos == size)
38       throw new /*NoSuchElement*/Exception("NoSuchElementException");
39     last = pos;
40     return this.list.get(pos++);
41   }
42
43   /**
44    * Removes the last object retrieved by <code>next()</code>
45    * from the list, if the list supports object removal.
46    *
47    * @throws ConcurrentModificationException if the list
48    *         has been modified elsewhere.
49    * @throws IllegalStateException if the iterator is positioned
50    *         before the start of the list or the last object has already
51    *         been removed.
52    * @throws UnsupportedOperationException if the list does
53    *         not support removing elements.
54    */
55   public void remove()
56   {
57     if (last < 0)
58       throw new /*IllegalState*/Exception("IllegalStateException");
59     this.list.remove(last);
60     pos--;
61     size--;
62     last = -1;
63   }
64 }