util: optimize TreeUtil.getPathFromRoot() appcode/142.4216 clion/142.4214 dbe/142.4215 idea/142.4217 phpstorm/142.4219 pycharm/142.4213 rubymine/142.4218 webstorm/142.4220
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Sat, 22 Aug 2015 16:04:25 +0000 (19:04 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Sat, 22 Aug 2015 16:04:25 +0000 (19:04 +0300)
platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java

index 29f22ff896399579ba83035591f358e6485c558b..d3e2810101927f625413445d78753149656971cb 100644 (file)
@@ -192,13 +192,22 @@ public final class TreeUtil {
 
   @NotNull
   public static TreePath getPathFromRoot(@NotNull TreeNode node) {
-    final ArrayList<TreeNode> path = new ArrayList<TreeNode>();
-    do {
-      path.add(node);
-      node = node.getParent();
-    } while (node != null);
-    Collections.reverse(path);
-    return new TreePath(path.toArray());
+    TreeNode[] path = getPathFromRoot(node, 1);
+    return new TreePath(path);
+  }
+
+  @NotNull
+  private static TreeNode[] getPathFromRoot(@NotNull TreeNode node, int depth) {
+    TreeNode[] path;
+    TreeNode parent = node.getParent();
+    if (parent == null) {
+      path = new TreeNode[depth];
+    }
+    else {
+      path = getPathFromRoot(parent, depth + 1);
+    }
+    path[path.length - depth] = node;
+    return path;
   }
 
   @Nullable