@@ -24,7 +24,17 @@ 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 () { }
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
+ }
28
38
29
39
/**
30
40
* Constructor with external Node creator.
@@ -33,21 +43,6 @@ public PatriciaTrie(INodeCreator creator) {
33
43
this .creator = creator ;
34
44
}
35
45
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
-
51
46
/**
52
47
* {@inheritDoc}
53
48
*/
@@ -67,12 +62,8 @@ public boolean add(C seq) {
67
62
* sequence already exists.
68
63
*/
69
64
protected Node addSequence (C seq ) {
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
- }
65
+ if (root == null )
66
+ root = this .creator .createNewNode (null , null , BLACK );
76
67
77
68
int indexIntoParent = -1 ;
78
69
int indexIntoString = -1 ;
@@ -117,11 +108,7 @@ protected Node addSequence(C seq) {
117
108
118
109
// Create new parent
119
110
if (parent != null ) parent .removeChild (node );
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 );
111
+ Node newParent = this .creator .createNewNode (parent , parentString , BLACK );
125
112
if (parent != null )
126
113
parent .addChild (newParent );
127
114
@@ -133,11 +120,7 @@ protected Node addSequence(C seq) {
133
120
134
121
// Create a new node from the rest of the string
135
122
CharSequence newString = seq .subSequence (indexIntoString , seq .length ());
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 );
123
+ Node newNode2 = this .creator .createNewNode (newParent , newString .toString ().toCharArray (), WHITE );
141
124
newParent .addChild (newNode2 );
142
125
143
126
// New node which was added
@@ -147,11 +130,7 @@ protected Node addSequence(C seq) {
147
130
// converting the previous node
148
131
if (parent != null )
149
132
parent .removeChild (node );
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 );
133
+ Node newParent = this .creator .createNewNode (parent , parentString , WHITE );
155
134
if (parent != null )
156
135
parent .addChild (newParent );
157
136
@@ -177,20 +156,12 @@ protected Node addSequence(C seq) {
177
156
} else if (node .string != null ) {
178
157
// Adding a child
179
158
CharSequence newString = seq .subSequence (indexIntoString , seq .length ());
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 );
159
+ Node newNode = this .creator .createNewNode (node , newString .toString ().toCharArray (), WHITE );
185
160
node .addChild (newNode );
186
161
addedNode = newNode ;
187
162
} else {
188
163
// Add to root node
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 );
164
+ Node newNode = this .creator .createNewNode (node , seq .toString ().toCharArray (), WHITE );
194
165
node .addChild (newNode );
195
166
addedNode = newNode ;
196
167
}
0 commit comments