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
CommitLineData
79e08fd0 1/*******************************************************************************
b544077e 2 * Copyright (c) 2011, 2012 Ericsson
20ff3b75 3 *
79e08fd0
BH
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
20ff3b75 8 *
79e08fd0 9 * Contributors:
20ff3b75
AM
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
11 *
79e08fd0
BH
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui.views.statistics.model;
15
16import java.util.Collection;
17import java.util.HashMap;
18import java.util.Map;
19import java.util.Set;
20
72f1e62a 21import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 22import org.eclipse.linuxtools.tmf.core.util.TmfFixedArray;
79e08fd0 23import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo;
79e08fd0
BH
24
25/**
09667aa4
MD
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.
20ff3b75 29 *
25a042b3 30 * @version 2.0
09667aa4 31 * @author Mathieu Denis
79e08fd0
BH
32 */
33public abstract class AbsTmfStatisticsTree {
34
35 /**
25a042b3 36 * String builder used to merge string more efficienctly.
79e08fd0 37 */
1cceddbe 38 protected static final StringBuilder fBuilder = new StringBuilder();
09667aa4 39
79e08fd0 40 /**
09667aa4 41 * Identification of the root.
79e08fd0
BH
42 */
43 public static final TmfFixedArray<String> ROOT = new TmfFixedArray<String>("root"); //$NON-NLS-1$
44
45 /**
25a042b3 46 * Function to merge many string more efficienctly.
20ff3b75 47 *
79e08fd0
BH
48 * @param strings
49 * Strings to merge.
50 * @return A new string containing all the strings.
51 */
66711dc8 52 public synchronized static String mergeString(String... strings) {
79e08fd0 53 fBuilder.setLength(0);
20ff3b75 54 for (String s : strings) {
79e08fd0 55 fBuilder.append(s);
20ff3b75 56 }
09667aa4 57 return fBuilder.toString();
79e08fd0
BH
58 }
59
60 /**
09667aa4
MD
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.
79e08fd0 63 */
66711dc8 64 protected Map<String, Set<String>> fKeys;
09667aa4 65
79e08fd0 66 /**
09667aa4 67 * The nodes in the tree.
79e08fd0 68 */
66711dc8 69 protected HashMap<TmfFixedArray<String>, TmfStatisticsTreeNode> fNodes;
79e08fd0
BH
70
71 /**
09667aa4 72 * Constructor.
79e08fd0
BH
73 */
74 public AbsTmfStatisticsTree() {
75 fNodes = new HashMap<TmfFixedArray<String>, TmfStatisticsTreeNode>();
76 fKeys = new HashMap<String, Set<String>>();
77 }
78
79 /**
09667aa4 80 * Get a node.
20ff3b75 81 *
79e08fd0
BH
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 /**
09667aa4 91 * Get the children of a node.
20ff3b75 92 *
79e08fd0
BH
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 /**
09667aa4
MD
100 * Get every children of a node, even if it doesn't have any registered
101 * events, as opposed to getChildren
20ff3b75 102 *
79e08fd0
BH
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);
20ff3b75 108
79e08fd0 109 /**
09667aa4 110 * Get the map of existing elements of path classified by parent.
20ff3b75 111 *
79e08fd0
BH
112 * @return The map.
113 */
66711dc8 114 public Map<String, Set<String>> getKeys() {
79e08fd0
BH
115 return fKeys;
116 }
117
118 /**
09667aa4 119 * Get or create a node.
20ff3b75 120 *
79e08fd0
BH
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 /**
09667aa4 136 * Get the parent of a node.
20ff3b75 137 *
79e08fd0
BH
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) {
20ff3b75 144 if (path.equals(ROOT)) {
79e08fd0 145 return null;
20ff3b75 146 }
abbdd66a 147 return get(ROOT);
79e08fd0
BH
148 }
149 return get(path.subArray(0, path.size() - 1));
150 }
151
152 /**
09667aa4
MD
153 * Increase any kind of counter.
154 *
155 * This method must be implemented by subclasses.
156 *
79e08fd0
BH
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 */
72f1e62a 164 public abstract void increase(ITmfEvent event, ITmfExtraEventInfo extraInfo, int values);
79e08fd0
BH
165
166 /**
09667aa4
MD
167 * Register an event.
168 *
169 * This method must be implemented by subclasses.
170 *
79e08fd0
BH
171 * @param event
172 * Current event.
173 * @param extraInfo
174 * Extra information to pass along with the event.
175 */
72f1e62a 176 public abstract void registerEvent(ITmfEvent event, ITmfExtraEventInfo extraInfo);
79e08fd0 177
25a042b3
MD
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
79e08fd0 191 /**
09667aa4
MD
192 * Register that a new node was created.
193 *
79e08fd0
BH
194 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
195 * will return the newly created node.
20ff3b75 196 *
79e08fd0
BH
197 * @param path
198 * Path of the new node.
199 */
200 protected abstract void registerName(final TmfFixedArray<String> path);
201
202 /**
09667aa4
MD
203 * Reset a node.
204 *
25a042b3 205 * Works recursively.
20ff3b75 206 *
79e08fd0
BH
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 }
25a042b3
MD
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 }
79e08fd0 232}
This page took 0.043027 seconds and 5 git commands to generate.