ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / statistics / model / TmfStatisticsTree.java
CommitLineData
79e08fd0 1/*******************************************************************************
c8422608 2 * Copyright (c) 2011, 2013 Ericsson
013a5f1c 3 *
79e08fd0
BH
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
013a5f1c 8 *
79e08fd0 9 * Contributors:
36033ff0 10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
7588c810
AM
11 * Alexandre Montplaisir - Merge TmfBaseStatisticsTree and AbsStatisticsTree
12 * Move the tree structure logic into the nodes
79e08fd0
BH
13 *******************************************************************************/
14
cfd22ad0 15package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model;
79e08fd0 16
79e08fd0 17
79e08fd0 18/**
36033ff0
AM
19 * Base class for the statistics storage. It allow to implement a tree structure
20 * while avoiding the need to run through the tree each time you need to add a
21 * node at a given place.
013a5f1c 22 *
b544077e 23 * @author Mathieu Denis
36033ff0 24 * @version 2.0
cfd22ad0 25 * @since 2.0
79e08fd0 26 */
36033ff0
AM
27public class TmfStatisticsTree {
28
7588c810 29 /** Header for the event type categories. */
66711dc8 30 public static final String HEADER_EVENT_TYPES = Messages.TmfStatisticsData_EventTypes;
79e08fd0 31
7588c810
AM
32 /** Root node of this tree */
33 private final TmfStatisticsTreeNode rootNode;
36033ff0 34
b544077e
BH
35 /**
36 * Default constructor. Creates base statistics tree for counting total
37 * number of events and number of events per event type.
38 */
36033ff0 39 public TmfStatisticsTree() {
7588c810 40 rootNode = new TmfStatisticsTreeNode(this, null, new String[0]);
36033ff0
AM
41 }
42
43 /**
7588c810 44 * Retrieve the root node of this tree.
09667aa4 45 *
7588c810 46 * @return The root node
79e08fd0 47 */
7588c810
AM
48 public TmfStatisticsTreeNode getRootNode() {
49 return rootNode;
79e08fd0
BH
50 }
51
36033ff0 52 /**
7588c810 53 * Get a node.
09667aa4 54 *
36033ff0
AM
55 * @param path
56 * Path to the node.
7588c810
AM
57 * @return The node, or null if it doesn't current exist in the tree.
58 */
59 public TmfStatisticsTreeNode getNode(String... path) {
60 TmfStatisticsTreeNode curNode = rootNode;
61 for (String pathElem : path) {
62 curNode = curNode.getChild(pathElem);
63 if (curNode == null) {
64 /* The requested path doesn't exist, return null */
65 break;
013a5f1c 66 }
79e08fd0 67 }
7588c810 68 return curNode;
36033ff0
AM
69 }
70
71 /**
72 * Get or create a node.
73 *
74 * @param path
75 * Path to the node.
7588c810
AM
76 * @return The requested node. Will be created if it didn't exist.
77 */
78 public TmfStatisticsTreeNode getOrCreateNode(String... path) {
79 TmfStatisticsTreeNode curNode = rootNode;
80 TmfStatisticsTreeNode nextNode;
81 for (String pathElem : path) {
82 nextNode = curNode.getChild(pathElem);
83 if (nextNode == null) {
84 nextNode = curNode.addChild(pathElem);
36033ff0 85 }
7588c810 86 curNode = nextNode;
36033ff0 87 }
7588c810 88 return curNode;
36033ff0
AM
89 }
90
91 /**
92 * Set the value to display in the "total" cells. This means the row
93 * indicating the total count of events for a trace.
94 *
95 * @param traceName
96 * The name of the trace (will be used as a sub-tree in the view)
97 * @param isGlobal
98 * Is this a for a global or a time range request? Determines if
99 * this goes in the Global column or the Selected Time Range one.
100 * @param qty
101 * The value to display
102 */
89c06060 103 public void setTotal(String traceName, boolean isGlobal, long qty) {
5673a177
AM
104 String[][] paths = getNormalPaths(traceName);
105 for (String path[] : paths) {
7588c810 106 getOrCreateNode(path).getValues().setValue(isGlobal, qty);
73fbf6be 107 }
25a042b3
MD
108 }
109
36033ff0
AM
110 /**
111 * Set the value to display in the "Type count" cells. These are the counts
112 * for each event types.
113 *
114 * @param traceName
115 * The name of the trace (will be used as a sub-tree in the view)
116 * @param type
117 * The event type
118 * @param isGlobal
119 * Is this a for a global or a time range request? Determines if
120 * this goes in the Global column or the Selected Time Range one.
121 * @param qty
122 * The value to display
123 */
89c06060 124 public void setTypeCount(String traceName, String type, boolean isGlobal, long qty) {
5673a177
AM
125 String[][] paths = getTypePaths(traceName, type);
126 for (String[] path : paths) {
7588c810 127 getOrCreateNode(path).getValues().setValue(isGlobal, qty);
25a042b3
MD
128 }
129 }
130
79e08fd0 131 /**
09667aa4 132 * Get the event types paths.
013a5f1c 133 *
a0a88f65
AM
134 * @param traceName
135 * The name of the trace (will be used as a sub-tree in the view)
136 * @param type
137 * The event type
138 * @return Array of arrays representing the paths
79e08fd0 139 */
5673a177
AM
140 protected String[][] getTypePaths(String traceName, String type) {
141 String[][] paths = { new String[] {traceName, HEADER_EVENT_TYPES, type } };
79e08fd0
BH
142 return paths;
143 }
144
145 /**
09667aa4 146 * Get the standard paths for an event.
013a5f1c 147 *
a0a88f65
AM
148 * @param traceName
149 * The name of the trace (will be used as a sub-tree in the view)
150 * @return Array of arrays representing the paths
79e08fd0 151 */
5673a177
AM
152 protected String[][] getNormalPaths(String traceName) {
153 String[][] paths = { new String[] { traceName } };
79e08fd0
BH
154 return paths;
155 }
013a5f1c 156
36033ff0
AM
157 /**
158 * Function to merge many string more efficiently.
159 *
160 * @param strings
161 * Strings to merge.
162 * @return A new string containing all the strings.
163 */
164 protected static String mergeString(String... strings) {
165 StringBuilder builder = new StringBuilder();
166 for (String s : strings) {
167 builder.append(s);
168 }
169 return builder.toString();
170 }
79e08fd0 171}
This page took 0.063297 seconds and 5 git commands to generate.