01: /*
02: * NodeFactory.java
03: *
04: * Created on 25 May 2003, 12:21
05: */
06:
07: package com.jofti.btree;
08:
09: import com.jofti.core.IStoreKey;
10: import com.jofti.core.IStoreManager;
11:
12: /**
13: * This factory is responsible for generating new Leaf and IndexCache Nodes for the Tree.<p>
14: *
15: * @author Steve Woodcock
16: * @version 1.15<br>
17: */
18: class NodeFactory {
19:
20: private static NodeFactory factory = null;
21: IStoreManager manager = null;
22:
23: /** Creates a new instance of NodeFactory */
24: private NodeFactory() {
25: }
26:
27: private NodeFactory(IStoreManager manager) {
28: this .manager = manager;
29: }
30:
31: /**
32: * Obtains the NodeFactory Singleton instance.Creates the factory if it does not exist.
33: * @return
34: */
35: static synchronized NodeFactory getInstance() {
36: if (factory == null) {
37: factory = new NodeFactory();
38:
39: }
40: return factory;
41: }
42:
43: /**
44: * Obtains the NodeFactory Singleton instance. Creates the factory if it does not exist with the
45: * passed in {@link IStoreManager}.
46: *
47: * @param manager
48: * @return
49: */
50: static synchronized NodeFactory getInstance(IStoreManager manager) {
51: if (factory == null) {
52: factory = new NodeFactory(manager);
53:
54: }
55: return factory;
56: }
57:
58: IndexNode createIndexNode() {
59: return new IndexNode();
60: }
61:
62: Node createLeafNode() {
63: Node temp = null;
64: if (manager != null) {
65: try {
66: IStoreKey storeKey = manager.getNextKey();
67: temp = new PagedLeafNode(manager, storeKey);
68: } catch (Exception e) {
69: throw new RuntimeException("unable to get node ", e);
70: }
71:
72: } else {
73: temp = new LeafNode();
74: }
75:
76: return temp;
77: }
78:
79: IndexNodeEntry createIndexNodeEntry() {
80: return new IndexNodeEntry();
81: }
82:
83: LeafNodeEntry createLeafNodeEntry() {
84: return new LeafNodeEntry();
85: }
86:
87: LeafNodeEntry createMaxLeafNodeEntry() {
88:
89: return new MaxLeafNodeEntry();
90:
91: }
92:
93: LeafNodeEntry createLeafNodeEntry(Object uniqueId, Comparable value) {
94: return new LeafNodeEntry(uniqueId, value);
95: }
96:
97: }
|