KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: _BaseTreeMap_TreeIterator.java,v 1.2 2003/11/05 14:58:43 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
56 /**
57  * <p>DO NOT USE THIS CLASS DIRECTLY.</p>
58  * <p>This should be an inner class; ozone unfortunately does not (yet) support
59  * inner classes as Ozone objects. Until it does we have to resort to this hack.</p>
60  * <p>Note that this class resembles <code>_BaseTreeMap_OzoneTreeIteratorImpl</code>.</p>
61  */

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

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

99     private final BaseTreeMap.Node max;
100
101     /**
102      * Construct a new TreeIterator with the supplied type.
103      * @param type {@link AbstractOzoneMap#KEYS}, {@link AbstractOzoneMap#VALUES},
104      * or {@link AbstractOzoneMap#ENTRIES}
105      */

106     private _BaseTreeMap_TreeIterator(int type) {
107         // FIXME gcj cannot handle this. Bug java/4695
108
// this(type, firstNode(), nil);
109
this.type = type;
110         this.next = owner._org_ozoneDB_firstNode();
111         this.max = BaseTreeMapImpl.nilNode;
112     }
113
114     /**
115      * Construct a new TreeIterator with the supplied type. Iteration will
116      * be from "first" (inclusive) to "max" (exclusive).
117      *
118      * @param type {@link AbstractOzoneMap#KEYS}, {@link AbstractOzoneMap#VALUES},
119      * or {@link AbstractOzoneMap#ENTRIES}
120      * @param first where to start iteration, nil for empty iterator
121      * @param max the cutoff for iteration, nil for all remaining nodes
122      */

123     private _BaseTreeMap_TreeIterator(int type, BaseTreeMap.Node first, BaseTreeMap.Node max) {
124         this.type = type;
125         this.next = first;
126         this.max = max;
127     }
128
129     /**
130      * Returns true if the Iterator has more elements.
131      * @return true if there are more elements
132      * @throws ConcurrentModificationException if the TreeMap was modified
133      */

134     public boolean hasNext() {
135         if (knownMod != owner._org_ozoneDB_getModification())
136             throw new ConcurrentModificationException JavaDoc();
137         return !next.equals(max);
138     }
139
140     /**
141      * Returns the next element in the Iterator's sequential view.
142      * @return the next element
143      * @throws ConcurrentModificationException if the TreeMap was modified
144      * @throws NoSuchElementException if there is none
145      */

146     public Object JavaDoc next() {
147         if (knownMod != owner._org_ozoneDB_getModification())
148             throw new ConcurrentModificationException JavaDoc();
149         if (next.equals(max))
150             throw new NoSuchElementException JavaDoc();
151         last = next;
152         next = owner._org_ozoneDB_successor(last);
153
154         if (type == BaseTreeMapImpl.VALUES)
155             return last.getValue();
156         else if (type == BaseTreeMapImpl.KEYS)
157             return last.getKey();
158         return last;
159     }
160
161     /**
162      * Removes from the backing TreeMap the last element which was fetched
163      * with the <code>next()</code> method.
164      * @throws ConcurrentModificationException if the TreeMap was modified
165      * @throws IllegalStateException if called when there is no last element
166      */

167     public void remove() {
168         if (last == null) {
169             throw new IllegalStateException JavaDoc();
170         }
171         if (knownMod != owner._org_ozoneDB_getModification()) {
172             throw new ConcurrentModificationException JavaDoc();
173         }
174
175         owner._org_ozoneDB_removeNode(last);
176         last = null;
177         knownMod++;
178     }
179
180 }
Popular Tags