(no commit message)
[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 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.statistics.model;
13
14 import java.util.AbstractMap;
15 import java.util.Collection;
16 import java.util.HashMap;
17
18 /*
19 * A tree where nodes can be accessed efficiently using paths.
20 *
21 * It works like file systems. Each node is identified by a key. A path is a list of keys separated by the character '/'.
22 * For example, the path 'persons/yann' will browse to the child 'persons' and return it's 'yann' child.
23 *
24 * If a key might contains the character '/', use the #escapeKey method to get an escaped key. Use the #unescapeKey
25 * method to unescaped the key.
26 */
27 public class StatisticsTreeNode {
28
29 private StatisticsTreeNode parent;
30
31 private String key;
32
33 private Statistics value;
34
35 private AbstractMap<String, StatisticsTreeNode> children;
36
37 /*
38 * Construct a node with the given key
39 */
40 public StatisticsTreeNode(String key) {
41 this(null, key);
42 }
43
44 /*
45 * Construct a node with the given parent, key and value.
46 */
47 public StatisticsTreeNode(StatisticsTreeNode parent, String key) {
48 super();
49 this.parent = parent;
50 this.key = key;
51 this.value = new Statistics();
52 this.children = new HashMap<String, StatisticsTreeNode>();
53 }
54
55 /*
56 * @return key associated with this node.
57 */
58 public StatisticsTreeNode getParent() {
59 return this.parent;
60 }
61
62 /*
63 * @return key associated with this node.
64 */
65 public String getKey() {
66 return this.key;
67 }
68
69 /*
70 * @return value associated with this node.
71 */
72 public Statistics getValue() {
73 return this.value;
74 }
75
76 /*
77 * Add a direct child with the given value at the given path.
78 *
79 * @return children node that was created.
80 */
81 public StatisticsTreeNode addChild(String key) {
82 StatisticsTreeNode created = new StatisticsTreeNode(this, key);
83
84 this.children.put(key, created);
85
86 return created;
87 }
88
89 /*
90 * @return direct children node with the given key. null if not found.
91 */
92 public StatisticsTreeNode getChild(String key) {
93 if (!this.children.containsKey(key)) {
94 return null;
95 }
96
97 return this.children.get(key);
98 }
99
100 /*
101 * @return number of direct children of this node.
102 */
103 public boolean hasChildren() {
104 return getNbChildren() > 0;
105 }
106
107 /*
108 * @return direct children of this node.
109 */
110 public Collection<StatisticsTreeNode> getChildren() {
111 return children.values();
112 }
113
114 /*
115 * @return number of direct children of this node.
116 */
117 public int getNbChildren() {
118 return children.size();
119 }
120
121 /*
122 * Get the node at the given path. If it doesn't exist each node in the path
123 * will be created with the given class.
124 *
125 * @return children node with the given path. null if not found.
126 */
127 public StatisticsTreeNode getOrCreateChildFromPath(String[] path) {
128 // StatisticsTreeNode previous = this.parent;
129 StatisticsTreeNode current = this;
130 for (String key : path) {
131 if (!current.children.containsKey(key)) {
132 current.children.put(key, new StatisticsTreeNode(current, key));
133 }
134
135 // previous = current;
136 current = current.children.get(key);
137 }
138
139 return current;
140 }
141
142 /*
143 * Get the node at the given path. If it doesn't exist each node in the path
144 * will be created with the given class.
145 *
146 * @return children node with the given path. null if not found.
147 */
148 public StatisticsTreeNode getOrCreateChildFromPath(String path) {
149 StatisticsTreeNode previous = this.parent;
150 StatisticsTreeNode current = this;
151 for (String key : path.split("/")) {
152 if (!current.children.containsKey(key)) {
153 current.children.put(key, new StatisticsTreeNode(previous, key));
154 }
155
156 previous = current;
157 current = current.children.get(key);
158 }
159
160 return current;
161 }
162
163 /*
164 * @return children node with the given path. null if not found.
165 */
166 public StatisticsTreeNode getChildFromPath(String path) {
167 StatisticsTreeNode current = this;
168 for (String key : path.split("/")) {
169 if (!current.children.containsKey(key)) {
170 return null;
171 }
172
173 current = current.children.get(key);
174 }
175
176 return current;
177 }
178
179 /*
180 * Use this to escape a key that might contain the '/' character.
181 *
182 * @return escaped key
183 */
184 public static String escapeKey(String key) {
185 return key.replace("%", "%25").replace("/", "%2F");
186 }
187
188 /*
189 * Use this to unescape a key.
190 *
191 * @return unescaped key
192 */
193 public static String unescapeKey(String key) {
194 return key.replace("%2F", "/").replace("%25", "%");
195 }
196
197 /**
198 * Start from creation time i.e. keep key and parent but new statistics and
199 * no children
200 */
201 public void reset() {
202 this.value = new Statistics();
203 this.children = new HashMap<String, StatisticsTreeNode>();
204 }
205
206 /**
207 *
208 * @param key
209 * @return true: if child with given key is present, false: if no child
210 * exists with given key name
211 */
212 public boolean containsChild(String key) {
213 return children.containsKey(key);
214 }
215 }
This page took 0.163392 seconds and 6 git commands to generate.