KickJava   Java API By Example, From Geeks To Geeks.

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


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

48
49 package org.ozoneDB.collections;
50
51 import java.io.Serializable JavaDoc;
52 import java.util.ConcurrentModificationException JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.NoSuchElementException JavaDoc;
55 import org.ozoneDB.OzoneObject;
56
57 /**
58  * <p>DO NOT USE THIS CLASS DIRECTLY.</p>
59  * <p>This should be an inner class; ozone unfortunately does not (yet) support
60  * inner classes as Ozone objects. Until it does we have to resort to this hack.</p>
61  * <p>Note that this class resembles <code>_BaseTreeMap_TreeIterator</code>.</p>
62  * <p>TODO: as soon as Ozone supports server side objects that are not storable, this
63  * class has to be rewritten to such a server side non-persistent object.</p>
64  */

65 public class _BaseTreeMap_OzoneTreeIteratorImpl extends OzoneObject implements OzoneIterator {
66
67     private static final long serialVersionUID = 1L;
68
69     private BaseTreeMap owner;
70
71     public _BaseTreeMap_OzoneTreeIteratorImpl(BaseTreeMap owner, int type) {
72         this.owner = owner;
73         knownMod = owner._org_ozoneDB_getModification();
74         this.type = type;
75         this.next = owner._org_ozoneDB_firstNode();
76         this.max = BaseTreeMapImpl.nilNode;
77         valid = true;
78     }
79
80     public _BaseTreeMap_OzoneTreeIteratorImpl(BaseTreeMap owner, int type, BaseTreeMap.Node first, BaseTreeMap.Node max) {
81         this.owner = owner;
82         knownMod = owner._org_ozoneDB_getModification();
83         this.type = type;
84         this.next = first;
85         this.max = max;
86         valid = true;
87     }
88
89     /**
90      * The type of this Iterator: {@link AbstractOzoneMap#KEYS}, {@link AbstractOzoneMap#VALUES},
91      * or {@link AbstractOzoneMap#ENTRIES}.
92      */

93     private final int type;
94     /** The number of modifications to the backing Map that we know about. */
95     private int knownMod;
96     /** The last Entry returned by a next() call. */
97     private BaseTreeMap.Node last;
98     /** The next entry that should be returned by next(). */
99     private BaseTreeMap.Node next;
100     /**
101      * The last node visible to this iterator. This is used when iterating
102      * on a SubMap.
103      */

104     private final BaseTreeMap.Node max;
105     
106     /**
107      * set by ctor; if not set it indicates that since this instance has
108      * been deserialized and its <code>last</code> and <code>next</code> are not
109      * longer the same nodes as their counterparts in the collection this
110      * iterator is connected to
111      */

112     private transient boolean valid = false;
113
114     /**
115      * Construct a new TreeIterator with the supplied type.
116      * @param type {@link AbstractOzoneMap#KEYS}, {@link AbstractOzoneMap#VALUES},
117      * or {@link AbstractOzoneMap#ENTRIES}
118      */

119     private _BaseTreeMap_OzoneTreeIteratorImpl(int type) {
120         // FIXME gcj cannot handle this. Bug java/4695
121
// this(type, firstNode(), nil);
122
this.type = type;
123         this.next = owner._org_ozoneDB_firstNode();
124         this.max = BaseTreeMapImpl.nilNode;
125         valid = true;
126     }
127
128     /**
129      * Construct a new TreeIterator with the supplied type. Iteration will
130      * be from "first" (inclusive) to "max" (exclusive).
131      *
132      * @param type {@link AbstractOzoneMap#KEYS}, {@link AbstractOzoneMap#VALUES},
133      * or {@link AbstractOzoneMap#ENTRIES}
134      * @param first where to start iteration, nil for empty iterator
135      * @param max the cutoff for iteration, nil for all remaining nodes
136      */

137     private _BaseTreeMap_OzoneTreeIteratorImpl(int type, BaseTreeMap.Node first, BaseTreeMap.Node max) {
138         this.type = type;
139         this.next = first;
140         this.max = max;
141         valid = true;
142     }
143
144     /**
145      * Returns true if the Iterator has more elements.
146      * @return true if there are more elements
147      * @throws ConcurrentModificationException if the TreeMap was modified
148      */

149     public boolean hasNext() {
150         if (!valid) {
151             throw new ConcurrentModificationException JavaDoc("iterator has been persisted and reloaded; see javadoc on FullTreeMapImpl");
152         }
153         if (knownMod != owner._org_ozoneDB_getModification())
154             throw new ConcurrentModificationException JavaDoc();
155         return !next.equals(max);
156     }
157
158     /**
159      * Returns the next element in the Iterator's sequential view.
160      * @return the next element
161      * @throws ConcurrentModificationException if the TreeMap was modified
162      * @throws NoSuchElementException if there is none
163      */

164     public Object JavaDoc next() {
165         if (!valid) {
166             throw new ConcurrentModificationException JavaDoc("iterator has been persisted and reloaded; see javadoc on FullTreeMapImpl");
167         }
168         if (knownMod != owner._org_ozoneDB_getModification())
169             throw new ConcurrentModificationException JavaDoc();
170         if (next.equals(max))
171             throw new NoSuchElementException JavaDoc();
172         last = next;
173         next = owner._org_ozoneDB_successor(last);
174
175         if (type == BaseTreeMapImpl.VALUES)
176             return last.getValue();
177         else if (type == BaseTreeMapImpl.KEYS)
178             return last.getKey();
179         return last;
180     }
181
182     /**
183      * Removes from the backing TreeMap the last element which was fetched
184      * with the <code>next()</code> method.
185      * @throws ConcurrentModificationException if the TreeMap was modified
186      * @throws IllegalStateException if called when there is no last element
187      */

188     public void remove() {
189         if (last == null) {
190             throw new IllegalStateException JavaDoc();
191         }
192         if (!valid) {
193             throw new ConcurrentModificationException JavaDoc("iterator has been persisted and reloaded; see javadoc on FullTreeMapImpl");
194         }
195         if (knownMod != owner._org_ozoneDB_getModification()) {
196             throw new ConcurrentModificationException JavaDoc();
197         }
198
199         owner._org_ozoneDB_removeNode(last);
200         last = null;
201         knownMod++;
202     }
203
204 }
Popular Tags