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 * 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
.linuxtools
.tmf
.ui
.viewers
.statistics
.model
;
18 import java
.util
.Arrays
;
19 import java
.util
.Collection
;
20 import java
.util
.HashMap
;
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
;
55 * Owner tree of this node
57 * Parent node of this one
61 public TmfStatisticsTreeNode(TmfStatisticsTree tree
,
62 TmfStatisticsTreeNode parent
, final String
... path
) {
63 /* The path must not contain any null element, or else we won't be
64 * able to walk the tree. */
65 for (String elem
: path
) {
67 throw new IllegalArgumentException();
74 fChildren
= new HashMap
<String
, TmfStatisticsTreeNode
>();
75 fValues
= new TmfStatisticsValues();
79 * Get the name for this node. It's used as the key in the parent's node.
81 * @return Name of this node.
83 public String
getName() {
84 if (fPath
.length
== 0) {
85 /* This means we are the root node, which has no path itself */
86 return "root"; //$NON-NLS-1$
88 return fPath
[fPath
.length
- 1];
92 * Test if a node contain the specified child.
96 * @return true: if child with given key is present, false: if no child
97 * exists with given key name
99 public boolean containsChild(String childName
) {
100 return fChildren
.containsKey(childName
);
104 * Retrieve the given child from this node.
107 * The (base)name of the child you want
108 * @return The child object, or null if it doesn't exist
110 public TmfStatisticsTreeNode
getChild(String childName
) {
111 return fChildren
.get(childName
);
115 * Get the children of this node.
117 * @return Direct children of this node.
119 public Collection
<TmfStatisticsTreeNode
> getChildren() {
120 return fChildren
.values();
124 * Add a child to this node.
127 * Name of the child to add
128 * @return The newly-created child
130 public TmfStatisticsTreeNode
addChild(String childName
) {
131 TmfStatisticsTreeNode child
;
132 String
[] childPath
= new String
[fPath
.length
+ 1];
133 System
.arraycopy(fPath
, 0, childPath
, 0, fPath
.length
);
134 childPath
[fPath
.length
] = childName
;
136 child
= new TmfStatisticsTreeNode(this.fTree
, this, childPath
);
137 fChildren
.put(childName
, child
);
142 * Get the number of children this node have.
144 * @return Number of direct children of this node.
146 public int getNbChildren() {
147 return fChildren
.size();
151 * Return the parent node.
153 * @return Parent node.
155 public TmfStatisticsTreeNode
getParent() {
160 * Get the path of the node.
162 * @return The path of the node.
164 public String
[] getPath() {
169 * Get the value of this node.
171 * @return Value associated with this node.
173 public TmfStatisticsValues
getValues() {
178 * Indicate if the node have children.
180 * @return True if the node has children.
182 public boolean hasChildren() {
183 return (fChildren
.size() > 0);
187 * Start from creation time i.e. keep key and parent but new statistics and
190 public void reset() {
191 fValues
.resetTotalCount();
192 fValues
.resetPartialCount();
197 * Resets the global number of events. It doesn't remove any node
198 * and doesn't modify the partial event count. Works recursively.
202 public void resetGlobalValue() {
203 for (TmfStatisticsTreeNode child
: fChildren
.values()) {
204 child
.resetGlobalValue();
206 fValues
.resetTotalCount();
210 * Resets the number of events in the time range. It doesn't remove any node
211 * and doesn't modify the global event count. Works recursively.
215 public void resetTimeRangeValue() {
216 for (TmfStatisticsTreeNode child
: fChildren
.values()) {
217 child
.resetTimeRangeValue();
219 fValues
.resetPartialCount();
223 public String
toString() {
224 /* Used for debugging only */
225 return "Stats node, path = " + Arrays
.toString(fPath
) + //$NON-NLS-1$
226 ", values = " + fValues
.toString(); //$NON-NLS-1$