1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * Yann N. Dauphin <dhaemon@gmail.com> - Implementation for stats
11 * Francois Godin <copelnug@gmail.com> - Re-design for new stats structure
12 * Mathieu Denis <mathieu.denis@polymtl.ca> - Re-design for new stats structure (2)
13 * Alexandre Montplaisir - Move the tree structure logic into the nodes
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.statistics
.model
;
18 import java
.util
.Arrays
;
19 import java
.util
.Collection
;
21 import java
.util
.concurrent
.ConcurrentHashMap
;
24 * A tree where nodes can be accessed efficiently using paths.
26 * It works like file systems. Each node is identified by a key. A path is an
27 * array of String. The elements of the array represent the path from the root
30 * @author Mathieu Denis
34 public class TmfStatisticsTreeNode
{
36 /** Tree to which this node belongs */
37 private final TmfStatisticsTree fTree
;
39 /** Path of this node. The last element represents its basename. */
40 private final String
[] fPath
;
43 private final TmfStatisticsTreeNode fParent
;
45 /** Children of this node, indexed by their basename. */
46 private final Map
<String
, TmfStatisticsTreeNode
> fChildren
;
48 /** Statistics values associated to this node. */
49 private final TmfStatisticsValues fValues
;
52 * Return the node at the top of the branch
54 private final TmfStatisticsTreeNode fTopNode
;
60 * Owner tree of this node
62 * Parent node of this one
66 public TmfStatisticsTreeNode(TmfStatisticsTree tree
,
67 TmfStatisticsTreeNode parent
, final String
... path
) {
69 * The path must not contain any null element, or else we won't be able
72 for (String elem
: path
) {
74 throw new IllegalArgumentException();
81 fChildren
= new ConcurrentHashMap
<>();
82 fValues
= new TmfStatisticsValues();
84 /* calculating top node */
85 TmfStatisticsTreeNode topNode
= this;
86 while (topNode
.getParent() != null && topNode
.getParent().getParent() != null) {
87 topNode
= topNode
.getParent();
93 * Get the name for this node. It's used as the key in the parent's node.
95 * @return Name of this node.
97 public String
getName() {
98 if (fPath
.length
== 0) {
99 /* This means we are the root node, which has no path itself */
100 return "root"; //$NON-NLS-1$
102 return fPath
[fPath
.length
- 1];
106 * Test if a node contain the specified child.
110 * @return true: if child with given key is present, false: if no child
111 * exists with given key name
113 public boolean containsChild(String childName
) {
114 return fChildren
.containsKey(childName
);
118 * Retrieve the given child from this node.
121 * The (base)name of the child you want
122 * @return The child object, or null if it doesn't exist
124 public TmfStatisticsTreeNode
getChild(String childName
) {
125 return fChildren
.get(childName
);
129 * Get the children of this node.
131 * @return Direct children of this node.
133 public Collection
<TmfStatisticsTreeNode
> getChildren() {
134 return fChildren
.values();
138 * Add a child to this node.
141 * Name of the child to add
142 * @return The newly-created child
144 public TmfStatisticsTreeNode
addChild(String childName
) {
145 TmfStatisticsTreeNode child
;
146 String
[] childPath
= new String
[fPath
.length
+ 1];
147 System
.arraycopy(fPath
, 0, childPath
, 0, fPath
.length
);
148 childPath
[fPath
.length
] = childName
;
150 child
= new TmfStatisticsTreeNode(this.fTree
, this, childPath
);
151 fChildren
.put(childName
, child
);
156 * Get the number of children this node have.
158 * @return Number of direct children of this node.
160 public int getNbChildren() {
161 return fChildren
.size();
165 * Return the parent node.
167 * @return Parent node.
169 public TmfStatisticsTreeNode
getParent() {
174 * Return the top node.
179 public TmfStatisticsTreeNode
getTop() {
184 * Get the path of the node.
186 * @return The path of the node.
188 public String
[] getPath() {
193 * Get the value of this node.
195 * @return Value associated with this node.
197 public TmfStatisticsValues
getValues() {
202 * Indicate if the node have children.
204 * @return True if the node has children.
206 public boolean hasChildren() {
207 return (fChildren
.size() > 0);
211 * Start from creation time i.e. keep key and parent but new statistics and
214 public void reset() {
215 fValues
.resetTotalCount();
216 fValues
.resetPartialCount();
221 * Resets the global number of events. It doesn't remove any node and
222 * doesn't modify the partial event count. Works recursively.
226 public void resetGlobalValue() {
227 for (TmfStatisticsTreeNode child
: fChildren
.values()) {
228 child
.resetGlobalValue();
230 fValues
.resetTotalCount();
234 * Resets the number of events in the time range. It doesn't remove any node
235 * and doesn't modify the global event count. Works recursively.
239 public void resetTimeRangeValue() {
240 for (TmfStatisticsTreeNode child
: fChildren
.values()) {
241 child
.resetTimeRangeValue();
243 fValues
.resetPartialCount();
247 public String
toString() {
248 /* Used for debugging only */
249 return "Stats node, path = " + Arrays
.toString(fPath
) + //$NON-NLS-1$
250 ", values = " + fValues
.toString(); //$NON-NLS-1$