KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > BtreeIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex;
20
21 import org.netbeans.mdr.persistence.*;
22 import java.util.*;
23 import java.io.*;
24
25 /**
26  * Iterator over a BtreeCollection, which is the set of values contained in
27  * a SinglevaluedBtree.
28  *
29  * @author Dana Bergen
30  * @version 1.0
31  */

32 public class BtreeIterator extends Object JavaDoc implements Iterator {
33
34     protected Btree btree;
35     protected SearchResult current;
36     protected BtreePageSource pageSource;
37     protected int modCount;
38
39     BtreeIterator(Btree btree) {
40         try {
41         this.btree = btree;
42         pageSource = btree.pageSource;
43         btree.beginRead();
44         modCount = btree.modCount;
45         current = btree.getFirst();
46         if (current.entryNum >= 0) {
47             current.entryNum--;
48         }
49     } catch (StorageException e) {
50         throw new RuntimeStorageException(e);
51     } finally {
52         btree.endRead();
53     }
54     }
55
56     /**
57      * Tests whether there are any more elements to return
58      *
59      * @return true if a call to next() would succeed
60      */

61     public boolean hasNext() {
62
63     try {
64         btree.beginRead();
65         checkModCount();
66         if (!BtreePage.hasNext(null, current)) {
67             pageSource.unpinPage(current.page);
68         current.page = null;
69         return false;
70         } else {
71             return true;
72         }
73     } catch (StorageException e) {
74         throw new RuntimeStorageException(e);
75     } finally {
76         btree.endRead();
77     }
78     }
79
80     /**
81      * Gets the next value in the btree. If this is on a different page
82      * from the previous value, the old page will be unpinned.
83      * If there are no more records, the last page will be unpinned.
84      *
85      * @return The next value in the btree
86      *
87      * @exception NoSuchElementException If there was any error or if
88      * there are no more records
89      */

90     public Object JavaDoc next() throws NoSuchElementException {
91
92     BtreePage oldPage;
93     
94     if (current.page == null) {
95         throw new NoSuchElementException();
96     }
97     try {
98         btree.beginRead();
99         checkModCount();
100         oldPage = current.page;
101         BtreePage.getNext(null, current);
102         if (current.page != oldPage) {
103             pageSource.unpinPage(oldPage);
104         }
105         if (current.matched) {
106         return getCurrentItem();
107         } else {
108             throw new NoSuchElementException();
109         }
110     } catch (StorageException e) {
111         throw new RuntimeStorageException(e);
112     } finally {
113         btree.endRead();
114     }
115     }
116
117     /**
118      * Returns the data value at the current position
119      */

120     Object JavaDoc getCurrentItem() throws StorageException {
121     return btree.dataInfo.fromBuffer(
122                 current.page.getData(current.entryNum));
123     }
124
125     void checkModCount() {
126     if (btree.modCount > modCount) {
127         throw new ConcurrentModificationException("Index " + btree.getName()
128             + " has been modified since iterator was created.");
129     }
130     }
131
132     /*
133      * Unpin the last page.
134      */

135     protected void finalize() {
136     if (current.page != null) {
137             pageSource.unpinPage(current.page);
138         current.page = null;
139     }
140     }
141
142     /**
143      * This is not supported.
144      *
145      * @exception UnsupportedOperationException Always thrown.
146      */

147     public void remove() throws UnsupportedOperationException JavaDoc {
148         throw new UnsupportedOperationException JavaDoc();
149     }
150 }
151
Popular Tags