Skip to main content
summaryrefslogtreecommitdiffstats
blob: 172e05e3ad41ae84d770f3d4c1fc0d8e1ae6cb77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************************************************
 * Copyright (c) 2009 Ericsson
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Yann N. Dauphin     (dhaemon@gmail.com)  - Implementation for stats
 *******************************************************************************/
package org.eclipse.linuxtools.lttng.ui.views.statistics.model;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.HashMap;

/*
 * A tree where nodes can be accessed efficiently using paths.
 * 
 * It works like file systems. Each node is identified by a key. A path is a list of keys separated by the character '/'.
 * For example, the path 'persons/yann' will browse to the child 'persons' and return it's 'yann' child.
 * 
 * If a key might contains the character '/', use the #escapeKey method to get an escaped key. Use the #unescapeKey
 * method to unescaped the key.
 */
public class StatisticsTreeNode {

	private StatisticsTreeNode parent;

	private String key;

	private Statistics value;

	private AbstractMap<String, StatisticsTreeNode> children;

	/*
	 * Construct a node with the given key
	 */
	public StatisticsTreeNode(String key) {
		this(null, key);
	}

	/*
	 * Construct a node with the given parent, key and value.
	 */
	public StatisticsTreeNode(StatisticsTreeNode parent, String key) {
		super();
		this.parent = parent;
		this.key = key;
		this.value = new Statistics();
		this.children = new HashMap<String, StatisticsTreeNode>();
	}

	/*
	 * @return key associated with this node.
	 */
	public StatisticsTreeNode getParent() {
		return this.parent;
	}

	/*
	 * @return key associated with this node.
	 */
	public String getKey() {
		return this.key;
	}

	/*
	 * @return value associated with this node.
	 */
	public Statistics getValue() {
		return this.value;
	}

	/*
	 * Add a direct child with the given value at the given path.
	 * 
	 * @return children node that was created.
	 */
	public StatisticsTreeNode addChild(String key) {
		StatisticsTreeNode created = new StatisticsTreeNode(this, key);

		this.children.put(key, created);

		return created;
	}

	/*
	 * @return direct children node with the given key. null if not found.
	 */
	public StatisticsTreeNode getChild(String key) {
		if (!this.children.containsKey(key)) {
			return null;
		}

		return this.children.get(key);
	}

	/*
	 * @return number of direct children of this node.
	 */
	public boolean hasChildren() {
		return getNbChildren() > 0;
	}

	/*
	 * @return direct children of this node.
	 */
	public Collection<StatisticsTreeNode> getChildren() {
		return children.values();
	}

	/*
	 * @return number of direct children of this node.
	 */
	public int getNbChildren() {
		return children.size();
	}

	/*
	 * Get the node at the given path. If it doesn't exist each node in the path
	 * will be created with the given class.
	 * 
	 * @return children node with the given path. null if not found.
	 */
	public StatisticsTreeNode getOrCreateChildFromPath(String[] path) {
		// StatisticsTreeNode previous = this.parent;
		StatisticsTreeNode current = this;
		for (String key : path) {
			if (!current.children.containsKey(key)) {
				current.children.put(key, new StatisticsTreeNode(current, key));
			}

			// previous = current;
			current = current.children.get(key);
		}

		return current;
	}

	/*
	 * Get the node at the given path. If it doesn't exist each node in the path
	 * will be created with the given class.
	 * 
	 * @return children node with the given path. null if not found.
	 */
	public StatisticsTreeNode getOrCreateChildFromPath(String path) {
		StatisticsTreeNode previous = this.parent;
		StatisticsTreeNode current = this;
		for (String key : path.split("/")) {
			if (!current.children.containsKey(key)) {
				current.children.put(key, new StatisticsTreeNode(previous, key));
			}

			previous = current;
			current = current.children.get(key);
		}

		return current;
	}

	/*
	 * @return children node with the given path. null if not found.
	 */
	public StatisticsTreeNode getChildFromPath(String path) {
		StatisticsTreeNode current = this;
		for (String key : path.split("/")) {
			if (!current.children.containsKey(key)) {
				return null;
			}

			current = current.children.get(key);
		}

		return current;
	}

	/*
	 * Use this to escape a key that might contain the '/' character.
	 * 
	 * @return escaped key
	 */
	public static String escapeKey(String key) {
		return key.replace("%", "%25").replace("/", "%2F");
	}

	/*
	 * Use this to unescape a key.
	 * 
	 * @return unescaped key
	 */
	public static String unescapeKey(String key) {
		return key.replace("%2F", "/").replace("%25", "%");
	}

	/**
	 * Start from creation time i.e. keep key and parent but new statistics and
	 * no children
	 */
	public void reset() {
		this.value = new Statistics();
		this.children = new HashMap<String, StatisticsTreeNode>();
	}

	/**
	 * 
	 * @param key
	 * @return true: if child with given key is present, false: if no child
	 *         exists with given key name
	 */
	public boolean containsChild(String key) {
		return children.containsKey(key);
	}
}

Back to the top