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 and value.
40 public StatisticsTreeNode(String key
, Statistics value
) {
41 this(null, key
, value
);
45 * Construct a node with the given parent, key and value.
47 public StatisticsTreeNode(StatisticsTreeNode parent
, String key
,
53 this.children
= new HashMap
<String
, StatisticsTreeNode
>();
57 * @return key associated with this node.
59 public StatisticsTreeNode
getParent() {
64 * @return key associated with this node.
66 public String
getKey() {
71 * @return value associated with this node.
73 public Statistics
getValue() {
78 * Add a direct child with the given value at the given path.
80 * @return children node that was created.
82 public StatisticsTreeNode
addChild(String key
, Statistics value
) {
83 StatisticsTreeNode created
= new StatisticsTreeNode(this, key
, value
);
85 this.children
.put(key
, created
);
91 * @return direct children node with the given key. null if not found.
93 public StatisticsTreeNode
getChild(String key
) {
94 if (!this.children
.containsKey(key
)) {
98 return this.children
.get(key
);
102 * @return number of direct children of this node.
104 public boolean hasChildren() {
105 return getNbChildren() > 0;
109 * @return direct children of this node.
111 public Collection
<StatisticsTreeNode
> getChildren() {
112 return children
.values();
116 * @return number of direct children of this node.
118 public int getNbChildren() {
119 return children
.size();
123 * Get the node at the given path. If it doesn't exist each node in the path
124 * will be created with the given class.
126 * @return children node with the given path. null if not found.
128 public StatisticsTreeNode
getOrCreateChildFromPath(String
[] path
) {
129 // StatisticsTreeNode previous = this.parent;
130 StatisticsTreeNode current
= this;
131 for (String key
: path
) {
132 if (!current
.children
.containsKey(key
)) {
133 current
.children
.put(key
, new StatisticsTreeNode(current
, key
,
137 // previous = current;
138 current
= current
.children
.get(key
);
145 * Get the node at the given path. If it doesn't exist each node in the path
146 * will be created with the given class.
148 * @return children node with the given path. null if not found.
150 public StatisticsTreeNode
getOrCreateChildFromPath(String path
) {
151 StatisticsTreeNode previous
= this.parent
;
152 StatisticsTreeNode current
= this;
153 for (String key
: path
.split("/")) {
154 if (!current
.children
.containsKey(key
)) {
155 current
.children
.put(key
, new StatisticsTreeNode(previous
, key
,
160 current
= current
.children
.get(key
);
167 * @return children node with the given path. null if not found.
169 public StatisticsTreeNode
getChildFromPath(String path
) {
170 StatisticsTreeNode current
= this;
171 for (String key
: path
.split("/")) {
172 if (!current
.children
.containsKey(key
)) {
176 current
= current
.children
.get(key
);
183 * Use this to escape a key that might contain the '/' character.
185 * @return escaped key
187 public static String
escapeKey(String key
) {
188 return key
.replace("%", "%25").replace("/", "%2F");
192 * Use this to unescape a key.
194 * @return unescaped key
196 public static String
unescapeKey(String key
) {
197 return key
.replace("%2F", "/").replace("%25", "%");