From: Alexandre Montplaisir Date: Tue, 16 Oct 2012 17:41:21 +0000 (-0400) Subject: tmf: Move the stats tree structure logic to the nodes X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=7588c8108899de5298e62baae26a23eec89cfb03;p=deliverable%2Ftracecompass.git tmf: Move the stats tree structure logic to the nodes Re-organize the TmfStatisticsTree so that the information about node children are stored in the nodes themselves, and not all at the top-level. This makes debugging, and eventually extending the tree, much easier, imo. This patch also drops the fKeys (which limited the types of children each node can have) and the weird limitation where only nodes whose path depths are mutiples of 2 could have values. Now the tree is more generic, to give better flexibility to potential sub-classes. Change-Id: I30bc0f7f47256388b6c55e09f09b26286723c0b9 Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/8298 Tested-by: Hudson CI Reviewed-by: Patrick Tasse IP-Clean: Patrick Tasse Tested-by: Patrick Tasse --- diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java index fd9f6d7bb3..d4fb301f6d 100644 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java @@ -102,7 +102,7 @@ public class TmfBaseColumnDataProviderTest extends TestCase { fStatsData = new TmfStatisticsTree(); - fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes); + fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes); fStatsData.setTotal(fTestName, true, 3); fStatsData.setTypeCount(fTestName, fEvent1.getType().getName(), true, 1); @@ -123,9 +123,9 @@ public class TmfBaseColumnDataProviderTest extends TestCase { assertNotNull("getColumnData", columnsData); assertEquals("getColumnData", 3, columnsData.size()); - TmfStatisticsTreeNode parentNode = fStatsData.get(fTestName); - TmfStatisticsTreeNode treeNode1 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()); - TmfStatisticsTreeNode treeNode2 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent3.getType().getName()); + TmfStatisticsTreeNode parentNode = fStatsData.getNode(fTestName); + TmfStatisticsTreeNode treeNode1 = fStatsData.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()); + TmfStatisticsTreeNode treeNode2 = fStatsData.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent3.getType().getName()); ViewerComparator vComp = null; for (TmfBaseColumnData columnData : columnsData) { assertNotNull("getColumnData", columnData); @@ -135,7 +135,7 @@ public class TmfBaseColumnDataProviderTest extends TestCase { // Testing labelProvider ColumnLabelProvider labelProvider = columnData.getLabelProvider(); if (columnData.getHeader().compareTo(LEVEL_COLUMN) == 0) { - assertEquals("getColumnData", 0, labelProvider.getText(treeNode1).compareTo(treeNode1.getKey())); + assertEquals("getColumnData", 0, labelProvider.getText(treeNode1).compareTo(treeNode1.getName())); } else if (columnData.getHeader().compareTo(EVENTS_COUNT_COLUMN) == 0) { assertEquals("getColumnData", 0, labelProvider.getText(treeNode1).compareTo(Long.toString(1))); } diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java index d0b7861ac7..c333010b94 100755 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java @@ -59,7 +59,7 @@ public class TmfBaseColumnDataTest extends TestCase { fLabelProvider = new ColumnLabelProvider() { @Override public String getText(Object element) { - return ((TmfStatisticsTreeNode) element).getKey(); + return ((TmfStatisticsTreeNode) element).getName(); } @Override @@ -73,7 +73,7 @@ public class TmfBaseColumnDataTest extends TestCase { TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1; TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2; - return n1.getKey().compareTo(n2.getKey()); + return n1.getName().compareTo(n2.getName()); } }; fPercentageProvider = new ITmfColumnPercentageProvider() { @@ -93,7 +93,7 @@ public class TmfBaseColumnDataTest extends TestCase { TmfStatisticsTree baseData = new TmfStatisticsTree(); fTraceName = "trace1"; - fTreeNode = new TmfStatisticsTreeNode(baseData, fTraceName); + fTreeNode = new TmfStatisticsTreeNode(baseData, baseData.getRootNode(), fTraceName); fBaseColumnData = new TmfBaseColumnData(fHeader, fWidth, fAlignment, fToolTip, fLabelProvider, fComparator, fPercentageProvider); } diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java index a65fc93024..0a570fe638 100755 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java @@ -13,6 +13,7 @@ package org.eclipse.linuxtools.tmf.ui.tests.statistics; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.Vector; @@ -68,7 +69,7 @@ public class TmfBaseStatisticsDataTest extends TestCase { private final TmfEventField fContent2; private final TmfEventField fContent3; - private final TmfStatisticsTree fStatsData; + private final TmfStatisticsTree fStatsTree; // ------------------------------------------------------------------------ // Housekeeping @@ -92,12 +93,12 @@ public class TmfBaseStatisticsDataTest extends TestCase { fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content"); fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType3, fContent3, fReference); - fStatsData = new TmfStatisticsTree(); + fStatsTree = new TmfStatisticsTree(); - fStatsData.setTotal(fTestName, true, 3); - fStatsData.setTypeCount(fTestName, fEvent1.getType().getName(), true, 1); - fStatsData.setTypeCount(fTestName, fEvent2.getType().getName(), true, 1); - fStatsData.setTypeCount(fTestName, fEvent3.getType().getName(), true, 1); + fStatsTree.setTotal(fTestName, true, 3); + fStatsTree.setTypeCount(fTestName, fEvent1.getType().getName(), true, 1); + fStatsTree.setTypeCount(fTestName, fEvent2.getType().getName(), true, 1); + fStatsTree.setTypeCount(fTestName, fEvent3.getType().getName(), true, 1); } // ------------------------------------------------------------------------ @@ -109,87 +110,38 @@ public class TmfBaseStatisticsDataTest extends TestCase { */ public void testGetChildren() { // Getting children of the ROOT - Collection childrenTreeNode = fStatsData.getChildren(TmfStatisticsTree.ROOT); + Collection childrenTreeNode = fStatsTree.getRootNode().getChildren(); assertEquals("getChildren", 1, childrenTreeNode.size()); TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", fTestName, treeNode.getKey()); + assertEquals("getChildren", fTestName, treeNode.getName()); // Getting children of the trace - childrenTreeNode = fStatsData.getChildren(fTestName); + childrenTreeNode = fStatsTree.getNode(fTestName).getChildren(); assertEquals("getChildren", 1, childrenTreeNode.size()); treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); + assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getName()); Vector keyExpected = new Vector(); keyExpected.add(fEvent1.getType().getName()); keyExpected.add(fEvent3.getType().getName()); // Getting children of a category - childrenTreeNode = fStatsData.getChildren(treeNode.getPath()); + childrenTreeNode = treeNode.getChildren(); assertEquals("getChildren", 2, childrenTreeNode.size()); Iterator iterChild = childrenTreeNode.iterator(); TmfStatisticsTreeNode temp; while (iterChild.hasNext()) { temp = iterChild.next(); - assertEquals(0, fStatsData.getChildren(temp.getPath()).size()); - if (keyExpected.contains(temp.getKey())) { - keyExpected.removeElement(temp.getKey()); + assertEquals(0, temp.getChildren().size()); + if (keyExpected.contains(temp.getName())) { + keyExpected.removeElement(temp.getName()); } else { fail(); } } // Get children of a specific event type - childrenTreeNode = fStatsData.getChildren(childrenTreeNode.iterator().next().getPath()); - assertEquals("getChildren", 0, childrenTreeNode.size()); - } - - // ------------------------------------------------------------------------ - // GetAllChildren - // ------------------------------------------------------------------------ - - /** - * Test getting of all children. - */ - public void testGetAllChildren() { - // Getting children of the ROOT - Collection childrenTreeNode = fStatsData.getAllChildren(TmfStatisticsTree.ROOT); - assertEquals("getChildren", 1, childrenTreeNode.size()); - TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", fTestName, treeNode.getKey()); - - // Getting children of the trace - childrenTreeNode = fStatsData.getAllChildren(fTestName); - assertEquals("getChildren", 1, childrenTreeNode.size()); - treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); - - Vector keyExpected = new Vector(); - keyExpected.add(fEvent1.getType().getName()); - keyExpected.add(fEvent3.getType().getName()); - /* - * It should return the eventType even though the number of events - * equals 0 - */ - fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()).reset(); - // Getting children of a category - childrenTreeNode = fStatsData.get(treeNode.getPath()).getAllChildren(); - assertEquals("getChildren", 2, childrenTreeNode.size()); - - Iterator iterChild = childrenTreeNode.iterator(); - TmfStatisticsTreeNode temp; - while (iterChild.hasNext()) { - temp = iterChild.next(); - assertEquals(0, fStatsData.getAllChildren(temp.getPath()).size()); - if (keyExpected.contains(temp.getKey())) { - keyExpected.removeElement(temp.getKey()); - } else { - fail(); - } - } - - // Get children of a specific event type - childrenTreeNode = fStatsData.getAllChildren(childrenTreeNode.iterator().next().getPath()); + childrenTreeNode = childrenTreeNode.iterator().next().getChildren(); assertEquals("getChildren", 0, childrenTreeNode.size()); } @@ -201,14 +153,14 @@ public class TmfBaseStatisticsDataTest extends TestCase { * Test registering of events. */ public void testRegisterEvent() { - TmfStatisticsTreeNode trace = fStatsData.get(fTestName); + TmfStatisticsTreeNode trace = fStatsTree.getNode(fTestName); assertEquals("registerEvent", 3, trace.getValues().getTotal()); - Collection childrenTreeNode = fStatsData.getChildren(fTestName, Messages.TmfStatisticsData_EventTypes); + Collection childrenTreeNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getChildren(); for (TmfStatisticsTreeNode child : childrenTreeNode) { - if (child.getKey().compareTo(fEvent1.getType().getName()) == 0) { + if (child.getName().compareTo(fEvent1.getType().getName()) == 0) { assertEquals("registerEvent", 1, child.getValues().getTotal()); - } else if (child.getKey().compareTo(fEvent3.getType().getName()) == 0) { + } else if (child.getName().compareTo(fEvent3.getType().getName()) == 0) { assertEquals("registerEvent", 1, child.getValues().getTotal()); } } @@ -222,7 +174,7 @@ public class TmfBaseStatisticsDataTest extends TestCase { * Test getter. */ public void testGet() { - TmfStatisticsTreeNode traceRoot = fStatsData.get(fTestName); + TmfStatisticsTreeNode traceRoot = fStatsTree.getNode(fTestName); assertNotNull("get", traceRoot); assertEquals("get", 0, traceRoot.getPath()[0].compareTo(fTestName)); assertEquals("get", 3, traceRoot.getValues().getTotal()); @@ -241,21 +193,21 @@ public class TmfBaseStatisticsDataTest extends TestCase { TmfStatisticsTreeNode newEventTypeNode; // newEventType is not in the tree - newEventTypeNode = fStatsData.get(newEventType); - assertNull("getOrCreate", newEventTypeNode); + newEventTypeNode = fStatsTree.getNode(newEventType); + assertNull(newEventTypeNode); - newEventTypeNode = fStatsData.getOrCreate(newEventType); - assertNotNull("getOrCreate", newEventTypeNode); - assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType)); + newEventTypeNode = fStatsTree.getOrCreateNode(newEventType); + assertNotNull(newEventTypeNode); + assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath())); // newEventType is in the tree newEventTypeNode.reset(); - newEventTypeNode = fStatsData.get(newEventType); - assertNotNull("getOrCreate", newEventTypeNode); + newEventTypeNode = fStatsTree.getNode(newEventType); + assertNotNull(newEventTypeNode); - newEventTypeNode = fStatsData.getOrCreate(newEventType); - assertNotNull("getOrCreate", newEventTypeNode); - assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType)); + newEventTypeNode = fStatsTree.getOrCreateNode(newEventType); + assertNotNull(newEventTypeNode); + assertTrue(Arrays.equals(newEventType, newEventTypeNode.getPath())); } // ------------------------------------------------------------------------ @@ -266,25 +218,16 @@ public class TmfBaseStatisticsDataTest extends TestCase { * Test getting of parent node. */ public void testGetParent() { - TmfStatisticsTreeNode parentNode = fStatsData.getParent(TmfStatisticsTree.ROOT); - assertNull("getParent", parentNode); - - parentNode = fStatsData.getParent("TreeRootNode that should not exist"); - assertNotNull("getParent", parentNode); - assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(TmfStatisticsTree.ROOT).getKey().toString())); + TmfStatisticsTreeNode parentNode = fStatsTree.getRootNode().getParent(); + assertNull(parentNode); - parentNode = fStatsData.getParent("TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist"); - assertNull("getParent", parentNode); - parentNode = fStatsData.getParent(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName()); - assertNull("getParent", parentNode); + parentNode = fStatsTree.getNode(fTestName).getParent(); + assertNotNull(parentNode); + assertEquals(parentNode.getPath().toString(), fStatsTree.getRootNode().getPath().toString()); - parentNode = fStatsData.getParent(fTestName); - assertNotNull("getParent", parentNode); - assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(TmfStatisticsTree.ROOT.toString())); - - parentNode = fStatsData.getParent(fTestName, Messages.TmfStatisticsData_EventTypes); - assertNotNull("getParent", parentNode); - assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(fTestName).getPath().toString())); + parentNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getParent(); + assertNotNull(parentNode); + assertEquals(parentNode.getPath().toString(), fStatsTree.getNode(fTestName).getPath().toString()); } // ------------------------------------------------------------------------ @@ -295,15 +238,13 @@ public class TmfBaseStatisticsDataTest extends TestCase { * Test reset method */ public void testReset() { - fStatsData.reset(fTestName, Messages.TmfStatisticsData_EventTypes); - - assertEquals("reset", 0, fStatsData.getChildren(fTestName, Messages.TmfStatisticsData_EventTypes).size()); - assertNull("reset", fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName())); - assertNull("reset", fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getName())); + fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).reset(); - fStatsData.reset(fTestName); + assertEquals(0, fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes).getChildren().size()); + assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName())); + assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getName())); - // A root should always have at least one child that is eventType - assertEquals("reset", 1, fStatsData.getChildren(fTestName).size()); + fStatsTree.getNode(fTestName).reset(); + assertEquals(0, fStatsTree.getNode(fTestName).getChildren().size()); } } diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeManagerTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeManagerTest.java index 58225e13e5..e7601775ee 100755 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeManagerTest.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeManagerTest.java @@ -84,12 +84,12 @@ public class TmfStatisticsTreeManagerTest extends TestCase { TmfStatisticsTreeNode returnRootNode = TmfStatisticsTreeManager.addStatsTreeRoot(fDataKey1, fStatisticsData1); assertSame(fStatisticsData1, TmfStatisticsTreeManager.getStatTree(fDataKey1)); - assertSame(fStatisticsData1.get(TmfStatisticsTree.ROOT), returnRootNode); + assertSame(fStatisticsData1.getRootNode(), returnRootNode); // Overwriting the value returnRootNode = TmfStatisticsTreeManager.addStatsTreeRoot(fDataKey1, fStatisticsData2); assertSame(fStatisticsData2, TmfStatisticsTreeManager.getStatTree(fDataKey1)); - assertSame(fStatisticsData2.get(TmfStatisticsTree.ROOT), returnRootNode); + assertSame(fStatisticsData2.getRootNode(), returnRootNode); // Success } catch(Exception e) { diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java index 5ab6e83dc0..394a85a4ed 100755 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java @@ -19,11 +19,6 @@ import java.util.Vector; import junit.framework.TestCase; -import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; -import org.eclipse.linuxtools.tmf.core.event.TmfEvent; -import org.eclipse.linuxtools.tmf.core.event.TmfEventField; -import org.eclipse.linuxtools.tmf.core.event.TmfEventType; -import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.Messages; import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree; import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode; @@ -31,44 +26,19 @@ import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeN /** * TmfStatisticsTreeNode Test Cases. */ -@SuppressWarnings("nls") public class TmfStatisticsTreeNodeTest extends TestCase { // ------------------------------------------------------------------------ // Fields // ------------------------------------------------------------------------ - private String fTestName = null; + private final String fTypeId1 = "Some type1"; //$NON-NLS-1$ + private final String fTypeId2 = "Some type2"; //$NON-NLS-1$ + private final String fTypeId3 = "Some type3"; //$NON-NLS-1$ - private final String fContext = "UnitTest"; - private final String fTypeId1 = "Some type1"; - private final String fTypeId2 = "Some type2"; + private final TmfStatisticsTree fStatsTree; - private final String fLabel0 = "label1"; - private final String fLabel1 = "label2"; - private final String fLabel2 = "label3"; - private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 }; - - private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); - private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); - private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5); - - private final String fSource = "Source"; - - private final TmfEventType fType1 = new TmfEventType(fContext, fTypeId1, TmfEventField.makeRoot(fLabels)); - private final TmfEventType fType2 = new TmfEventType(fContext, fTypeId2, TmfEventField.makeRoot(fLabels)); - - private final String fReference = "Some reference"; - - private final TmfEvent fEvent1; - private final TmfEvent fEvent2; - private final TmfEvent fEvent3; - - private final TmfEventField fContent1; - private final TmfEventField fContent2; - private final TmfEventField fContent3; - - private final TmfStatisticsTree fStatsData; + private String fTestName = null; // ------------------------------------------------------------------------ // Housekeeping @@ -82,28 +52,19 @@ public class TmfStatisticsTreeNodeTest extends TestCase { super(name); fTestName = name; - - fContent1 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some content"); - fEvent1 = new TmfEvent(null, fTimestamp1, fSource, fType1, fContent1, fReference); - - fContent2 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other content"); - fEvent2 = new TmfEvent(null, fTimestamp2, fSource, fType1, fContent2, fReference); - - fContent3 = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, "Some other different content"); - fEvent3 = new TmfEvent(null, fTimestamp3, fSource, fType2, fContent3, fReference); - - fStatsData = new TmfStatisticsTree(); - - fStatsData.setTotal(fTestName, true, 9); - fStatsData.setTypeCount(fTestName, fEvent1.getType().getName(), true, 2); - fStatsData.setTypeCount(fTestName, fEvent2.getType().getName(), true, 3); - fStatsData.setTypeCount(fTestName, fEvent3.getType().getName(), true, 4); - - // Registers some events in time range - fStatsData.setTotal(fTestName, false, 9); - fStatsData.setTypeCount(fTestName, fEvent1.getType().getName(), false, 2); - fStatsData.setTypeCount(fTestName, fEvent2.getType().getName(), false, 3); - fStatsData.setTypeCount(fTestName, fEvent3.getType().getName(), false, 4); + fStatsTree = new TmfStatisticsTree(); + + /* Enter some global values */ + fStatsTree.setTotal(fTestName, true, 18); + fStatsTree.setTypeCount(fTestName, fTypeId1, true, 5); + fStatsTree.setTypeCount(fTestName, fTypeId2, true, 6); + fStatsTree.setTypeCount(fTestName, fTypeId3, true, 7); + + /* Enter some time range values */ + fStatsTree.setTotal(fTestName, false, 9); + fStatsTree.setTypeCount(fTestName, fTypeId1, false, 2); + fStatsTree.setTypeCount(fTestName, fTypeId2, false, 3); + fStatsTree.setTypeCount(fTestName, fTypeId3, false, 4); } // ------------------------------------------------------------------------ @@ -114,22 +75,22 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test checking for child. */ public void testContainsChild() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); + TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode(); + TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName); // Creates a category from the key already created TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - assertTrue("containsChild", rootNode.containsChild(fTestName)); - assertFalse("containsChild", rootNode.containsChild(catNode.getKey())); - assertFalse("containsChild", rootNode.containsChild(null)); + assertTrue(rootNode.containsChild(fTestName)); + assertFalse(rootNode.containsChild(catNode.getName())); + assertFalse(rootNode.containsChild(null)); - assertTrue("containsChild", traceNode.containsChild(catNode.getKey())); - assertFalse("containsChild", traceNode.containsChild(fType1.getName())); - assertFalse("containsChild", traceNode.containsChild(null)); + assertTrue(traceNode.containsChild(catNode.getName())); + assertFalse(traceNode.containsChild(fTypeId1)); + assertFalse(traceNode.containsChild(null)); - assertTrue("containsChild", catNode.containsChild(fType1.getName())); - assertTrue("containsChild", catNode.containsChild(fType2.getName())); - assertFalse("containsChild", catNode.containsChild(null)); + assertTrue(catNode.containsChild(fTypeId1)); + assertTrue(catNode.containsChild(fTypeId2)); + assertFalse(catNode.containsChild(null)); } // ------------------------------------------------------------------------ @@ -141,86 +102,39 @@ public class TmfStatisticsTreeNodeTest extends TestCase { */ public void testGetChildren() { // Getting children of the ROOT - Collection childrenTreeNode = fStatsData.get(TmfStatisticsTree.ROOT[0]).getChildren(); - assertEquals("getChildren", 1, childrenTreeNode.size()); + Collection childrenTreeNode = fStatsTree.getRootNode().getChildren(); + assertEquals(1, childrenTreeNode.size()); TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", fTestName, treeNode.getKey()); + assertEquals(fTestName, treeNode.getName()); // Getting children of the trace - childrenTreeNode = fStatsData.get(fTestName).getChildren(); - assertEquals("getChildren", 1, childrenTreeNode.size()); + childrenTreeNode = fStatsTree.getNode(fTestName).getChildren(); + assertEquals(1, childrenTreeNode.size()); treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); + assertEquals(Messages.TmfStatisticsData_EventTypes, treeNode.getName()); Vector keyExpected = new Vector(); - keyExpected.add(fType1.getName()); - keyExpected.add(fType2.getName()); + keyExpected.add(fTypeId1); + keyExpected.add(fTypeId2); + keyExpected.add(fTypeId3); // Getting children of a category childrenTreeNode = treeNode.getChildren(); - assertEquals("getChildren", 2, childrenTreeNode.size()); + assertEquals(3, childrenTreeNode.size()); Iterator iterChild = childrenTreeNode.iterator(); TmfStatisticsTreeNode temp; while (iterChild.hasNext()) { temp = iterChild.next(); - if (keyExpected.contains(temp.getKey())) { - keyExpected.removeElement(temp.getKey()); + if (keyExpected.contains(temp.getName())) { + keyExpected.removeElement(temp.getName()); } else { fail(); } } // Get children of a specific event type - childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getChildren(); - assertEquals("getChildren", 0, childrenTreeNode.size()); - } - - // ------------------------------------------------------------------------ - // GetAllChildren - // ------------------------------------------------------------------------ - - /** - * Test getting of all children. - */ - public void testGetAllChildren() { - // Getting children of the ROOT - Collection childrenTreeNode = fStatsData.get(TmfStatisticsTree.ROOT).getAllChildren(); - assertEquals("getChildren", 1, childrenTreeNode.size()); - TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", fTestName, treeNode.getKey()); - - // Getting children of the trace - childrenTreeNode = fStatsData.get(fTestName).getAllChildren(); - assertEquals("getChildren", 1, childrenTreeNode.size()); - treeNode = childrenTreeNode.iterator().next(); - assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); - - Vector keyExpected = new Vector(); - keyExpected.add(fType1.getName()); - keyExpected.add(fType2.getName()); - /* - * It should return the eventType even though the number of events - * equals 0 - */ - fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()).reset(); - // Getting children of a category - childrenTreeNode = treeNode.getAllChildren(); - assertEquals("getChildren", 2, childrenTreeNode.size()); - - Iterator iterChild = childrenTreeNode.iterator(); - TmfStatisticsTreeNode temp; - while (iterChild.hasNext()) { - temp = iterChild.next(); - if (keyExpected.contains(temp.getKey())) { - keyExpected.removeElement(temp.getKey()); - } else { - fail(); - } - } - - // Get children of a specific event type - childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getAllChildren(); - assertEquals("getChildren", 0, childrenTreeNode.size()); + childrenTreeNode = fStatsTree.getNode(childrenTreeNode.iterator().next().getPath()).getChildren(); + assertEquals(0, childrenTreeNode.size()); } // ------------------------------------------------------------------------ @@ -231,15 +145,15 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test getting of number of children. */ public void testGetNbChildren() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); + TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode(); + TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName); TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode elementNode = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); + TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); - assertEquals("getNbChildren", 1, rootNode.getNbChildren()); - assertEquals("getNbChildren", 1, traceNode.getNbChildren()); - assertEquals("getNbChildren", 2, catNode.getNbChildren()); - assertEquals("getNbChildren", 0, elementNode.getNbChildren()); + assertEquals(1, rootNode.getNbChildren()); + assertEquals(1, traceNode.getNbChildren()); + assertEquals(3, catNode.getNbChildren()); + assertEquals(0, elementNode.getNbChildren()); } // ------------------------------------------------------------------------ @@ -250,15 +164,15 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test checking for children. */ public void testHasChildren() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); + TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode(); + TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName); TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode elementNode = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); + TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); - assertTrue("hasChildren", rootNode.hasChildren()); - assertTrue("hasChildren", traceNode.hasChildren()); - assertTrue("hasChildren", catNode.hasChildren()); - assertFalse("hasChildren", elementNode.hasChildren()); + assertTrue(rootNode.hasChildren()); + assertTrue(traceNode.hasChildren()); + assertTrue(catNode.hasChildren()); + assertFalse(elementNode.hasChildren()); } // ------------------------------------------------------------------------ @@ -269,35 +183,31 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test getting of parent. */ public void testGetParent() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); + final TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode(); TmfStatisticsTreeNode parentNode = rootNode.getParent(); - assertNull("getParent", parentNode); + assertNull(parentNode); - TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(fStatsData, "newly created trace node"); + TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(fStatsTree, rootNode, "newly created trace node"); //$NON-NLS-1$ parentNode = newTraceNode.getParent(); - assertNotNull("getParent", parentNode); - assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(TmfStatisticsTree.ROOT).getKey().toString())); + assertNotNull(parentNode); + assertTrue(fStatsTree.getRootNode() == parentNode); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); + TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName); parentNode = traceNode.getParent(); - assertNotNull("getParent", parentNode); - assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(TmfStatisticsTree.ROOT.toString())); + assertNotNull(parentNode); + assertTrue(rootNode == parentNode); - TmfStatisticsTreeNode newNode = new TmfStatisticsTreeNode(fStatsData, "TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist"); - parentNode = newNode.getParent(); - assertNull("getParent", parentNode); - - TmfStatisticsTreeNode elementNode = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); + TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); parentNode = elementNode.getParent(); - assertNull("getParent", parentNode); + assertTrue(parentNode == fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes)); TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); parentNode = catNode.getParent(); - assertNotNull("getParent", parentNode); - assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(fTestName).getPath().toString())); + assertNotNull(parentNode); + assertTrue(parentNode == fStatsTree.getNode(fTestName)); parentNode = elementNode.getParent(); - assertNotNull("getParent", parentNode); + assertNotNull(parentNode); assertTrue(arraysEqual(parentNode.getPath(), fTestName, Messages.TmfStatisticsData_EventTypes)); } @@ -308,16 +218,16 @@ public class TmfStatisticsTreeNodeTest extends TestCase { /** * Test getting of key. */ - public void testGetKey() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); + public void testgetName() { + TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode(); + TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName); TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode elementNode = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); + TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); - assertEquals("getKey", 0, rootNode.getKey().compareTo(TmfStatisticsTree.ROOT[0])); - assertEquals("getKey", 0, traceNode.getKey().compareTo(fTestName)); - assertEquals("getKey", 0, catNode.getKey().compareTo(Messages.TmfStatisticsData_EventTypes)); - assertEquals("getKey", 0, elementNode.getKey().compareTo(fType1.getName())); + assertEquals(0, rootNode.getName().compareTo("root")); //$NON-NLS-1$ + assertEquals(0, traceNode.getName().compareTo(fTestName)); + assertEquals(0, catNode.getName().compareTo(Messages.TmfStatisticsData_EventTypes)); + assertEquals(0, elementNode.getName().compareTo(fTypeId1)); } // ------------------------------------------------------------------------ @@ -328,17 +238,17 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test getting of path to node. */ public void testGetPath() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); + TmfStatisticsTreeNode rootNode = fStatsTree.getRootNode(); + TmfStatisticsTreeNode traceNode = fStatsTree.getNode(fTestName); TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode elementNode = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); + TmfStatisticsTreeNode elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); - assertTrue("getPath", arraysEqual(rootNode.getPath(), TmfStatisticsTree.ROOT)); - assertTrue("getPath", arraysEqual(traceNode.getPath(), fTestName)); - assertTrue("getPath", arraysEqual(catNode.getPath(), + assertEquals(0, rootNode.getPath().length); /* Root node has an empty path */ + assertTrue(arraysEqual(traceNode.getPath(), fTestName)); + assertTrue(arraysEqual(catNode.getPath(), fTestName, Messages.TmfStatisticsData_EventTypes)); - assertTrue("getPath", arraysEqual(elementNode.getPath(), - fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName())); + assertTrue(arraysEqual(elementNode.getPath(), + fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1)); } // ------------------------------------------------------------------------ @@ -349,23 +259,27 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test getting statistic value. */ public void testGetValue() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); - TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode elementNode1 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); - TmfStatisticsTreeNode elementNode3 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType2.getName()); - - assertEquals("getValue", 0, rootNode.getValues().getTotal()); - assertEquals("getValue", 9, traceNode.getValues().getTotal()); - assertEquals("getValue", 0, catNode.getValues().getTotal()); - assertEquals("getValue", 3, elementNode1.getValues().getTotal()); - assertEquals("getValue", 4, elementNode3.getValues().getTotal()); - - assertEquals("getValue", 0, rootNode.getValues().getPartial()); - assertEquals("getValue", 9, traceNode.getValues().getPartial()); - assertEquals("getValue", 0, catNode.getValues().getPartial()); - assertEquals("getValue", 3, elementNode1.getValues().getPartial()); - assertEquals("getValue", 4, elementNode3.getValues().getPartial()); + TmfStatisticsTreeNode rootNode, traceNode, catNode, elementNode1, elementNode2, elementNode3; + rootNode = fStatsTree.getRootNode(); + traceNode = fStatsTree.getNode(fTestName); + catNode = traceNode.getChildren().iterator().next(); + elementNode1 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); + elementNode2 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId2); + elementNode3 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId3); + + assertEquals(0, rootNode.getValues().getTotal()); + assertEquals(18, traceNode.getValues().getTotal()); + assertEquals(0, catNode.getValues().getTotal()); + assertEquals(5, elementNode1.getValues().getTotal()); + assertEquals(6, elementNode2.getValues().getTotal()); + assertEquals(7, elementNode3.getValues().getTotal()); + + assertEquals(0, rootNode.getValues().getPartial()); + assertEquals(9, traceNode.getValues().getPartial()); + assertEquals(0, catNode.getValues().getPartial()); + assertEquals(2, elementNode1.getValues().getPartial()); + assertEquals(3, elementNode2.getValues().getPartial()); + assertEquals(4, elementNode3.getValues().getPartial()); } // ------------------------------------------------------------------------ @@ -376,31 +290,31 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * Test reset of tree. */ public void testReset() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); - TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode elementNode = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); + TmfStatisticsTreeNode rootNode, traceNode, catNode, elementNode; + rootNode = fStatsTree.getRootNode(); + traceNode = fStatsTree.getNode(fTestName); + catNode = traceNode.getChildren().iterator().next(); + elementNode = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); elementNode.reset(); - assertEquals("reset", 0, elementNode.getValues().getTotal()); - assertEquals("reset", 0, elementNode.getValues().getPartial()); + assertEquals(0, elementNode.getValues().getTotal()); + assertEquals(0, elementNode.getValues().getPartial()); catNode.reset(); - assertEquals("reset", 0, catNode.getValues().getTotal()); - assertEquals("reset", 0, catNode.getValues().getPartial()); - assertEquals("reset", 0, catNode.getNbChildren()); - assertNull("reset", fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName())); + assertEquals(0, catNode.getValues().getTotal()); + assertEquals(0, catNode.getValues().getPartial()); + assertEquals(0, catNode.getNbChildren()); + assertNull(fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1)); traceNode.reset(); - assertEquals("reset", 0, traceNode.getValues().getTotal()); - assertEquals("reset", 0, traceNode.getValues().getPartial()); - // A trace always have at least one child that is eventType - assertEquals("reset", 1, traceNode.getNbChildren()); + assertEquals(0, traceNode.getValues().getTotal()); + assertEquals(0, traceNode.getValues().getPartial()); + assertEquals(0, traceNode.getNbChildren()); rootNode.reset(); - assertEquals("reset", 0, rootNode.getValues().getTotal()); - assertEquals("reset", 0, rootNode.getValues().getPartial()); - assertEquals("reset", 1, rootNode.getNbChildren()); + assertEquals(0, rootNode.getValues().getTotal()); + assertEquals(0, rootNode.getValues().getPartial()); + assertEquals(0, rootNode.getNbChildren()); } /** @@ -408,11 +322,13 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * the global value without removing any node from the tree. */ public void testResetGlobalValue() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); - TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode eventTypeNode1 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); - TmfStatisticsTreeNode eventTypeNode2 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType2.getName()); + TmfStatisticsTreeNode rootNode, traceNode, catNode, eventTypeNode1, eventTypeNode2, eventTypeNode3; + rootNode = fStatsTree.getRootNode(); + traceNode = fStatsTree.getNode(fTestName); + catNode = traceNode.getChildren().iterator().next(); + eventTypeNode1 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); + eventTypeNode2 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId2); + eventTypeNode3 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId3); rootNode.resetGlobalValue(); @@ -421,20 +337,22 @@ public class TmfStatisticsTreeNodeTest extends TestCase { assertEquals(0, catNode.getValues().getTotal()); assertEquals(0, eventTypeNode1.getValues().getTotal()); assertEquals(0, eventTypeNode2.getValues().getTotal()); + assertEquals(0, eventTypeNode3.getValues().getTotal()); // Checks the state of the statistics tree - Collection rootChildren = rootNode.getAllChildren(); + Collection rootChildren = rootNode.getChildren(); assertEquals(1, rootChildren.size()); assertTrue(rootChildren.contains(traceNode)); - Collection traceChildren = traceNode.getAllChildren(); + Collection traceChildren = traceNode.getChildren(); assertEquals(1, traceChildren.size()); assertTrue(traceChildren.contains(catNode)); - Collection catChildren = catNode.getAllChildren(); - assertEquals(2, catChildren.size()); + Collection catChildren = catNode.getChildren(); + assertEquals(3, catChildren.size()); assertTrue(catChildren.contains(eventTypeNode1)); assertTrue(catChildren.contains(eventTypeNode2)); + assertTrue(catChildren.contains(eventTypeNode3)); } /** @@ -442,11 +360,13 @@ public class TmfStatisticsTreeNodeTest extends TestCase { * the time range value without removing any node from the tree. */ public void testResetTimeRangeValue() { - TmfStatisticsTreeNode rootNode = fStatsData.get(TmfStatisticsTree.ROOT); - TmfStatisticsTreeNode traceNode = fStatsData.get(fTestName); - TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); - TmfStatisticsTreeNode eventTypeNode1 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getName()); - TmfStatisticsTreeNode eventTypeNode2 = fStatsData.get(fTestName, Messages.TmfStatisticsData_EventTypes, fType2.getName()); + TmfStatisticsTreeNode rootNode, traceNode, catNode, eventTypeNode1, eventTypeNode2, eventTypeNode3; + rootNode = fStatsTree.getRootNode(); + traceNode = fStatsTree.getNode(fTestName); + catNode = traceNode.getChildren().iterator().next(); + eventTypeNode1 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId1); + eventTypeNode2 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId2); + eventTypeNode3 = fStatsTree.getNode(fTestName, Messages.TmfStatisticsData_EventTypes, fTypeId3); rootNode.resetTimeRangeValue(); @@ -457,22 +377,24 @@ public class TmfStatisticsTreeNodeTest extends TestCase { assertEquals(0, eventTypeNode2.getValues().getPartial()); // Checks the state of the statistics tree - Collection rootChildren = rootNode.getAllChildren(); + Collection rootChildren = rootNode.getChildren(); assertEquals(1, rootChildren.size()); assertTrue(rootChildren.contains(traceNode)); - Collection traceChildren = traceNode.getAllChildren(); + Collection traceChildren = traceNode.getChildren(); assertEquals(1, traceChildren.size()); assertTrue(traceChildren.contains(catNode)); - Collection catChildren = catNode.getAllChildren(); - assertEquals(2, catChildren.size()); + Collection catChildren = catNode.getChildren(); + assertEquals(3, catChildren.size()); assertTrue(catChildren.contains(eventTypeNode1)); assertTrue(catChildren.contains(eventTypeNode2)); + assertTrue(catChildren.contains(eventTypeNode3)); } /** * Check if two String arrays are equals, by comparing their contents. + * Unlike Arrays.equals(), we can use varargs for the second argument. */ private static boolean arraysEqual(String[] array1, String... array2) { if (array1.length != array2.length) { diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java index 6296b5ff52..0fb60773c7 100755 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java @@ -105,7 +105,7 @@ public class TmfTreeContentProviderTest extends TestCase { * but it could be rewritten to be much more simple... */ public void testGetChildren() { - Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes)); + Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes)); TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]); String[][] childrenExpected = new String[][] { @@ -151,10 +151,10 @@ public class TmfTreeContentProviderTest extends TestCase { * Test getting of parent. */ public void testGetParent() { - TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.get(fTestName)); + TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.getNode(fTestName)); assertNotNull("getParent", parent); - assertTrue("getParent", parent.getPath().equals(TmfStatisticsTree.ROOT)); + assertTrue("getParent", parent.getPath().equals(fStatsData.getRootNode().getPath())); } // ------------------------------------------------------------------------ @@ -164,16 +164,16 @@ public class TmfTreeContentProviderTest extends TestCase { * Test checking for children. */ public void testHasChildren() { - Boolean hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(TmfStatisticsTree.ROOT)); + Boolean hasChildren = treeProvider.hasChildren(fStatsData.getRootNode()); assertTrue("hasChildren", hasChildren); - hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(fTestName)); + hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName)); assertTrue("hasChildren", hasChildren); - hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes)); + hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes)); assertTrue("hasChildren", hasChildren); - hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName())); + hasChildren = treeProvider.hasChildren(fStatsData.getOrCreateNode(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().getName())); assertFalse("hasChildren", hasChildren); } @@ -185,7 +185,7 @@ public class TmfTreeContentProviderTest extends TestCase { * Test getting of elements. */ public void testGetElements() { - Object[] objectElements = treeProvider.getElements(fStatsData.get(TmfStatisticsTree.ROOT)); + Object[] objectElements = treeProvider.getElements(fStatsData.getRootNode()); TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]); assertEquals("getElements", 1, nodeElements.length); assertTrue("getElements", nodeElements[0].getPath()[0].equals(fTestName)); diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfBaseColumnDataProvider.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfBaseColumnDataProvider.java index e2eeb80727..004161b813 100755 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfBaseColumnDataProvider.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfBaseColumnDataProvider.java @@ -86,13 +86,13 @@ public class TmfBaseColumnDataProvider implements ITmfColumnDataProvider { fColumnData.add(new TmfBaseColumnData(LEVEL_COLUMN, 200, SWT.LEFT, LEVEL_COLUMN_TIP, new ColumnLabelProvider() { @Override public String getText(Object element) { - return ((TmfStatisticsTreeNode) element).getKey(); + return ((TmfStatisticsTreeNode) element).getName(); } @Override public Image getImage(Object element) { TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element; - if (fFolderLevels.contains(node.getKey())) { + if (fFolderLevels.contains(node.getName())) { return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); } return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); @@ -103,7 +103,7 @@ public class TmfBaseColumnDataProvider implements ITmfColumnDataProvider { TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1; TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2; - return n1.getKey().compareTo(n2.getKey()); + return n1.getName().compareTo(n2.getName()); } }, null)); @@ -112,7 +112,7 @@ public class TmfBaseColumnDataProvider implements ITmfColumnDataProvider { @Override public String getText(Object element) { TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element; - if (!fFolderLevels.contains(node.getKey())) { + if (!fFolderLevels.contains(node.getName())) { return Long.toString(node.getValues().getTotal()); } return ""; //$NON-NLS-1$ @@ -147,7 +147,7 @@ public class TmfBaseColumnDataProvider implements ITmfColumnDataProvider { @Override public String getText(Object element) { TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element; - if (!fFolderLevels.contains(node.getKey())) { + if (!fFolderLevels.contains(node.getName())) { return Long.toString(node.getValues().getPartial()); } return ""; //$NON-NLS-1$ diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTree.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTree.java index 8153b9bfbb..4d894b8114 100755 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTree.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTree.java @@ -8,18 +8,12 @@ * * Contributors: * Mathieu Denis - Implementation and Initial API - * + * Alexandre Montplaisir - Merge TmfBaseStatisticsTree and AbsStatisticsTree + * Move the tree structure logic into the nodes *******************************************************************************/ package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; /** * Base class for the statistics storage. It allow to implement a tree structure @@ -32,168 +26,46 @@ import java.util.Set; */ public class TmfStatisticsTree { - /** - * Identification of the root. - */ - public static final String[] ROOT = new String[] { "root" }; //$NON-NLS-1$ - - /** - * Header for the event type categories. - */ + /** Header for the event type categories. */ public static final String HEADER_EVENT_TYPES = Messages.TmfStatisticsData_EventTypes; - /** - * Indicate that it's a value. - * - * Used when checking the possible child node for a node. - * - * It differentiate a category of a value by being appended to a value. - */ - protected static final String NODE = "z"; //$NON-NLS-1$ - - /** - * Root node key. - */ - protected static final String ROOT_NODE_KEY = mergeString(ROOT[0], NODE); - - /** - * Define what children a node can have. The management and usage of this map - * is done by subclasses. HashSet are always faster than TreeSet for String keys. - */ - protected Map> fKeys; - - /** - * The nodes in the tree. - */ - protected Map, TmfStatisticsTreeNode> fNodes; + /** Root node of this tree */ + private final TmfStatisticsTreeNode rootNode; /** * Default constructor. Creates base statistics tree for counting total * number of events and number of events per event type. */ public TmfStatisticsTree() { - fNodes = new HashMap, TmfStatisticsTreeNode>(); - fKeys = new HashMap>(); - - Map> keys = getKeys(); - - // //////////// Adding category sets - // common - keys.put(HEADER_EVENT_TYPES, new HashSet()); - - // /////////// Adding value sets - // Under a trace - Set temp = new HashSet(8); - temp.add(HEADER_EVENT_TYPES); - keys.put(ROOT_NODE_KEY, temp); - // Under an event type - temp = new HashSet(16); - keys.put(mergeString(HEADER_EVENT_TYPES, NODE), temp); - - // //////////// CREATE root - keys.put(ROOT[0], new HashSet(2)); // 1 trace at the time - getOrCreate(ROOT); - } - - /** - * Get a node. - * - * @param path - * Path to the node. - * @return The node or null. - */ - public TmfStatisticsTreeNode get(String... path) { - List pathAsList = Arrays.asList(path); - return fNodes.get(pathAsList); + rootNode = new TmfStatisticsTreeNode(this, null, new String[0]); } /** - * Get the children of a node. + * Retrieve the root node of this tree. * - * @param path - * Path to the node. - * @return Collection containing the children. + * @return The root node */ - public List getChildren(String... path) { - List result = new LinkedList(); - - if (path.length % 2 == 0) { // if we are at a Category - TmfStatisticsTreeNode current = null; - for (String value : getKeys().get(path[path.length - 1])) { - current = get(addToArray(path, value)); - if (current != null) { - if (current.getValues().getTotal() > 0 || current.getValues().getPartial() > 0) { - result.add(current); - } - } - } - } else if (path.length == 1) { // Special case. - if (path.equals(ROOT)) { - for (String value : getKeys().get(ROOT[0])) { - result.add(getOrCreate(value)); - } - } else { - // Get value under the root - for (String value : getKeys().get(ROOT_NODE_KEY)) { - result.add(getOrCreate(addToArray(path, value))); - } - } - } else {// If we are at a value - for (String value : getKeys().get(mergeString(path[path.length - 2], NODE))) { - // Search the parent name + NODE - result.add(getOrCreate(addToArray(path, value))); - } - } - - return result; + public TmfStatisticsTreeNode getRootNode() { + return rootNode; } /** - * Get every children of a node, even if it doesn't have any registered - * events, as opposed to getChildren + * Get a node. * * @param path * Path to the node. - * @return Collection containing all the children. - */ - public List getAllChildren(String... path) { - LinkedList result = new LinkedList(); - - if (path.length % 2 == 0) { // if we are at a Category - TmfStatisticsTreeNode current = null; - for (String value : getKeys().get(path[path.length - 1])) { - current = get(addToArray(path, value)); - if (current != null) { - result.add(current); - } - } - } else if (path.length == 1) { // Special case. - if (path.equals(ROOT)) { - for (String value : getKeys().get(ROOT[0])) { - result.add(getOrCreate(value)); - } - } else { - // Get value under the root - for (String value : getKeys().get(ROOT_NODE_KEY)) { - result.add(getOrCreate(addToArray(path, value))); - } - } - } else {// If we are at a value - for (String value : getKeys().get(mergeString(path[path.length - 2], NODE))) { - // Search the parent name + NODE - result.add(getOrCreate(addToArray(path, value))); + * @return The node, or null if it doesn't current exist in the tree. + */ + public TmfStatisticsTreeNode getNode(String... path) { + TmfStatisticsTreeNode curNode = rootNode; + for (String pathElem : path) { + curNode = curNode.getChild(pathElem); + if (curNode == null) { + /* The requested path doesn't exist, return null */ + break; } } - return result; - } - - /** - * Get the map of existing elements of path classified by parent. - * - * @return The map. - */ - public Map> getKeys() { - return fKeys; + return curNode; } /** @@ -201,38 +73,19 @@ public class TmfStatisticsTree { * * @param path * Path to the node. - * @return The node. - */ - public TmfStatisticsTreeNode getOrCreate(String... path) { - List pathAsList = Arrays.asList(path); - TmfStatisticsTreeNode current = fNodes.get(pathAsList); - - if (current == null) { - registerName(path); - current = new TmfStatisticsTreeNode(this, path); - fNodes.put(pathAsList, current); - } - return current; - } - - /** - * Get the parent of a node. - * - * @param path - * Path to the node. - * @return Parent node or null. - */ - public TmfStatisticsTreeNode getParent(final String... path) { - if (path.length == 1) { - if (path.equals(ROOT)) { - return null; + * @return The requested node. Will be created if it didn't exist. + */ + public TmfStatisticsTreeNode getOrCreateNode(String... path) { + TmfStatisticsTreeNode curNode = rootNode; + TmfStatisticsTreeNode nextNode; + for (String pathElem : path) { + nextNode = curNode.getChild(pathElem); + if (nextNode == null) { + nextNode = curNode.addChild(pathElem); } - return get(ROOT); + curNode = nextNode; } - - String[] parentPath = new String[path.length - 1]; - System.arraycopy(path, 0, parentPath, 0, parentPath.length); - return get(parentPath); + return curNode; } /** @@ -250,7 +103,7 @@ public class TmfStatisticsTree { public void setTotal(String traceName, boolean isGlobal, long qty) { String[][] paths = getNormalPaths(traceName); for (String path[] : paths) { - getOrCreate(path).getValues().setValue(isGlobal, qty); + getOrCreateNode(path).getValues().setValue(isGlobal, qty); } } @@ -271,7 +124,7 @@ public class TmfStatisticsTree { public void setTypeCount(String traceName, String type, boolean isGlobal, long qty) { String[][] paths = getTypePaths(traceName, type); for (String[] path : paths) { - getOrCreate(path).getValues().setValue(isGlobal, qty); + getOrCreateNode(path).getValues().setValue(isGlobal, qty); } } @@ -303,71 +156,6 @@ public class TmfStatisticsTree { return paths; } - /** - * Register that a new node was created. - * - * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node - * will return the newly created node. - * - * @param path - * Path of the new node. - */ - protected void registerName(String... path) { - if (path.length == 1) { - if (!path.equals(ROOT)) { - getKeys().get(ROOT[0]).add(path[0]); - } - } else if (path.length % 2 != 0) { - getKeys().get(path[path.length - 2]).add(path[path.length - 1]); - } - } - - /** - * Resets a node. - * - * Works recursively. - * - * @param path - * Path to the node. - */ - public void reset(final String... path) { - for (TmfStatisticsTreeNode node : getAllChildren(path)) { - reset(node.getPath()); - List nodePathList = Arrays.asList(node.getPath()); - fNodes.remove(nodePathList); - } - } - - /** - * Reset the global value of a node. - * - * Works recursively. - * - * @param path - * Path to the node. - * @since 2.0 - */ - public void resetGlobalValue(final String... path) { - for (TmfStatisticsTreeNode node : getChildren(path)) { - node.resetGlobalValue(); - } - } - - /** - * Reset the time range value of a node. - * - * Works recursively. - * - * @param path - * Path to the node. - * @since 2.0 - */ - public void resetTimeRangeValue(final String... path) { - for (TmfStatisticsTreeNode node : getChildren(path)) { - node.resetTimeRangeValue(); - } - } - /** * Function to merge many string more efficiently. * @@ -382,15 +170,4 @@ public class TmfStatisticsTree { } return builder.toString(); } - - /** - * Return a new array that's a copy of the old one, plus 'newElem' added at - * the end. - */ - private static String[] addToArray(String[] array, String newElem) { - String[] newArray = new String[array.length + 1]; - System.arraycopy(array, 0, newArray, 0, array.length); - newArray[array.length] = newElem; - return newArray; - } } diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeManager.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeManager.java index a85e600022..1de14e585b 100755 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeManager.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeManager.java @@ -22,9 +22,9 @@ import java.util.Map; * root node is created for each tree. Using the tree node ID the statistics * tree can be retrieved. * + * @author Mathieu Denis * @version 2.0 * @since 2.0 - * @author Mathieu Denis */ public class TmfStatisticsTreeManager { @@ -46,7 +46,7 @@ public class TmfStatisticsTreeManager { if (tree == null) { return null; } - return tree.getOrCreate(TmfStatisticsTree.ROOT); + return tree.getRootNode(); } /** @@ -83,8 +83,7 @@ public class TmfStatisticsTreeManager { return null; } fTreeInstances.put(traceUniqueId, statsData); - // if called for the first time, create the root node - return statsData.getOrCreate(TmfStatisticsTree.ROOT); + return statsData.getRootNode(); } /** diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeNode.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeNode.java index bbc57c52da..2c7f02b911 100755 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeNode.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsTreeNode.java @@ -10,11 +10,15 @@ * Yann N. Dauphin - Implementation for stats * Francois Godin - Re-design for new stats structure * Mathieu Denis - Re-design for new stats structure (2) + * Alexandre Montplaisir - Move the tree structure logic into the nodes *******************************************************************************/ package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model; +import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; /** * A tree where nodes can be accessed efficiently using paths. @@ -23,85 +27,115 @@ import java.util.Collection; * array of String. The elements of the array represent the path from the root * to this node. * + * @author Mathieu Denis * @version 2.0 * @since 2.0 - * @author Mathieu Denis */ public class TmfStatisticsTreeNode { - /** - * Value of the node. - */ - protected TmfStatisticsValues fValues; + /** Tree to which this node belongs */ + private final TmfStatisticsTree fTree; - /** - * Path of the node. - */ - protected String[] fPath; + /** Path of this node. The last element represents its basename. */ + private final String[] fPath; - /** - * Corresponding StatisticsData. - */ - protected TmfStatisticsTree fNodes; + /** Parent node */ + private final TmfStatisticsTreeNode fParent; + + /** Children of this node, indexed by their basename. */ + private final Map fChildren; + + /** Statistics values associated to this node. */ + private final TmfStatisticsValues fValues; /** * Constructor. * + * @param tree + * Owner tree of this node + * @param parent + * Parent node of this one * @param path * Path to the node. - * @param nodes - * Corresponding StatisticsData. */ - public TmfStatisticsTreeNode(TmfStatisticsTree nodes, final String... path) { + public TmfStatisticsTreeNode(TmfStatisticsTree tree, + TmfStatisticsTreeNode parent, final String... path) { + /* The path must not contain any null element, or else we won't be + * able to walk the tree. */ + for (String elem : path) { + if (elem == null) { + throw new IllegalArgumentException(); + } + } + + fTree = tree; fPath = path; - fNodes = nodes; + fParent = parent; + fChildren = new HashMap(); fValues = new TmfStatisticsValues(); } + /** + * Get the name for this node. It's used as the key in the parent's node. + * + * @return Name of this node. + */ + public String getName() { + if (fPath.length == 0) { + /* This means we are the root node, which has no path itself */ + return "root"; //$NON-NLS-1$ + } + return fPath[fPath.length - 1]; + } + /** * Test if a node contain the specified child. * - * @param key + * @param childName * Name of the child. * @return true: if child with given key is present, false: if no child * exists with given key name */ - public boolean containsChild(String key) { - if (TmfStatisticsTree.ROOT.equals(fPath)) { - return fNodes.get(key) != null; - } - - String[] childPath = new String[fPath.length + 1]; - System.arraycopy(fPath, 0, childPath, 0, fPath.length); - childPath[fPath.length] = key; - return (fNodes.get(childPath) != null); + public boolean containsChild(String childName) { + return fChildren.containsKey(childName); } /** - * Get the children of this node. + * Retrieve the given child from this node. * - * @return Direct children of this node. + * @param childName + * The (base)name of the child you want + * @return The child object, or null if it doesn't exist */ - public Collection getChildren() { - return fNodes.getChildren(fPath); + public TmfStatisticsTreeNode getChild(String childName) { + return fChildren.get(childName); } /** - * Gets every children of this node even if no event has been registered for a node. + * Get the children of this node. * * @return Direct children of this node. */ - public Collection getAllChildren() { - return fNodes.getAllChildren(fPath); + public Collection getChildren() { + return fChildren.values(); } /** - * Get the key for this node. + * Add a child to this node. * - * @return Key associated with this node. + * @param childName + * Name of the child to add + * @return The newly-created child */ - public String getKey() { - return fPath[fPath.length - 1]; + public TmfStatisticsTreeNode addChild(String childName) { + TmfStatisticsTreeNode child; + String[] childPath = new String[fPath.length + 1]; + System.arraycopy(fPath, 0, childPath, 0, fPath.length); + childPath[fPath.length] = childName; + + child = new TmfStatisticsTreeNode(this.fTree, this, childPath); + fChildren.put(childName, child); + return child; } /** @@ -110,7 +144,7 @@ public class TmfStatisticsTreeNode { * @return Number of direct children of this node. */ public int getNbChildren() { - return fNodes.getChildren(fPath).size(); + return fChildren.size(); } /** @@ -119,7 +153,7 @@ public class TmfStatisticsTreeNode { * @return Parent node. */ public TmfStatisticsTreeNode getParent() { - return fNodes.getParent(fPath); + return fParent; } /** @@ -146,7 +180,7 @@ public class TmfStatisticsTreeNode { * @return True if the node has children. */ public boolean hasChildren() { - return !fNodes.getChildren(fPath).isEmpty(); + return (fChildren.size() > 0); } /** @@ -154,33 +188,41 @@ public class TmfStatisticsTreeNode { * no children. */ public void reset() { - fValues = new TmfStatisticsValues(); - fNodes.reset(fPath); + fValues.resetTotalCount(); + fValues.resetPartialCount(); + fChildren.clear(); } /** * Resets the global number of events. It doesn't remove any node - * and doesn't modify the partial event count. - * - * Works recursively. + * and doesn't modify the partial event count. Works recursively. * * @since 2.0 */ public void resetGlobalValue() { - getValues().resetTotalCount(); - fNodes.resetGlobalValue(fPath); + for (TmfStatisticsTreeNode child : fChildren.values()) { + child.resetGlobalValue(); + } + fValues.resetTotalCount(); } /** * Resets the number of events in the time range. It doesn't remove any node - * and doesn't modify the global event count. - * - * Works recursively. + * and doesn't modify the global event count. Works recursively. * * @since 2.0 */ public void resetTimeRangeValue() { - getValues().resetPartialCount(); - fNodes.resetTimeRangeValue(fPath); + for (TmfStatisticsTreeNode child : fChildren.values()) { + child.resetTimeRangeValue(); + } + fValues.resetPartialCount(); + } + + @Override + public String toString() { + /* Used for debugging only */ + return "Stats node, path = " + Arrays.toString(fPath) + //$NON-NLS-1$ + ", values = " + fValues.toString(); //$NON-NLS-1$ } } diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsValues.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsValues.java index 2c3d6deefc..b1fa4d0c46 100755 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsValues.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/viewers/statistics/model/TmfStatisticsValues.java @@ -82,4 +82,9 @@ public class TmfStatisticsValues { public void resetPartialCount() { fNbEventsInTimeRange = 0; } + + @Override + public String toString() { + return fNbEvents + ", " + fNbEventsInTimeRange; //$NON-NLS-1$ + } }