tmf: Drop the use of TmfFixedArray for statistics
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / statistics / model / AbsTmfStatisticsTree.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
11 *
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model;
15
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
23 /**
24 * Base class for the statistics storage. It allow to implement a tree structure
25 * while avoiding the need to run through the tree each time you need to add a
26 * node at a given place.
27 *
28 * @version 2.0
29 * @author Mathieu Denis
30 * @since 2.0
31 */
32 public abstract class AbsTmfStatisticsTree {
33
34 /**
35 * String builder used to merge string more efficiently.
36 */
37 protected static final StringBuilder fBuilder = new StringBuilder();
38
39 /**
40 * Identification of the root.
41 */
42 public static final String[] ROOT = new String[] { "root" }; //$NON-NLS-1$
43
44 /**
45 * Function to merge many string more efficiently.
46 *
47 * @param strings
48 * Strings to merge.
49 * @return A new string containing all the strings.
50 */
51 public synchronized static String mergeString(String... strings) {
52 fBuilder.setLength(0);
53 for (String s : strings) {
54 fBuilder.append(s);
55 }
56 return fBuilder.toString();
57 }
58
59 /**
60 * Define what children a node can have. The management and usage of this map
61 * is done by subclasses. HashSet are always faster than TreeSet for String keys.
62 */
63 protected Map<String, Set<String>> fKeys;
64
65 /**
66 * The nodes in the tree.
67 */
68 protected Map<List<String>, TmfStatisticsTreeNode> fNodes;
69
70 /**
71 * Constructor.
72 */
73 public AbsTmfStatisticsTree() {
74 fNodes = new HashMap<List<String>, TmfStatisticsTreeNode>();
75 fKeys = new HashMap<String, Set<String>>();
76 }
77
78 /**
79 * Get a node.
80 *
81 * @param path
82 * Path to the node.
83 * @return The node or null.
84 */
85 public TmfStatisticsTreeNode get(String... path) {
86 List<String> pathAsList = Arrays.asList(path);
87 return fNodes.get(pathAsList);
88 }
89
90 /**
91 * Get the children of a node.
92 *
93 * @param path
94 * Path to the node.
95 * @return Collection containing the children.
96 */
97 public abstract Collection<TmfStatisticsTreeNode> getChildren(final String... path);
98
99 /**
100 * Get every children of a node, even if it doesn't have any registered
101 * events, as opposed to getChildren
102 *
103 * @param path
104 * Path to the node.
105 * @return Collection containing all the children.
106 */
107 public abstract Collection<TmfStatisticsTreeNode> getAllChildren(final String... path);
108
109 /**
110 * Get the map of existing elements of path classified by parent.
111 *
112 * @return The map.
113 */
114 public Map<String, Set<String>> getKeys() {
115 return fKeys;
116 }
117
118 /**
119 * Get or create a node.
120 *
121 * @param path
122 * Path to the node.
123 * @return The node.
124 */
125 public TmfStatisticsTreeNode getOrCreate(String... path) {
126 List<String> pathAsList = Arrays.asList(path);
127 TmfStatisticsTreeNode current = fNodes.get(pathAsList);
128
129 if (current == null) {
130 registerName(path);
131 current = new TmfStatisticsTreeNode(this, path);
132 fNodes.put(pathAsList, current);
133 }
134 return current;
135 }
136
137 /**
138 * Get the parent of a node.
139 *
140 * @param path
141 * Path to the node.
142 * @return Parent node or null.
143 */
144 public TmfStatisticsTreeNode getParent(final String... path) {
145 if (path.length == 1) {
146 if (path.equals(ROOT)) {
147 return null;
148 }
149 return get(ROOT);
150 }
151
152 String[] parentPath = new String[path.length - 1];
153 System.arraycopy(path, 0, parentPath, 0, parentPath.length);
154 return get(parentPath);
155 }
156
157 /**
158 * Set the value to display in the "total" cells. This means the row
159 * indicating the total count of events for a trace.
160 *
161 * @param traceName
162 * The name of the trace (will be used as a sub-tree in the view)
163 * @param isGlobal
164 * Is this a for a global or a time range request? Determines if
165 * this goes in the Global column or the Selected Time Range one.
166 * @param qty
167 * The value to display
168 */
169 public abstract void setTotal(String traceName, boolean isGlobal, long qty);
170
171 /**
172 * Set the value to display in the "Type count" cells. These are the counts
173 * for each event types.
174 *
175 * @param traceName
176 * The name of the trace (will be used as a sub-tree in the view)
177 * @param type
178 * The event type
179 * @param isGlobal
180 * Is this a for a global or a time range request? Determines if
181 * this goes in the Global column or the Selected Time Range one.
182 * @param qty
183 * The value to display
184 */
185 public abstract void setTypeCount(String traceName, String type,
186 boolean isGlobal, long qty);
187
188 /**
189 * Register that a new node was created.
190 *
191 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
192 * will return the newly created node.
193 *
194 * @param path
195 * Path of the new node.
196 */
197 protected abstract void registerName(final String... path);
198
199 /**
200 * Resets a node.
201 *
202 * Works recursively.
203 *
204 * @param path
205 * Path to the node.
206 */
207 public void reset(final String... path) {
208 for (TmfStatisticsTreeNode node : getAllChildren(path)) {
209 reset(node.getPath());
210 List<String> nodePathList = Arrays.asList(node.getPath());
211 fNodes.remove(nodePathList);
212 }
213 }
214
215 /**
216 * Reset the global value of a node.
217 *
218 * Works recursively.
219 *
220 * @param path
221 * Path to the node.
222 * @since 2.0
223 */
224 public void resetGlobalValue(final String... path) {
225 for (TmfStatisticsTreeNode node : getChildren(path)) {
226 node.resetGlobalValue();
227 }
228 }
229
230 /**
231 * Reset the time range value of a node.
232 *
233 * Works recursively.
234 *
235 * @param path
236 * Path to the node.
237 * @since 2.0
238 */
239 public void resetTimeRangeValue(final String... path) {
240 for (TmfStatisticsTreeNode node : getChildren(path)) {
241 node.resetTimeRangeValue();
242 }
243 }
244 }
This page took 0.035884 seconds and 5 git commands to generate.