tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / statistics / model / TmfStatisticsTreeNode.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 * 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 *******************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model;
17
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 /**
24 * A tree where nodes can be accessed efficiently using paths.
25 *
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
28 * to this node.
29 *
30 * @author Mathieu Denis
31 * @version 2.0
32 * @since 2.0
33 */
34 public class TmfStatisticsTreeNode {
35
36 /** Tree to which this node belongs */
37 private final TmfStatisticsTree fTree;
38
39 /** Path of this node. The last element represents its basename. */
40 private final String[] fPath;
41
42 /** Parent node */
43 private final TmfStatisticsTreeNode fParent;
44
45 /** Children of this node, indexed by their basename. */
46 private final Map<String, TmfStatisticsTreeNode> fChildren;
47
48 /** Statistics values associated to this node. */
49 private final TmfStatisticsValues fValues;
50
51 /**
52 * Constructor.
53 *
54 * @param tree
55 * Owner tree of this node
56 * @param parent
57 * Parent node of this one
58 * @param path
59 * Path to the node.
60 */
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) {
66 if (elem == null) {
67 throw new IllegalArgumentException();
68 }
69 }
70
71 fTree = tree;
72 fPath = path;
73 fParent = parent;
74 fChildren = new HashMap<>();
75 fValues = new TmfStatisticsValues();
76 }
77
78 /**
79 * Get the name for this node. It's used as the key in the parent's node.
80 *
81 * @return Name of this node.
82 */
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$
87 }
88 return fPath[fPath.length - 1];
89 }
90
91 /**
92 * Test if a node contain the specified child.
93 *
94 * @param childName
95 * Name of the child.
96 * @return true: if child with given key is present, false: if no child
97 * exists with given key name
98 */
99 public boolean containsChild(String childName) {
100 return fChildren.containsKey(childName);
101 }
102
103 /**
104 * Retrieve the given child from this node.
105 *
106 * @param childName
107 * The (base)name of the child you want
108 * @return The child object, or null if it doesn't exist
109 */
110 public TmfStatisticsTreeNode getChild(String childName) {
111 return fChildren.get(childName);
112 }
113
114 /**
115 * Get the children of this node.
116 *
117 * @return Direct children of this node.
118 */
119 public Collection<TmfStatisticsTreeNode> getChildren() {
120 return fChildren.values();
121 }
122
123 /**
124 * Add a child to this node.
125 *
126 * @param childName
127 * Name of the child to add
128 * @return The newly-created child
129 */
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;
135
136 child = new TmfStatisticsTreeNode(this.fTree, this, childPath);
137 fChildren.put(childName, child);
138 return child;
139 }
140
141 /**
142 * Get the number of children this node have.
143 *
144 * @return Number of direct children of this node.
145 */
146 public int getNbChildren() {
147 return fChildren.size();
148 }
149
150 /**
151 * Return the parent node.
152 *
153 * @return Parent node.
154 */
155 public TmfStatisticsTreeNode getParent() {
156 return fParent;
157 }
158
159 /**
160 * Get the path of the node.
161 *
162 * @return The path of the node.
163 */
164 public String[] getPath() {
165 return fPath;
166 }
167
168 /**
169 * Get the value of this node.
170 *
171 * @return Value associated with this node.
172 */
173 public TmfStatisticsValues getValues() {
174 return fValues;
175 }
176
177 /**
178 * Indicate if the node have children.
179 *
180 * @return True if the node has children.
181 */
182 public boolean hasChildren() {
183 return (fChildren.size() > 0);
184 }
185
186 /**
187 * Start from creation time i.e. keep key and parent but new statistics and
188 * no children.
189 */
190 public void reset() {
191 fValues.resetTotalCount();
192 fValues.resetPartialCount();
193 fChildren.clear();
194 }
195
196 /**
197 * Resets the global number of events. It doesn't remove any node
198 * and doesn't modify the partial event count. Works recursively.
199 *
200 * @since 2.0
201 */
202 public void resetGlobalValue() {
203 for (TmfStatisticsTreeNode child : fChildren.values()) {
204 child.resetGlobalValue();
205 }
206 fValues.resetTotalCount();
207 }
208
209 /**
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.
212 *
213 * @since 2.0
214 */
215 public void resetTimeRangeValue() {
216 for (TmfStatisticsTreeNode child : fChildren.values()) {
217 child.resetTimeRangeValue();
218 }
219 fValues.resetPartialCount();
220 }
221
222 @Override
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$
227 }
228 }
This page took 0.061454 seconds and 5 git commands to generate.