KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > collections > _BaseList_ListIteratorImpl


1 /*
2  * $Id: _BaseTreeMap_TreeIteratorImpl.java,v 1.5 2003/02/16 20:27:31 leomekenkamp Exp $
3  * This file is based on AbstractList.java from GNU Classpath. Quote:
4
5 AbstractList.java -- Abstract implementation of most of List
6 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
7
8 This file is part of GNU Classpath.
9
10 GNU Classpath is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU Classpath is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Classpath; see the file COPYING. If not, write to the
22 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307 USA.
24
25 Linking this library statically or dynamically with other modules is
26 making a combined work based on this library. Thus, the terms and
27 conditions of the GNU General Public License cover the whole
28 combination.
29
30 As a special exception, the copyright holders of this library give you
31 permission to link this library with independent modules to produce an
32 executable, regardless of the license terms of these independent
33 modules, and to copy and distribute the resulting executable under
34 terms of your choice, provided that you also meet, for each linked
35 independent module, the terms and conditions of the license of that
36 module. An independent module is a module which is not derived from
37 or based on this library. If you modify this library, you may extend
38 this exception to your version of the library, but you are not
39 obligated to do so. If you do not wish to do so, delete this
40 exception statement from your version.
41
42  * end quote.
43  *
44  * This file is licenced under the same conditions as its original (GPL +
45  * "special exception").
46  */

47
48 package org.ozoneDB.collections;
49
50 import java.util.ConcurrentModificationException JavaDoc;
51 import java.util.NoSuchElementException JavaDoc;
52 import org.ozoneDB.OzoneObject;
53
54 /**
55  *
56  * @author leo
57  */

58 public class _BaseList_ListIteratorImpl extends OzoneObject implements OzoneListIterator {
59
60     private BaseList owner;
61     private int knownMod;
62     private int position;
63     private int lastReturned = -1;
64     private int size;
65
66  
67     public _BaseList_ListIteratorImpl(BaseList owner, int index) {
68         this.owner = owner;
69         knownMod = owner._org_ozoneDB_getModCount();
70         position = index;
71         size = owner.size();
72     }
73     
74     // This will get inlined, since it is private.
75
private void checkMod() {
76         if (knownMod != owner._org_ozoneDB_getModCount()) {
77             throw new ConcurrentModificationException JavaDoc();
78         }
79     }
80
81     public boolean hasNext() {
82         checkMod();
83         return position < size;
84     }
85
86     public boolean hasPrevious() {
87         checkMod();
88         return position > 0;
89     }
90
91     public Object JavaDoc next() {
92         checkMod();
93         if (position == size) {
94             throw new NoSuchElementException JavaDoc();
95         }
96         lastReturned = position;
97         return owner.get(position++);
98     }
99
100     public Object JavaDoc previous() {
101         checkMod();
102         if (position == 0) {
103             throw new NoSuchElementException JavaDoc();
104         }
105         lastReturned = --position;
106         return owner.get(lastReturned);
107     }
108
109     public int nextIndex() {
110         checkMod();
111         return position;
112     }
113
114     public int previousIndex() {
115         checkMod();
116         return position - 1;
117     }
118
119     public void remove() {
120         checkMod();
121         if (lastReturned < 0)
122             throw new IllegalStateException JavaDoc();
123         owner.remove(lastReturned);
124         size--;
125         position = lastReturned;
126         lastReturned = -1;
127         knownMod = owner._org_ozoneDB_getModCount();
128     }
129
130     public void set(Object JavaDoc o) {
131         checkMod();
132         if (lastReturned < 0) {
133             throw new IllegalStateException JavaDoc();
134         }
135         owner.set(lastReturned, o);
136     }
137
138     public void add(Object JavaDoc o) {
139         checkMod();
140         owner.add(position++, o);
141         size++;
142         lastReturned = -1;
143         knownMod = owner._org_ozoneDB_getModCount();
144     }
145     
146 }
Popular Tags