KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > btree > BTreePointer


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.btree;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25
26 /**
27  * @exclude
28  */

29 public class BTreePointer{
30     
31     public static BTreePointer max(BTreePointer x, BTreePointer y) {
32         if (x == null) {
33             return x;
34         }
35         if (y == null) {
36             return y;
37         }
38         if (x.compareTo(y) > 0) {
39             return x;
40         }
41         return y;
42     }
43
44     public static BTreePointer min(BTreePointer x, BTreePointer y) {
45         if (x == null) {
46             return y;
47         }
48         if (y == null) {
49             return x;
50         }
51         if (x.compareTo(y) < 0) {
52             return x;
53         }
54         return y;
55     }
56     
57     private final BTreeNode _node;
58     
59     private final int _index;
60
61     private final Transaction _transaction;
62
63     private final YapReader _nodeReader;
64    
65     public BTreePointer(Transaction transaction, YapReader nodeReader, BTreeNode node, int index) {
66         if(transaction == null || node == null){
67             throw new ArgumentNullException();
68         }
69         _transaction = transaction;
70         _nodeReader = nodeReader;
71         _node = node;
72         _index = index;
73     }
74
75     public final Transaction transaction() {
76         return _transaction;
77     }
78     
79     public final int index(){
80         return _index;
81     }
82     
83     public final BTreeNode node() {
84         return _node;
85     }
86     
87     public final Object JavaDoc key() {
88         return node().key(transaction(), nodeReader(), index());
89     }
90
91     public final Object JavaDoc value() {
92         return node().value(nodeReader(), index());
93     }
94     
95     private YapReader nodeReader() {
96         return _nodeReader;
97     }
98     
99     public BTreePointer next(){
100         int indexInMyNode = index() + 1;
101         while(indexInMyNode < node().count()){
102             if(node().indexIsValid(transaction(), indexInMyNode)){
103                 return new BTreePointer(transaction(), nodeReader(), node(), indexInMyNode);
104             }
105             indexInMyNode ++;
106         }
107         int newIndex = -1;
108         BTreeNode nextNode = node();
109         YapReader nextReader = null;
110         while(newIndex == -1){
111             nextNode = nextNode.nextNode();
112             if(nextNode == null){
113                 return null;
114             }
115             nextReader = nextNode.prepareRead(transaction());
116             newIndex = nextNode.firstKeyIndex(transaction());
117         }
118         return new BTreePointer(transaction(), nextReader, nextNode, newIndex);
119     }
120     
121     public BTreePointer previous() {
122         int indexInMyNode = index() - 1;
123         while(indexInMyNode >= 0){
124             if(node().indexIsValid(transaction(), indexInMyNode)){
125                 return new BTreePointer(transaction(), nodeReader(), node(), indexInMyNode);
126             }
127             indexInMyNode --;
128         }
129         int newIndex = -1;
130         BTreeNode previousNode = node();
131         YapReader previousReader = null;
132         while(newIndex == -1){
133             previousNode = previousNode.previousNode();
134             if(previousNode == null){
135                 return null;
136             }
137             previousReader = previousNode.prepareRead(transaction());
138             newIndex = previousNode.lastKeyIndex(transaction());
139         }
140         return new BTreePointer(transaction(), previousReader, previousNode, newIndex);
141     }
142
143     
144     public boolean equals(Object JavaDoc obj) {
145         if(this == obj){
146             return true;
147         }
148         if(! (obj instanceof BTreePointer)){
149             return false;
150         }
151         BTreePointer other = (BTreePointer) obj;
152         
153         if(index() != other.index()){
154             return false;
155         }
156         
157         return node().equals(other.node());
158     }
159     
160     public String JavaDoc toString() {
161         return "BTreePointer(index=" + index() + ", node=" + node() + ")";
162     }
163
164     public int compareTo(BTreePointer y) {
165         if (null == y) {
166             throw new ArgumentNullException();
167         }
168         if (btree() != y.btree()) {
169             throw new IllegalArgumentException JavaDoc();
170         }
171         return btree().compareKeys(key(), y.key());
172     }
173
174     private BTree btree() {
175         return node().btree();
176     }
177
178     public static boolean lessThan(BTreePointer x, BTreePointer y) {
179         return BTreePointer.min(x, y) == x
180             && !equals(x, y);
181     }
182
183     public static boolean equals(BTreePointer x, BTreePointer y) {
184         if (x == null) {
185             return y == null;
186         }
187         return x.equals(y);
188     }
189
190     public boolean isValid() {
191         return node().indexIsValid(transaction(), index());
192     }
193 }
194
Popular Tags