From 0be0fbce4140512a2ea08a5b2d1aae8a70203694 Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Mon, 13 Aug 2018 16:39:27 +0530 Subject: [PATCH] Add TreeItem recycling for TreeVisualizer --- .../visualizers/TreeVisualizer.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/rohitawate/everest/controllers/visualizers/TreeVisualizer.java b/src/main/java/com/rohitawate/everest/controllers/visualizers/TreeVisualizer.java index e70364d..5a4cbee 100644 --- a/src/main/java/com/rohitawate/everest/controllers/visualizers/TreeVisualizer.java +++ b/src/main/java/com/rohitawate/everest/controllers/visualizers/TreeVisualizer.java @@ -29,9 +29,12 @@ import java.util.Map; public class TreeVisualizer extends Visualizer { private TreeView visualizer; + private ArrayList> recycledNodes; + private int lastNodeIndex; public TreeVisualizer() { visualizer = new TreeView<>(); + recycledNodes = new ArrayList<>(); visualizer.setShowRoot(false); visualizer.setCache(true); setContent(visualizer); @@ -39,7 +42,7 @@ public class TreeVisualizer extends Visualizer { public void populate(String body) throws IOException { JsonNode tree = EverestUtilities.jsonMapper.readTree(body); - this.populate(new TreeItem<>(), "root", tree); + this.populate(getTreeNode(null), "root", tree); } private void populate(TreeItem rootItem, String rootName, JsonNode root) { @@ -60,9 +63,9 @@ public class TreeVisualizer extends Visualizer { currentNode = iterator.next(); if (currentNode.isValueNode()) { - items.add(new TreeItem<>(i++ + ": " + EverestUtilities.trimString(currentNode.toString()))); + items.add(getTreeNode(i++ + ": " + EverestUtilities.trimString(currentNode.toString()))); } else if (currentNode.isObject()) { - TreeItem newRoot = new TreeItem<>(); + TreeItem newRoot = getTreeNode(null); newRoot.setExpanded(true); items.add(newRoot); populate(newRoot, i++ + ": [Anonymous Object]", currentNode); @@ -77,10 +80,10 @@ public class TreeVisualizer extends Visualizer { currentNode = currentEntry.getValue(); if (currentNode.isValueNode()) { - items.add(new TreeItem<>(currentEntry.getKey() + ": " + items.add(getTreeNode(currentEntry.getKey() + ": " + EverestUtilities.trimString(currentNode.toString()))); } else if (currentNode.isArray() || currentNode.isObject()) { - TreeItem newRoot = new TreeItem<>(); + TreeItem newRoot = getTreeNode(null); newRoot.setExpanded(true); items.add(newRoot); populate(newRoot, currentEntry.getKey(), currentNode); @@ -91,7 +94,23 @@ public class TreeVisualizer extends Visualizer { rootItem.getChildren().addAll(items); } + private TreeItem getTreeNode(String value) { + TreeItem node; + + if (recycledNodes.size() > 0) { + node = recycledNodes.get(lastNodeIndex); + recycledNodes.remove(lastNodeIndex--); + } else { + node = new TreeItem<>(); + } + + node.setValue(value); + node.getChildren().clear(); + return node; + } + public void clear() { + recycledNodes.addAll(visualizer.getRoot().getChildren()); visualizer.setRoot(null); System.gc(); }