1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Implementation and Initial API
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
;
16 import java
.util
.Collection
;
17 import java
.util
.HashMap
;
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
;
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.
31 * @author Mathieu Denis
33 public abstract class AbsTmfStatisticsTree
{
36 * String builder used to merge string more efficienctly.
38 protected static final StringBuilder fBuilder
= new StringBuilder();
41 * Identification of the root.
43 public static final TmfFixedArray
<String
> ROOT
= new TmfFixedArray
<String
>("root"); //$NON-NLS-1$
46 * Function to merge many string more efficienctly.
50 * @return A new string containing all the strings.
52 public synchronized static String
mergeString(String
... strings
) {
53 fBuilder
.setLength(0);
54 for (String s
: strings
) {
57 return fBuilder
.toString();
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.
64 protected Map
<String
, Set
<String
>> fKeys
;
67 * The nodes in the tree.
69 protected HashMap
<TmfFixedArray
<String
>, TmfStatisticsTreeNode
> fNodes
;
74 public AbsTmfStatisticsTree() {
75 fNodes
= new HashMap
<TmfFixedArray
<String
>, TmfStatisticsTreeNode
>();
76 fKeys
= new HashMap
<String
, Set
<String
>>();
84 * @return The node or null.
86 public TmfStatisticsTreeNode
get(final TmfFixedArray
<String
> path
) {
87 return fNodes
.get(path
);
91 * Get the children of a node.
95 * @return Collection containing the children.
97 public abstract Collection
<TmfStatisticsTreeNode
> getChildren(final TmfFixedArray
<String
> path
);
100 * Get every children of a node, even if it doesn't have any registered
101 * events, as opposed to getChildren
105 * @return Collection containing all the children.
107 public abstract Collection
<TmfStatisticsTreeNode
> getAllChildren(final TmfFixedArray
<String
> path
);
110 * Get the map of existing elements of path classified by parent.
114 public Map
<String
, Set
<String
>> getKeys() {
119 * Get or create a node.
125 public TmfStatisticsTreeNode
getOrCreate(final TmfFixedArray
<String
> path
) {
126 TmfStatisticsTreeNode current
= fNodes
.get(path
);
127 if (current
== null) {
129 current
= new TmfStatisticsTreeNode(path
, this);
130 fNodes
.put(path
, current
);
136 * Get the parent of a node.
140 * @return Parent node or null.
142 public TmfStatisticsTreeNode
getParent(final TmfFixedArray
<String
> path
) {
143 if (path
.size() == 1) {
144 if (path
.equals(ROOT
)) {
149 return get(path
.subArray(0, path
.size() - 1));
153 * Increase any kind of counter.
155 * This method must be implemented by subclasses.
160 * Extra information to pass along with the event.
164 public abstract void increase(ITmfEvent event
, ITmfExtraEventInfo extraInfo
, int values
);
169 * This method must be implemented by subclasses.
174 * Extra information to pass along with the event.
176 public abstract void registerEvent(ITmfEvent event
, ITmfExtraEventInfo extraInfo
);
179 * Register an event within a time range.
181 * This method must be implemented by subclasses.
186 * Extra information to pass along with the event.
189 public abstract void registerEventInTimeRange(ITmfEvent event
, ITmfExtraEventInfo extraInfo
);
192 * Register that a new node was created.
194 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
195 * will return the newly created node.
198 * Path of the new node.
200 protected abstract void registerName(final TmfFixedArray
<String
> path
);
210 public void reset(final TmfFixedArray
<String
> path
) {
211 for (TmfStatisticsTreeNode node
: getAllChildren(path
)) {
212 reset(node
.getPath());
213 fNodes
.remove(node
.getPath());
218 * Reset the time range value of a node.
226 public void resetTimeRangeValue(final TmfFixedArray
<String
> path
) {
227 for (TmfStatisticsTreeNode node
: getChildren(path
)) {
228 resetTimeRangeValue(node
.getPath());
229 node
.resetTimeRangeValue();