KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > TreeKeyIterator


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.foundation;
22
23 /**
24  * @exclude
25  */

26 public class TreeKeyIterator implements Iterator4 {
27     
28     private final Tree _tree;
29
30     private Stack4 _stack;
31
32     public TreeKeyIterator(Tree tree) {
33         _tree = tree;
34     }
35
36     public Object JavaDoc current() {
37         if(_stack == null){
38             throw new IllegalStateException JavaDoc();
39         }
40         Tree tree = peek();
41         if(tree == null){
42             return null;
43         }
44         return tree.key();
45     }
46     
47     private Tree peek(){
48         return (Tree) _stack.peek();
49     }
50
51     public void reset() {
52         _stack = null;
53     }
54
55     public boolean moveNext() {
56         if(_stack == null){
57             initStack();
58             return _stack != null;
59         }
60         
61         Tree current = peek();
62         if(current == null){
63             return false;
64         }
65         
66         if(pushPreceding(current._subsequent)){
67             return true;
68         }
69         
70         while(true){
71             _stack.pop();
72             Tree parent = peek();
73             if(parent == null){
74                 return false;
75             }
76             if(current == parent._preceding){
77                 return true;
78             }
79             current = parent;
80         }
81     }
82
83     private void initStack() {
84         if(_tree == null){
85             return;
86         }
87         _stack = new Stack4();
88         pushPreceding(_tree);
89     }
90
91     private boolean pushPreceding(Tree node) {
92         if(node == null){
93             return false;
94         }
95         while (node != null) {
96             _stack.push(node);
97             node = node._preceding;
98         }
99         return true;
100     }
101
102 }
103
Popular Tags