KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > btree > NodeFactory


1 /*
2  * NodeFactory.java
3  *
4  * Created on 25 May 2003, 12:21
5  */

6
7 package com.jofti.btree;
8
9
10 import com.jofti.core.IStoreKey;
11 import com.jofti.core.IStoreManager;
12
13
14 /**
15  * This factory is responsible for generating new Leaf and IndexCache Nodes for the Tree.<p>
16  *
17   * @author Steve Woodcock
18  * @version 1.15<br>
19  */

20 class NodeFactory {
21     
22     private static NodeFactory factory = null;
23     IStoreManager manager =null;
24
25    /** Creates a new instance of NodeFactory */
26     private NodeFactory() {
27     }
28     
29    private NodeFactory(IStoreManager manager){
30     this.manager = manager;
31    }
32     
33     /**
34      * Obtains the NodeFactory Singleton instance.Creates the factory if it does not exist.
35      * @return
36      */

37     static synchronized NodeFactory getInstance() {
38         if (factory == null){
39             factory = new NodeFactory();
40             
41         }
42         return factory;
43     }
44     
45     /**
46      * Obtains the NodeFactory Singleton instance. Creates the factory if it does not exist with the
47      * passed in {@link IStoreManager}.
48      *
49      * @param manager
50      * @return
51      */

52     static synchronized NodeFactory getInstance(IStoreManager manager) {
53         if (factory == null){
54             factory = new NodeFactory(manager);
55             
56         }
57         return factory;
58     }
59     
60     IndexNode createIndexNode() {
61         return new IndexNode();
62     }
63     
64     Node createLeafNode() {
65         Node temp =null;
66         if (manager != null){
67             try {
68                 IStoreKey storeKey = manager.getNextKey();
69                 temp = new PagedLeafNode(manager,storeKey);
70             } catch (Exception JavaDoc e){
71                 throw new RuntimeException JavaDoc("unable to get node ",e);
72             }
73             
74             
75         }else{
76             temp = new LeafNode();
77         }
78             
79     return temp;
80     }
81    
82     
83     IndexNodeEntry createIndexNodeEntry() {
84         return new IndexNodeEntry();
85     }
86     
87     LeafNodeEntry createLeafNodeEntry() {
88         return new LeafNodeEntry();
89     }
90     
91     LeafNodeEntry createMaxLeafNodeEntry() {
92        
93             return new MaxLeafNodeEntry();
94        
95     }
96     
97     LeafNodeEntry createLeafNodeEntry(Object JavaDoc uniqueId, Comparable JavaDoc value) {
98             return new LeafNodeEntry(uniqueId,value);
99     }
100
101 }
102
Popular Tags