KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 /**
25  * @exclude
26  */

27 public class Searcher {
28     
29     private int _lower;
30     
31     private int _upper;
32     
33     private int _cursor;
34     
35     private int _cmp;
36     
37     private final SearchTarget _target;
38     
39     private final int _count;
40     
41     public Searcher(SearchTarget target, int count){
42         if(count < 0){
43             throw new IllegalArgumentException JavaDoc();
44         }
45         _target = target;
46         _count = count;
47         _cmp = -1;
48         if(count == 0){
49             complete();
50             return;
51         }
52         _cursor = -1;
53         _upper = count - 1;
54         adjustCursor();
55     }
56     
57     private void adjustBounds(){
58         if(_cmp > 0){
59             _upper = _cursor - 1;
60             if (_upper < _lower) {
61                 _upper = _lower;
62             }
63             return;
64         }
65         
66         if (_cmp < 0) {
67             if(_lower == _cursor && _lower < _upper){
68                 _lower++;
69             }else{
70                 _lower = _cursor;
71             }
72             return;
73         }
74         
75         if(_target == SearchTarget.ANY){
76             _lower = _cursor;
77             _upper = _cursor;
78         }else if(_target == SearchTarget.HIGHEST){
79             _lower = _cursor;
80         }else if(_target == SearchTarget.LOWEST){
81             _upper = _cursor;
82         }else{
83             throw new IllegalStateException JavaDoc("Unknown target");
84         }
85         
86     }
87     
88     private void adjustCursor(){
89         int oldCursor = _cursor;
90         if(_upper - _lower <= 1){
91             if((_target == SearchTarget.LOWEST) && (_cmp == 0)){
92                 _cursor = _lower;
93             }else{
94                 _cursor = _upper;
95             }
96         }else{
97             _cursor = _lower + ((_upper - _lower) / 2);
98         }
99         if(_cursor == oldCursor){
100             complete();
101         }
102     }
103     
104     public boolean afterLast(){
105         if(_count == 0){
106             return false; // _cursor is 0: not after last
107
}
108         return (_cursor == _count -1) && _cmp < 0;
109     }
110     
111     public boolean beforeFirst() {
112         return (_cursor == 0) && (_cmp > 0);
113     }
114
115     private void complete(){
116         _upper = -2;
117     }
118     
119     public int count(){
120         return _count;
121     }
122     
123     public int cursor() {
124         return _cursor;
125     }
126
127     public boolean foundMatch(){
128         return _cmp == 0;
129     }
130     
131     public boolean incomplete() {
132         return _upper >= _lower;
133     }
134     
135     public void moveForward() {
136         _cursor++;
137     }
138
139     public void resultIs(int cmp){
140         _cmp = cmp;
141         adjustBounds();
142         adjustCursor();
143     }
144
145     public boolean isGreater() {
146         return _cmp < 0;
147     }
148     
149  
150 }
151
Popular Tags