From 073b57b268aadca4d0cb9d7e8fe6913fa0d132bb Mon Sep 17 00:00:00 2001 From: Sergey Simonchik Date: Sat, 22 Aug 2015 19:04:25 +0300 Subject: [PATCH] util: optimize TreeUtil.getPathFromRoot() --- .../com/intellij/util/ui/tree/TreeUtil.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java b/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java index 29f22ff89639..d3e281010192 100644 --- a/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java +++ b/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java @@ -192,13 +192,22 @@ public final class TreeUtil { @NotNull public static TreePath getPathFromRoot(@NotNull TreeNode node) { - final ArrayList path = new ArrayList(); - 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