1 /*******************************************************************************
2 * Copyright (c) 2011 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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
.model
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
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
;
25 * <h4>Base class for the statistics storage.</h4>
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.
31 public abstract class AbsTmfStatisticsTree
{
34 * <h4>String builder used to merge string with more efficacy.</h4>
36 protected static StringBuilder fBuilder
= new StringBuilder();
38 * <h4>Identification of the root.</h4>
40 public static final TmfFixedArray
<String
> ROOT
= new TmfFixedArray
<String
>("root"); //$NON-NLS-1$
43 * <h4>Function to merge many string with more efficacy.</h4>
47 * @return A new string containing all the strings.
49 public synchronized static String
mergeString(String
... strings
) {
50 fBuilder
.setLength(0);
51 for (String s
: strings
)
53 return fBuilder
.toString();
57 * <h4>Define what child a node can have.</h4>
59 * The management and usage of this map is done by subclass.
62 * HashSet are always faster than TreeSet.
65 protected Map
<String
, Set
<String
>> fKeys
;
67 * <h4>The nodes in the tree.</f4>
69 protected HashMap
<TmfFixedArray
<String
>, TmfStatisticsTreeNode
> fNodes
;
72 * <h4>Constructor.</h4>
74 public AbsTmfStatisticsTree() {
75 fNodes
= new HashMap
<TmfFixedArray
<String
>, TmfStatisticsTreeNode
>();
76 fKeys
= new HashMap
<String
, Set
<String
>>();
80 * <h4>Get a node.</h4>
84 * @return The node or null.
86 public TmfStatisticsTreeNode
get(final TmfFixedArray
<String
> path
) {
87 return fNodes
.get(path
);
91 * <h4>Get the children of a node.</h4>
95 * @return Collection containing the children.
97 public abstract Collection
<TmfStatisticsTreeNode
> getChildren(final TmfFixedArray
<String
> path
);
100 * <h4>Get every children of a node, even if it doesn't have any registered events, as opposed to getChildren</h4>
104 * @return Collection containing all the children.
106 public abstract Collection
<TmfStatisticsTreeNode
> getAllChildren(final TmfFixedArray
<String
> path
);
109 * <h4>Get the map of existing elements of path classified by parent.</h4>
113 public Map
<String
, Set
<String
>> getKeys() {
118 * <h4>Get or create a node.</h4>
124 public TmfStatisticsTreeNode
getOrCreate(final TmfFixedArray
<String
> path
) {
125 TmfStatisticsTreeNode current
= fNodes
.get(path
);
126 if (current
== null) {
128 current
= new TmfStatisticsTreeNode(path
, this);
129 fNodes
.put(path
, current
);
135 * <h4>Get the parent of a node.</h4>
139 * @return Parent node or null.
141 public TmfStatisticsTreeNode
getParent(final TmfFixedArray
<String
> path
) {
142 if (path
.size() == 1) {
143 if (path
.equals(ROOT
))
148 return get(path
.subArray(0, path
.size() - 1));
152 * <h4>Increase any kind of counter.</h4>
154 * This method must be implemented by subclass.
159 * Extra information to pass along with the event.
163 public abstract void increase(TmfEvent event
, ITmfExtraEventInfo extraInfo
, int values
);
166 * <h4>Register an event.</h4>
168 * This method must be implemented by subclass.
173 * Extra information to pass along with the event.
175 public abstract void registerEvent(TmfEvent event
, ITmfExtraEventInfo extraInfo
);
178 * <h4>Register that a new node was created.</h4>
180 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
181 * will return the newly created node.
185 * Path of the new node.
187 protected abstract void registerName(final TmfFixedArray
<String
> path
);
190 * <h4>Reset a node.</h4>
198 public void reset(final TmfFixedArray
<String
> path
) {
199 for (TmfStatisticsTreeNode node
: getAllChildren(path
)) {
200 reset(node
.getPath());
201 fNodes
.remove(node
.getPath());