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