@@ -24,17 +24,7 @@ public class PatriciaTrie<C extends CharSequence> implements ITree<C> {
24
24
protected static final boolean BLACK = false ; // non-terminating
25
25
protected static final boolean WHITE = true ; // terminating
26
26
27
- public PatriciaTrie () {
28
- this .creator = new INodeCreator () {
29
- /**
30
- * {@inheritDoc}
31
- */
32
- @ Override
33
- public Node createNewNode (Node parent , char [] seq , boolean type ) {
34
- return (new Node (parent , seq , type ));
35
- }
36
- };
37
- }
27
+ public PatriciaTrie () { }
38
28
39
29
/**
40
30
* Constructor with external Node creator.
@@ -43,6 +33,21 @@ public PatriciaTrie(INodeCreator creator) {
43
33
this .creator = creator ;
44
34
}
45
35
36
+ /**
37
+ * Create a new node for sequence.
38
+ *
39
+ * @param parent
40
+ * node of the new node.
41
+ * @param seq
42
+ * of characters which represents this node.
43
+ * @param type
44
+ * of the node, can be either BLACK or WHITE.
45
+ * @return Node which was created.
46
+ */
47
+ protected static Node createNewNode (Node parent , char [] seq , boolean type ) {
48
+ return (new Node (parent , seq , type ));
49
+ }
50
+
46
51
/**
47
52
* {@inheritDoc}
48
53
*/
@@ -62,8 +67,12 @@ public boolean add(C seq) {
62
67
* sequence already exists.
63
68
*/
64
69
protected Node addSequence (C seq ) {
65
- if (root == null )
66
- root = this .creator .createNewNode (null , null , BLACK );
70
+ if (root == null ) {
71
+ if (this .creator == null )
72
+ root = createNewNode (null , null , BLACK );
73
+ else
74
+ root = this .creator .createNewNode (null , null , BLACK );
75
+ }
67
76
68
77
int indexIntoParent = -1 ;
69
78
int indexIntoString = -1 ;
@@ -108,7 +117,11 @@ protected Node addSequence(C seq) {
108
117
109
118
// Create new parent
110
119
if (parent != null ) parent .removeChild (node );
111
- Node newParent = this .creator .createNewNode (parent , parentString , BLACK );
120
+ Node newParent = null ;
121
+ if (this .creator == null )
122
+ newParent = createNewNode (parent , parentString , BLACK );
123
+ else
124
+ newParent = this .creator .createNewNode (parent , parentString , BLACK );
112
125
if (parent != null )
113
126
parent .addChild (newParent );
114
127
@@ -120,7 +133,11 @@ protected Node addSequence(C seq) {
120
133
121
134
// Create a new node from the rest of the string
122
135
CharSequence newString = seq .subSequence (indexIntoString , seq .length ());
123
- Node newNode2 = this .creator .createNewNode (newParent , newString .toString ().toCharArray (), WHITE );
136
+ Node newNode2 = null ;
137
+ if (this .creator == null )
138
+ newNode2 = createNewNode (newParent , newString .toString ().toCharArray (), WHITE );
139
+ else
140
+ newNode2 = this .creator .createNewNode (newParent , newString .toString ().toCharArray (), WHITE );
124
141
newParent .addChild (newNode2 );
125
142
126
143
// New node which was added
@@ -130,7 +147,11 @@ protected Node addSequence(C seq) {
130
147
// converting the previous node
131
148
if (parent != null )
132
149
parent .removeChild (node );
133
- Node newParent = this .creator .createNewNode (parent , parentString , WHITE );
150
+ Node newParent = null ;
151
+ if (this .creator == null )
152
+ newParent = createNewNode (parent , parentString , WHITE );
153
+ else
154
+ newParent = this .creator .createNewNode (parent , parentString , WHITE );
134
155
if (parent != null )
135
156
parent .addChild (newParent );
136
157
@@ -156,12 +177,20 @@ protected Node addSequence(C seq) {
156
177
} else if (node .string != null ) {
157
178
// Adding a child
158
179
CharSequence newString = seq .subSequence (indexIntoString , seq .length ());
159
- Node newNode = this .creator .createNewNode (node , newString .toString ().toCharArray (), WHITE );
180
+ Node newNode = null ;
181
+ if (this .creator == null )
182
+ newNode = createNewNode (node , newString .toString ().toCharArray (), WHITE );
183
+ else
184
+ newNode = this .creator .createNewNode (node , newString .toString ().toCharArray (), WHITE );
160
185
node .addChild (newNode );
161
186
addedNode = newNode ;
162
187
} else {
163
188
// Add to root node
164
- Node newNode = this .creator .createNewNode (node , seq .toString ().toCharArray (), WHITE );
189
+ Node newNode = null ;
190
+ if (this .creator == null )
191
+ newNode = createNewNode (node , seq .toString ().toCharArray (), WHITE );
192
+ else
193
+ newNode = this .creator .createNewNode (node , seq .toString ().toCharArray (), WHITE );
165
194
node .addChild (newNode );
166
195
addedNode = newNode ;
167
196
}
0 commit comments