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