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 * <h4>Base class for the statistics storage.</h4>
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.
33 * @author Mathieu Denis
35 public abstract class AbsTmfStatisticsTree
{
38 * <h4>String builder used to merge string with more efficacy.</h4>
40 protected static final StringBuilder fBuilder
= new StringBuilder();
42 * <h4>Identification of the root.</h4>
44 public static final TmfFixedArray
<String
> ROOT
= new TmfFixedArray
<String
>("root"); //$NON-NLS-1$
47 * <h4>Function to merge many string with more efficacy.</h4>
51 * @return A new string containing all the strings.
53 public synchronized static String
mergeString(String
... strings
) {
54 fBuilder
.setLength(0);
55 for (String s
: strings
) {
58 return fBuilder
.toString();
62 * <h4>Define what child a node can have.</h4>
64 * The management and usage of this map is done by subclass.
67 * HashSet are always faster than TreeSet.
70 protected Map
<String
, Set
<String
>> fKeys
;
72 * <h4>The nodes in the tree.</f4>
74 protected HashMap
<TmfFixedArray
<String
>, TmfStatisticsTreeNode
> fNodes
;
77 * <h4>Constructor.</h4>
79 public AbsTmfStatisticsTree() {
80 fNodes
= new HashMap
<TmfFixedArray
<String
>, TmfStatisticsTreeNode
>();
81 fKeys
= new HashMap
<String
, Set
<String
>>();
85 * <h4>Get a node.</h4>
89 * @return The node or null.
91 public TmfStatisticsTreeNode
get(final TmfFixedArray
<String
> path
) {
92 return fNodes
.get(path
);
96 * <h4>Get the children of a node.</h4>
100 * @return Collection containing the children.
102 public abstract Collection
<TmfStatisticsTreeNode
> getChildren(final TmfFixedArray
<String
> path
);
105 * <h4>Get every children of a node, even if it doesn't have any registered events, as opposed to getChildren</h4>
109 * @return Collection containing all the children.
111 public abstract Collection
<TmfStatisticsTreeNode
> getAllChildren(final TmfFixedArray
<String
> path
);
114 * <h4>Get the map of existing elements of path classified by parent.</h4>
118 public Map
<String
, Set
<String
>> getKeys() {
123 * <h4>Get or create a node.</h4>
129 public TmfStatisticsTreeNode
getOrCreate(final TmfFixedArray
<String
> path
) {
130 TmfStatisticsTreeNode current
= fNodes
.get(path
);
131 if (current
== null) {
133 current
= new TmfStatisticsTreeNode(path
, this);
134 fNodes
.put(path
, current
);
140 * <h4>Get the parent of a node.</h4>
144 * @return Parent node or null.
146 public TmfStatisticsTreeNode
getParent(final TmfFixedArray
<String
> path
) {
147 if (path
.size() == 1) {
148 if (path
.equals(ROOT
)) {
154 return get(path
.subArray(0, path
.size() - 1));
158 * <h4>Increase any kind of counter.</h4>
160 * This method must be implemented by subclass.
165 * Extra information to pass along with the event.
169 public abstract void increase(ITmfEvent event
, ITmfExtraEventInfo extraInfo
, int values
);
172 * <h4>Register an event.</h4>
174 * This method must be implemented by subclass.
179 * Extra information to pass along with the event.
181 public abstract void registerEvent(ITmfEvent event
, ITmfExtraEventInfo extraInfo
);
184 * <h4>Register that a new node was created.</h4>
186 * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node
187 * will return the newly created node.
191 * Path of the new node.
193 protected abstract void registerName(final TmfFixedArray
<String
> path
);
196 * <h4>Reset a node.</h4>
204 public void reset(final TmfFixedArray
<String
> path
) {
205 for (TmfStatisticsTreeNode node
: getAllChildren(path
)) {
206 reset(node
.getPath());
207 fNodes
.remove(node
.getPath());