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
CommitLineData
6e512b93
ASL
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
9dbeec54 11 * Francois Godin (copelnug@gmail.com) - Re-design for new stats structure
6e512b93 12 *******************************************************************************/
9dbeec54 13
6e512b93
ASL
14package org.eclipse.linuxtools.lttng.ui.views.statistics.model;
15
6e512b93 16import java.util.Collection;
6e512b93 17
9dbeec54
FC
18/**
19 * <h4>A tree where nodes can be accessed efficiently using paths.</h4>
6e512b93 20 *
9dbeec54 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>
6e512b93 22 */
3037adf3 23public class StatisticsTreeNode implements Comparable<StatisticsTreeNode> {
9dbeec54
FC
24 /**
25 * <h4>Value of the node.</h4>
6e512b93 26 */
9dbeec54
FC
27 private Statistics fValue;
28 /**
29 * <h4>Path of the node.</h4>
6e512b93 30 */
b12f4544 31 private FixedArray fPath;
9dbeec54
FC
32 /**
33 * <h4>Corresponding StatisticsData.</h4>
6e512b93 34 */
9dbeec54
FC
35 private StatisticsData fNodes;
36 /**
b12f4544
FC
37 * <h4>Name of the node.</h4>
38 */
39 private String fName = ""; //$NON-NLS-1$
40 /**
9dbeec54
FC
41 * <h4>Constructor.</h4>
42 * @param path Path to the node.
43 * @param nodes Corresponding StatisticsData.
6e512b93 44 */
b12f4544
FC
45 public StatisticsTreeNode(final FixedArray path, StatisticsData nodes) {
46 this(path, nodes, ""); //$NON-NLS-1$
6e512b93 47 }
b12f4544
FC
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
9dbeec54
FC
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
6e512b93 66 */
b12f4544
FC
67 public boolean containsChild(Integer key) {
68 if(StatisticsData.ROOT == fPath)
69 return fNodes.get(new FixedArray(key)) != null;
9dbeec54 70 return (fNodes.get(fPath.append(key)) != null);
6e512b93 71 }
9dbeec54
FC
72 /**
73 * <h4>Get the children of this node.</h4>
74 * @return Direct children of this node.
6e512b93
ASL
75 */
76 public Collection<StatisticsTreeNode> getChildren() {
9dbeec54 77 return fNodes.getChildren(fPath);
6e512b93 78 }
9dbeec54
FC
79 /**
80 * <h4>Get the key for this node.</h4>
81 * @return Key associated with this node.
6e512b93 82 */
b12f4544 83 public Integer getKey() {
9dbeec54 84 return fPath.get(fPath.size() - 1);
6e512b93 85 }
9dbeec54
FC
86 /**
87 * <h4>Get the number of children this node have.</h4>
88 * @return Number of direct children of this node.
6e512b93 89 */
9dbeec54
FC
90 public int getNbChildren() {
91 return fNodes.getChildren(fPath).size();
6e512b93 92 }
9dbeec54
FC
93 /**
94 * <h4>Return the parent node.</h4>
95 * @return Parent node.
6e512b93 96 */
9dbeec54
FC
97 public StatisticsTreeNode getParent() {
98 return fNodes.getParent(fPath);
6e512b93 99 }
9dbeec54
FC
100 /**
101 * <h4>Get the path of the node.</h4>
102 * @return The path of the node.
6e512b93 103 */
b12f4544 104 public FixedArray getPath() {
9dbeec54 105 return fPath;
6e512b93 106 }
9dbeec54
FC
107 /**
108 * <h4>Get the value of this node.</h4>
109 * @return Value associated with this node.
6e512b93 110 */
9dbeec54
FC
111 public Statistics getValue() {
112 return fValue;
6e512b93 113 }
8827c197 114 /**
9dbeec54
FC
115 * <h4>Indicate if the node have children.</h4>
116 * @return True if the node has children.
8827c197 117 */
9dbeec54
FC
118 public boolean hasChildren() {
119 return !fNodes.getChildren(fPath).isEmpty();
8827c197 120 }
3037adf3 121
8827c197 122 /**
9dbeec54 123 * <h4>Start from creation time i.e. keep key and parent but new statistics and no children.</h4>
8827c197 124 */
9dbeec54
FC
125 public void reset() {
126 fValue = new Statistics();
127 fNodes.reset(fPath);
8827c197 128 }
b12f4544
FC
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 /**
3037adf3
FC
145 * <h4>Returns node content as string (full path is not included).</h4>
146 * @return Node content as string.
147 */
b12f4544
FC
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$
3037adf3
FC
153 }
154
b12f4544
FC
155 /**
156 * <h4>For sorting purposes (sorting by node name).</h4>
157 */
158 @Override
3037adf3 159 public int compareTo(StatisticsTreeNode o) {
b12f4544 160 return fName.compareTo(o.fName);
3037adf3 161 }
6e512b93 162}
This page took 0.039252 seconds and 5 git commands to generate.