1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Yann N. Dauphin (dhaemon@gmail.com) - Implementation for stats
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.statistics
.model
;
14 import java
.util
.AbstractMap
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
19 * A tree where nodes can be accessed efficiently using paths.
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.
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.
27 public class StatisticsTreeNode
{
29 private StatisticsTreeNode parent
;
33 private Statistics value
;
35 private AbstractMap
<String
, StatisticsTreeNode
> children
;
38 * Construct a node with the given key
40 public StatisticsTreeNode(String key
) {
45 * Construct a node with the given parent, key and value.
47 public StatisticsTreeNode(StatisticsTreeNode parent
, String key
) {
51 this.value
= new Statistics();
52 this.children
= new HashMap
<String
, StatisticsTreeNode
>();
56 * @return key associated with this node.
58 public StatisticsTreeNode
getParent() {
63 * @return key associated with this node.
65 public String
getKey() {
70 * @return value associated with this node.
72 public Statistics
getValue() {
77 * Add a direct child with the given value at the given path.
79 * @return children node that was created.
81 public StatisticsTreeNode
addChild(String key
) {
82 StatisticsTreeNode created
= new StatisticsTreeNode(this, key
);
84 this.children
.put(key
, created
);
90 * @return direct children node with the given key. null if not found.
92 public StatisticsTreeNode
getChild(String key
) {
93 if (!this.children
.containsKey(key
)) {
97 return this.children
.get(key
);
101 * @return number of direct children of this node.
103 public boolean hasChildren() {
104 return getNbChildren() > 0;
108 * @return direct children of this node.
110 public Collection
<StatisticsTreeNode
> getChildren() {
111 return children
.values();
115 * @return number of direct children of this node.
117 public int getNbChildren() {
118 return children
.size();
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.
125 * @return children node with the given path. null if not found.
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
));
135 // previous = current;
136 current
= current
.children
.get(key
);
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.
146 * @return children node with the given path. null if not found.
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
));
157 current
= current
.children
.get(key
);
164 * @return children node with the given path. null if not found.
166 public StatisticsTreeNode
getChildFromPath(String path
) {
167 StatisticsTreeNode current
= this;
168 for (String key
: path
.split("/")) {
169 if (!current
.children
.containsKey(key
)) {
173 current
= current
.children
.get(key
);
180 * Use this to escape a key that might contain the '/' character.
182 * @return escaped key
184 public static String
escapeKey(String key
) {
185 return key
.replace("%", "%25").replace("/", "%2F");
189 * Use this to unescape a key.
191 * @return unescaped key
193 public static String
unescapeKey(String key
) {
194 return key
.replace("%2F", "/").replace("%25", "%");
198 * Start from creation time i.e. keep key and parent but new statistics and
201 public void reset() {
202 this.value
= new Statistics();
203 this.children
= new HashMap
<String
, StatisticsTreeNode
>();
209 * @return true: if child with given key is present, false: if no child
210 * exists with given key name
212 public boolean containsChild(String key
) {
213 return children
.containsKey(key
);