KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > TreeReader


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;
22
23 import com.db4o.foundation.Tree;
24
25 /**
26  * @exclude
27  */

28 public final class TreeReader
29 {
30     private final Readable JavaDoc i_template;
31     private final YapReader i_bytes;
32     private int i_current = 0;
33     private int i_levels = 0;
34     private int i_size;
35     private boolean i_orderOnRead;
36     
37     public TreeReader(YapReader a_bytes, Readable JavaDoc a_template) {
38         this(a_bytes, a_template, false);
39     }
40     
41     public TreeReader(YapReader a_bytes, Readable JavaDoc a_template, boolean a_orderOnRead) {
42         i_template = a_template;
43         i_bytes = a_bytes;
44         i_orderOnRead = a_orderOnRead;
45     }
46     
47     public Tree read() {
48         return read(i_bytes.readInt());
49     }
50     
51     public Tree read(int a_size){
52         i_size = a_size;
53         if(i_size > 0){
54             if(i_orderOnRead){
55                 Tree tree = null;
56                 for (int i = 0; i < i_size; i++) {
57                     tree = Tree.add(tree, (Tree)i_template.read(i_bytes));
58                 }
59                 return tree;
60             }
61             while ((1 << i_levels) < (i_size + 1)){
62                 i_levels ++;
63             }
64             return linkUp(null, i_levels);
65         }
66         return null;
67     }
68     
69     private final Tree linkUp(Tree a_preceding, int a_level){
70         Tree node = (Tree)i_template.read(i_bytes);
71         i_current++;
72         node._preceding = a_preceding;
73         node._subsequent = linkDown(a_level + 1);
74         node.calculateSize();
75         if(i_current < i_size){
76             return linkUp(node, a_level - 1);
77         }
78         return node;
79
80     }
81
82     private final Tree linkDown(int a_level){
83         if(i_current < i_size){
84             i_current++;
85             if(a_level < i_levels) {
86                 Tree preceding = linkDown(a_level + 1);
87                 Tree node = (Tree)i_template.read(i_bytes);
88                 node._preceding = preceding;
89                 node._subsequent = linkDown(a_level + 1);
90                 node.calculateSize();
91                 return node;
92             }
93             return (Tree)i_template.read(i_bytes);
94         }
95         return null;
96     }
97 }
98
Popular Tags