19
19
public class BaseTreeNode extends TreeNode {
20
20
private ImageDescriptor imageDescriptor ;
21
21
private String text = "" ;
22
+ private Object value ;
22
23
23
24
public BaseTreeNode (Object value ) {
24
25
super (value );
25
26
if (value instanceof String )
26
27
this .text = value .toString ();
27
28
}
28
29
29
- public void setValue (Object value ) {
30
- this .value = value ;
31
- }
32
-
33
30
public void addChild (BaseTreeNode child ) {
34
31
TreeNode [] children = getChildren ();
35
32
List <BaseTreeNode > list = new ArrayList <BaseTreeNode >();
@@ -41,7 +38,12 @@ public void addChild(BaseTreeNode child) {
41
38
}
42
39
child .setParent (this );
43
40
list .add (child );
44
- this .setChildren (list .toArray (new BaseTreeNode [list .size ()]));
41
+
42
+ // Calls to a collection's `toArray(E[])` method should specify a target array
43
+ // of zero size. This allows the JVM
44
+ // to optimize the memory allocation and copying as much as possible.
45
+ // https://shipilev.net/blog/2016/arrays-wisdom-ancients/
46
+ this .setChildren (list .toArray (new BaseTreeNode [0 ]));
45
47
}
46
48
47
49
public void removeChildren () {
@@ -104,6 +106,10 @@ public void setText(String text) {
104
106
this .text = text ;
105
107
}
106
108
109
+ public void setValue (Object value ) {
110
+ this .value = value ;
111
+ }
112
+
107
113
@ Override
108
114
public String toString () {
109
115
return this .value .toString ();
@@ -112,8 +118,6 @@ public String toString() {
112
118
public void reset () {
113
119
this .removeChildren ();
114
120
this .text = "" ;
115
- this .value = null ;
116
- this .imageDescriptor = null ;
117
121
}
118
122
119
123
/**
0 commit comments