Fix JUnit test failing after previous merge
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / statistics / model / AbsTmfStatisticsTree.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
11 *
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.views.statistics.model;
15
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22 import org.eclipse.linuxtools.tmf.core.util.TmfFixedArray;
23 import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo;
24
25 /**
26 * Base class for the statistics storage. It allow to implement a tree structure
27 * while avoiding the need to run through the tree each time you need to add a
28 * node at a given place.
29 *
30 * @version 2.0
31 * @author Mathieu Denis
32 */
33 public abstract class AbsTmfStatisticsTree {
34
35 /**
36 * String builder used to merge string more efficienctly.
37 */
38 protected static final StringBuilder fBuilder = new StringBuilder();
39
40 /**
41 * Identification of the root.
42 */
43 public static final TmfFixedArray<String> ROOT = new TmfFixedArray<String>("root"); //$NON-NLS-1$
44
45 /**
46 * Function to merge many string more efficienctly.
47 *
48 * @param strings
49 * Strings to merge.
50 * @return A new string containing all the strings.
51 */
52 public synchronized static String mergeString(String... strings) {
53 fBuilder.setLength(0);
54 for (String s : strings) {
55 fBuilder.append(s);
56 }
57 return fBuilder.toString();
58 }
59
60 /**
61 * Define what children a node can have. The management and usage of this map
62 * is done by subclasses. HashSet are always faster than TreeSet for String keys.
63 */
64 protected Map<String, Set<String>> fKeys;
65
66 /**
67 * The nodes in the tree.
68 */
69 protected HashMap<TmfFixedArray<String>, TmfStatisticsTreeNode> fNodes;
70
71 /**
72 * Constructor.
73 */
74 public AbsTmfStatisticsTree() {
75 fNodes = new HashMap<TmfFixedArray<String>, TmfStatisticsTreeNode>();
76 fKeys = new HashMap<String, Set<String>>();
77 }
78
79 /**
80 * Get a node.
81 *
82 * @param path
83 * Path to the node.
84 * @return The node or null.
85 */
86 public TmfStatisticsTreeNode get(final TmfFixedArray<String> path) {
87 return fNodes.get(path);
88 }
89
90 /**
91 * Get the children of a node.
92 *
93 * @param path
94 * Path to the node.
95 * @return Collection containing the children.
96 */
97 public abstract Collection<TmfStatisticsTreeNode> getChildren(final TmfFixedArray<String> path);
98
99 /**
100 * Get every children of a node, even if it doesn't have any registered
101 * events, as opposed to getChildren
102 *
103 * @param path
104 * Path to the node.
105 * @return Collection containing all the children.
106 */
107 public abstract Collection<TmfStatisticsTreeNode> getAllChildren(final TmfFixedArray<String> path);
108
109 /**
110 * Get the map of existing elements of path classified by parent.
111 *
112 * @return The map.
113 */
114 public Map<String, Set<String>> getKeys() {
115 return fKeys;
116 }
117
118 /**
119 * Get or create a node.
120 *
121 * @param path
122 * Path to the node.
123 * @return The node.
124 */
125 public TmfStatisticsTreeNode getOrCreate(final TmfFixedArray<String> path) {
126 TmfStatisticsTreeNode current = fNodes.get(path);
127 if (current == null) {
128 registerName(path);
129 current = new TmfStatisticsTreeNode(path, this);
130 fNodes.put(path, current);
131 }
132 return current;
133 }
134
135 /**
136 * Get the parent of a node.
137 *
138 * @param path
139 * Path to the node.
140 * @return Parent node or null.
141 */
142 public TmfStatisticsTreeNode getParent(final TmfFixedArray<String> path) {
143 if (path.size() == 1) {
144 if (path.equals(ROOT)) {
145 return null;
146 }
147 return get(ROOT);
148 }
149 return get(path.subArray(0, path.size() - 1));
150 }
151
152 /**
153 * Increase any kind of counter.
154 *
155 * This method must be implemented by subclasses.
156 *
157 * @param event
158 * Current event.
159 * @param extraInfo
160 * Extra information to pass along with the event.
161 * @param values
162 * Values desired.
163 */
164 public abstract void increase(ITmfEvent event, ITmfExtraEventInfo extraInfo, int values);
165
166 /**
167 * Register an event.
168 *
169 * This method must be implemented by subclasses.
170 *
171 * @param event
172 * Current event.
173 * @param extraInfo
174 * Extra information to pass along with the event.
175 */
176 public abstract void registerEvent(ITmfEvent event, ITmfExtraEventInfo extraInfo);
177
178 /**
179 * Register an event within a time range.
180 *
181 * This method must be implemented by subclasses.
182 *
183 * @param event
184 * Current event.
185 * @param extraInfo
186 * Extra information to pass along with the event.
187 * @since 2.0
188 */
189 public abstract void registerEventInTimeRange(ITmfEvent event, ITmfExtraEventInfo extraInfo);
190
191 /**
192 * Register that a new node was created.
193 *
194 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
195 * will return the newly created node.
196 *
197 * @param path
198 * Path of the new node.
199 */
200 protected abstract void registerName(final TmfFixedArray<String> path);
201
202 /**
203 * Reset a node.
204 *
205 * Works recursively.
206 *
207 * @param path
208 * Path to the node.
209 */
210 public void reset(final TmfFixedArray<String> path) {
211 for (TmfStatisticsTreeNode node : getAllChildren(path)) {
212 reset(node.getPath());
213 fNodes.remove(node.getPath());
214 }
215 }
216
217 /**
218 * Reset the time range value of a node.
219 *
220 * Works recursively.
221 *
222 * @param path
223 * Path to the node.
224 * @since 2.0
225 */
226 public void resetTimeRangeValue(final TmfFixedArray<String> path) {
227 for (TmfStatisticsTreeNode node : getChildren(path)) {
228 resetTimeRangeValue(node.getPath());
229 node.resetTimeRangeValue();
230 }
231 }
232 }
This page took 0.054855 seconds and 5 git commands to generate.