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
CommitLineData
79e08fd0 1/*******************************************************************************
b544077e 2 * Copyright (c) 2011, 2012 Ericsson
013a5f1c 3 *
79e08fd0
BH
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
013a5f1c 8 *
79e08fd0 9 * Contributors:
09667aa4
MD
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)
7588c810 13 * Alexandre Montplaisir - Move the tree structure logic into the nodes
79e08fd0
BH
14 *******************************************************************************/
15
cfd22ad0 16package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model;
79e08fd0 17
7588c810 18import java.util.Arrays;
79e08fd0 19import java.util.Collection;
7588c810
AM
20import java.util.HashMap;
21import java.util.Map;
79e08fd0 22
79e08fd0 23/**
013a5f1c
AM
24 * A tree where nodes can be accessed efficiently using paths.
25 *
09667aa4 26 * It works like file systems. Each node is identified by a key. A path is an
5673a177
AM
27 * array of String. The elements of the array represent the path from the root
28 * to this node.
013a5f1c 29 *
7588c810 30 * @author Mathieu Denis
25a042b3 31 * @version 2.0
cfd22ad0 32 * @since 2.0
79e08fd0
BH
33 */
34public class TmfStatisticsTreeNode {
09667aa4 35
7588c810
AM
36 /** Tree to which this node belongs */
37 private final TmfStatisticsTree fTree;
09667aa4 38
7588c810
AM
39 /** Path of this node. The last element represents its basename. */
40 private final String[] fPath;
09667aa4 41
7588c810
AM
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;
09667aa4
MD
50
51 /**
52 * Constructor.
53 *
7588c810
AM
54 * @param tree
55 * Owner tree of this node
56 * @param parent
57 * Parent node of this one
09667aa4
MD
58 * @param path
59 * Path to the node.
09667aa4 60 */
7588c810
AM
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;
09667aa4 72 fPath = path;
7588c810 73 fParent = parent;
507b1336 74 fChildren = new HashMap<>();
89c06060 75 fValues = new TmfStatisticsValues();
09667aa4
MD
76 }
77
7588c810
AM
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
09667aa4
MD
91 /**
92 * Test if a node contain the specified child.
93 *
7588c810 94 * @param childName
09667aa4
MD
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 */
7588c810
AM
99 public boolean containsChild(String childName) {
100 return fChildren.containsKey(childName);
09667aa4
MD
101 }
102
103 /**
7588c810 104 * Retrieve the given child from this node.
09667aa4 105 *
7588c810
AM
106 * @param childName
107 * The (base)name of the child you want
108 * @return The child object, or null if it doesn't exist
09667aa4 109 */
7588c810
AM
110 public TmfStatisticsTreeNode getChild(String childName) {
111 return fChildren.get(childName);
09667aa4
MD
112 }
113
114 /**
7588c810 115 * Get the children of this node.
09667aa4 116 *
79e08fd0
BH
117 * @return Direct children of this node.
118 */
7588c810
AM
119 public Collection<TmfStatisticsTreeNode> getChildren() {
120 return fChildren.values();
79e08fd0 121 }
09667aa4
MD
122
123 /**
7588c810 124 * Add a child to this node.
09667aa4 125 *
7588c810
AM
126 * @param childName
127 * Name of the child to add
128 * @return The newly-created child
09667aa4 129 */
7588c810
AM
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;
09667aa4
MD
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() {
7588c810 147 return fChildren.size();
09667aa4
MD
148 }
149
150 /**
151 * Return the parent node.
152 *
153 * @return Parent node.
154 */
155 public TmfStatisticsTreeNode getParent() {
7588c810 156 return fParent;
09667aa4
MD
157 }
158
159 /**
160 * Get the path of the node.
161 *
162 * @return The path of the node.
163 */
5673a177 164 public String[] getPath() {
09667aa4
MD
165 return fPath;
166 }
167
168 /**
169 * Get the value of this node.
170 *
171 * @return Value associated with this node.
172 */
89c06060
AM
173 public TmfStatisticsValues getValues() {
174 return fValues;
09667aa4
MD
175 }
176
177 /**
178 * Indicate if the node have children.
179 *
180 * @return True if the node has children.
181 */
182 public boolean hasChildren() {
7588c810 183 return (fChildren.size() > 0);
09667aa4
MD
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() {
7588c810
AM
191 fValues.resetTotalCount();
192 fValues.resetPartialCount();
193 fChildren.clear();
09667aa4 194 }
25a042b3 195
73fbf6be
MD
196 /**
197 * Resets the global number of events. It doesn't remove any node
7588c810 198 * and doesn't modify the partial event count. Works recursively.
73fbf6be
MD
199 *
200 * @since 2.0
201 */
202 public void resetGlobalValue() {
7588c810
AM
203 for (TmfStatisticsTreeNode child : fChildren.values()) {
204 child.resetGlobalValue();
205 }
206 fValues.resetTotalCount();
73fbf6be
MD
207 }
208
25a042b3
MD
209 /**
210 * Resets the number of events in the time range. It doesn't remove any node
7588c810 211 * and doesn't modify the global event count. Works recursively.
eeb388b1 212 *
25a042b3
MD
213 * @since 2.0
214 */
215 public void resetTimeRangeValue() {
7588c810
AM
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$
25a042b3 227 }
09667aa4 228}
This page took 0.054283 seconds and 5 git commands to generate.