2010-11-23 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug325661
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / statistics / model / StatisticsTreeNode.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 *******************************************************************************/
13
14 package org.eclipse.linuxtools.lttng.ui.views.statistics.model;
15
16 import java.util.Collection;
17
18 /**
19 * <h4>A tree where nodes can be accessed efficiently using paths.</h4>
20 *
21 * <p>It works like file systems. Each node is identified by a key. A path is an array ({@link FixedArray}) of String. The elements of the array represent the path from the root to this node.</p>
22 */
23 public class StatisticsTreeNode implements Comparable<StatisticsTreeNode> {
24 /**
25 * <h4>Value of the node.</h4>
26 */
27 private Statistics fValue;
28 /**
29 * <h4>Path of the node.</h4>
30 */
31 private FixedArray fPath;
32 /**
33 * <h4>Corresponding StatisticsData.</h4>
34 */
35 private StatisticsData fNodes;
36 /**
37 * <h4>Name of the node.</h4>
38 */
39 private String fName = ""; //$NON-NLS-1$
40 /**
41 * <h4>Constructor.</h4>
42 * @param path Path to the node.
43 * @param nodes Corresponding StatisticsData.
44 */
45 public StatisticsTreeNode(final FixedArray path, StatisticsData nodes) {
46 this(path, nodes, ""); //$NON-NLS-1$
47 }
48
49 /**
50 * <h4>Constructor.</h4>
51 * @param path Path to the node.
52 * @param nodes Corresponding StatisticsData.
53 * @param name The name associated with this node.
54 */
55 public StatisticsTreeNode(final FixedArray path, StatisticsData nodes, String name) {
56 fPath = path;
57 fNodes = nodes;
58 fName = name;
59 fValue = new Statistics();
60 }
61
62 /**
63 * <h4>Test if a node contain the specified child.</h4>
64 * @param key Name of the child.
65 * @return true: if child with given key is present, false: if no child exists with given key name
66 */
67 public boolean containsChild(Integer key) {
68 if(StatisticsData.ROOT == fPath)
69 return fNodes.get(new FixedArray(key)) != null;
70 return (fNodes.get(fPath.append(key)) != null);
71 }
72 /**
73 * <h4>Get the children of this node.</h4>
74 * @return Direct children of this node.
75 */
76 public Collection<StatisticsTreeNode> getChildren() {
77 return fNodes.getChildren(fPath);
78 }
79 /**
80 * <h4>Get the key for this node.</h4>
81 * @return Key associated with this node.
82 */
83 public Integer getKey() {
84 return fPath.get(fPath.size() - 1);
85 }
86 /**
87 * <h4>Get the number of children this node have.</h4>
88 * @return Number of direct children of this node.
89 */
90 public int getNbChildren() {
91 return fNodes.getChildren(fPath).size();
92 }
93 /**
94 * <h4>Return the parent node.</h4>
95 * @return Parent node.
96 */
97 public StatisticsTreeNode getParent() {
98 return fNodes.getParent(fPath);
99 }
100 /**
101 * <h4>Get the path of the node.</h4>
102 * @return The path of the node.
103 */
104 public FixedArray getPath() {
105 return fPath;
106 }
107 /**
108 * <h4>Get the value of this node.</h4>
109 * @return Value associated with this node.
110 */
111 public Statistics getValue() {
112 return fValue;
113 }
114 /**
115 * <h4>Indicate if the node have children.</h4>
116 * @return True if the node has children.
117 */
118 public boolean hasChildren() {
119 return !fNodes.getChildren(fPath).isEmpty();
120 }
121
122 /**
123 * <h4>Start from creation time i.e. keep key and parent but new statistics and no children.</h4>
124 */
125 public void reset() {
126 fValue = new Statistics();
127 fNodes.reset(fPath);
128 }
129
130 /**
131 * <h4>Set the name of this node.</h4>
132 */
133 public void setName (String name) {
134 fName = name;
135 }
136 /**
137 * <h4>Get the name of this node.</h4>
138 * @return Name associated with this node.
139 */
140 public String getName() {
141 return fName;
142 }
143
144 /**
145 * <h4>Returns node content as string (full path is not included).</h4>
146 * @return Node content as string.
147 */
148 public String getContent() {
149 return fName + ": [nbEvents=" + fValue.nbEvents + //$NON-NLS-1$
150 ", cpuTime=" + fValue.cpuTime + //$NON-NLS-1$
151 ", cumulativeCpuTime=" + fValue.cumulativeCpuTime + //$NON-NLS-1$
152 ", elapsedTime=" + fValue.elapsedTime + "]"; //$NON-NLS-1$ //$NON-NLS-2$
153 }
154
155 /**
156 * <h4>For sorting purposes (sorting by node name).</h4>
157 */
158 @Override
159 public int compareTo(StatisticsTreeNode o) {
160 return fName.compareTo(o.fName);
161 }
162 }
This page took 0.039394 seconds and 5 git commands to generate.