1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
;
16 import java
.util
.Arrays
;
17 import java
.util
.Collection
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
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.
29 * @author Mathieu Denis
32 public abstract class AbsTmfStatisticsTree
{
35 * String builder used to merge string more efficiently.
37 protected static final StringBuilder fBuilder
= new StringBuilder();
40 * Identification of the root.
42 public static final String
[] ROOT
= new String
[] { "root" }; //$NON-NLS-1$
45 * Function to merge many string more efficiently.
49 * @return A new string containing all the strings.
51 public synchronized static String
mergeString(String
... strings
) {
52 fBuilder
.setLength(0);
53 for (String s
: strings
) {
56 return fBuilder
.toString();
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.
63 protected Map
<String
, Set
<String
>> fKeys
;
66 * The nodes in the tree.
68 protected Map
<List
<String
>, TmfStatisticsTreeNode
> fNodes
;
73 public AbsTmfStatisticsTree() {
74 fNodes
= new HashMap
<List
<String
>, TmfStatisticsTreeNode
>();
75 fKeys
= new HashMap
<String
, Set
<String
>>();
83 * @return The node or null.
85 public TmfStatisticsTreeNode
get(String
... path
) {
86 List
<String
> pathAsList
= Arrays
.asList(path
);
87 return fNodes
.get(pathAsList
);
91 * Get the children of a node.
95 * @return Collection containing the children.
97 public abstract Collection
<TmfStatisticsTreeNode
> getChildren(final String
... path
);
100 * Get every children of a node, even if it doesn't have any registered
101 * events, as opposed to getChildren
105 * @return Collection containing all the children.
107 public abstract Collection
<TmfStatisticsTreeNode
> getAllChildren(final String
... path
);
110 * Get the map of existing elements of path classified by parent.
114 public Map
<String
, Set
<String
>> getKeys() {
119 * Get or create a node.
125 public TmfStatisticsTreeNode
getOrCreate(String
... path
) {
126 List
<String
> pathAsList
= Arrays
.asList(path
);
127 TmfStatisticsTreeNode current
= fNodes
.get(pathAsList
);
129 if (current
== null) {
131 current
= new TmfStatisticsTreeNode(this, path
);
132 fNodes
.put(pathAsList
, current
);
138 * Get the parent of a node.
142 * @return Parent node or null.
144 public TmfStatisticsTreeNode
getParent(final String
... path
) {
145 if (path
.length
== 1) {
146 if (path
.equals(ROOT
)) {
152 String
[] parentPath
= new String
[path
.length
- 1];
153 System
.arraycopy(path
, 0, parentPath
, 0, parentPath
.length
);
154 return get(parentPath
);
158 * Set the value to display in the "total" cells. This means the row
159 * indicating the total count of events for a trace.
162 * The name of the trace (will be used as a sub-tree in the view)
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.
167 * The value to display
169 public abstract void setTotal(String traceName
, boolean isGlobal
, long qty
);
172 * Set the value to display in the "Type count" cells. These are the counts
173 * for each event types.
176 * The name of the trace (will be used as a sub-tree in the view)
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.
183 * The value to display
185 public abstract void setTypeCount(String traceName
, String type
,
186 boolean isGlobal
, long qty
);
189 * Register that a new node was created.
191 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
192 * will return the newly created node.
195 * Path of the new node.
197 protected abstract void registerName(final String
... path
);
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
);
216 * Reset the global value of a node.
224 public void resetGlobalValue(final String
... path
) {
225 for (TmfStatisticsTreeNode node
: getChildren(path
)) {
226 node
.resetGlobalValue();
231 * Reset the time range value of a node.
239 public void resetTimeRangeValue(final String
... path
) {
240 for (TmfStatisticsTreeNode node
: getChildren(path
)) {
241 node
.resetTimeRangeValue();