Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
72f1e62a | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
6c13869b | 22 | import org.eclipse.linuxtools.tmf.core.util.TmfFixedArray; |
79e08fd0 | 23 | import 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 | * |
09667aa4 MD |
30 | * @version 1.0 |
31 | * @author Mathieu Denis | |
79e08fd0 BH |
32 | */ |
33 | public abstract class AbsTmfStatisticsTree { | |
34 | ||
35 | /** | |
09667aa4 | 36 | * String builder used to merge string with more efficiency. |
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 | /** | |
09667aa4 | 46 | * Function to merge many string with more efficiency. |
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 BH |
177 | |
178 | /** | |
09667aa4 MD |
179 | * Register that a new node was created. |
180 | * | |
79e08fd0 BH |
181 | * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node |
182 | * will return the newly created node. | |
20ff3b75 | 183 | * |
79e08fd0 BH |
184 | * @param path |
185 | * Path of the new node. | |
186 | */ | |
187 | protected abstract void registerName(final TmfFixedArray<String> path); | |
188 | ||
189 | /** | |
09667aa4 MD |
190 | * Reset a node. |
191 | * | |
79e08fd0 | 192 | * Work recursively. |
20ff3b75 | 193 | * |
79e08fd0 BH |
194 | * @param path |
195 | * Path to the node. | |
196 | */ | |
197 | public void reset(final TmfFixedArray<String> path) { | |
198 | for (TmfStatisticsTreeNode node : getAllChildren(path)) { | |
199 | reset(node.getPath()); | |
200 | fNodes.remove(node.getPath()); | |
201 | } | |
202 | } | |
203 | } |