Commit | Line | Data |
---|---|---|
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 | |
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 | /* | |
8035003b | 38 | * Construct a node with the given key and value. |
6e512b93 | 39 | */ |
8035003b ASL |
40 | public StatisticsTreeNode(String key, Statistics value) { |
41 | this(null, key, value); | |
6e512b93 ASL |
42 | } |
43 | ||
44 | /* | |
45 | * Construct a node with the given parent, key and value. | |
46 | */ | |
8035003b ASL |
47 | public StatisticsTreeNode(StatisticsTreeNode parent, String key, |
48 | Statistics value) { | |
6e512b93 ASL |
49 | super(); |
50 | this.parent = parent; | |
51 | this.key = key; | |
8035003b | 52 | this.value = value; |
6e512b93 ASL |
53 | this.children = new HashMap<String, StatisticsTreeNode>(); |
54 | } | |
55 | ||
56 | /* | |
57 | * @return key associated with this node. | |
58 | */ | |
59 | public StatisticsTreeNode getParent() { | |
60 | return this.parent; | |
61 | } | |
62 | ||
63 | /* | |
64 | * @return key associated with this node. | |
65 | */ | |
66 | public String getKey() { | |
67 | return this.key; | |
68 | } | |
69 | ||
70 | /* | |
71 | * @return value associated with this node. | |
72 | */ | |
73 | public Statistics getValue() { | |
74 | return this.value; | |
75 | } | |
76 | ||
77 | /* | |
78 | * Add a direct child with the given value at the given path. | |
79 | * | |
80 | * @return children node that was created. | |
81 | */ | |
8035003b ASL |
82 | public StatisticsTreeNode addChild(String key, Statistics value) { |
83 | StatisticsTreeNode created = new StatisticsTreeNode(this, key, value); | |
6e512b93 ASL |
84 | |
85 | this.children.put(key, created); | |
86 | ||
87 | return created; | |
88 | } | |
89 | ||
90 | /* | |
91 | * @return direct children node with the given key. null if not found. | |
92 | */ | |
93 | public StatisticsTreeNode getChild(String key) { | |
94 | if (!this.children.containsKey(key)) { | |
95 | return null; | |
96 | } | |
97 | ||
98 | return this.children.get(key); | |
99 | } | |
100 | ||
101 | /* | |
102 | * @return number of direct children of this node. | |
103 | */ | |
104 | public boolean hasChildren() { | |
105 | return getNbChildren() > 0; | |
106 | } | |
107 | ||
108 | /* | |
109 | * @return direct children of this node. | |
110 | */ | |
111 | public Collection<StatisticsTreeNode> getChildren() { | |
112 | return children.values(); | |
113 | } | |
114 | ||
115 | /* | |
116 | * @return number of direct children of this node. | |
117 | */ | |
118 | public int getNbChildren() { | |
119 | return children.size(); | |
120 | } | |
121 | ||
122 | /* | |
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. | |
125 | * | |
126 | * @return children node with the given path. null if not found. | |
127 | */ | |
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)) { | |
8035003b ASL |
133 | current.children.put(key, new StatisticsTreeNode(current, key, |
134 | new Statistics())); | |
6e512b93 ASL |
135 | } |
136 | ||
137 | // previous = current; | |
138 | current = current.children.get(key); | |
139 | } | |
140 | ||
141 | return current; | |
142 | } | |
143 | ||
144 | /* | |
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. | |
147 | * | |
148 | * @return children node with the given path. null if not found. | |
149 | */ | |
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)) { | |
8035003b ASL |
155 | current.children.put(key, new StatisticsTreeNode(previous, key, |
156 | new Statistics())); | |
6e512b93 ASL |
157 | } |
158 | ||
159 | previous = current; | |
160 | current = current.children.get(key); | |
161 | } | |
162 | ||
163 | return current; | |
164 | } | |
165 | ||
166 | /* | |
167 | * @return children node with the given path. null if not found. | |
168 | */ | |
169 | public StatisticsTreeNode getChildFromPath(String path) { | |
170 | StatisticsTreeNode current = this; | |
171 | for (String key : path.split("/")) { | |
172 | if (!current.children.containsKey(key)) { | |
173 | return null; | |
174 | } | |
175 | ||
176 | current = current.children.get(key); | |
177 | } | |
178 | ||
179 | return current; | |
180 | } | |
181 | ||
182 | /* | |
183 | * Use this to escape a key that might contain the '/' character. | |
184 | * | |
185 | * @return escaped key | |
186 | */ | |
187 | public static String escapeKey(String key) { | |
188 | return key.replace("%", "%25").replace("/", "%2F"); | |
189 | } | |
190 | ||
191 | /* | |
192 | * Use this to unescape a key. | |
193 | * | |
194 | * @return unescaped key | |
195 | */ | |
196 | public static String unescapeKey(String key) { | |
197 | return key.replace("%2F", "/").replace("%25", "%"); | |
198 | } | |
6e512b93 | 199 | } |