Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / statistics / model / TmfBaseStatisticsTree.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> - Initial API and Implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.statistics.model;
14
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.LinkedList;
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 * Store information about base statistics data.
27 *
28 * This class provides a way to represent statistics data that is compatible
29 * with every type of trace.
30 *
31 * @version 1.0
32 * @author Mathieu Denis
33 */
34 public class TmfBaseStatisticsTree extends AbsTmfStatisticsTree {
35
36 /**
37 * Header for the event types categories.
38 */
39 public static final String HEADER_EVENT_TYPES = Messages.TmfStatisticsData_EventTypes;
40
41 /**
42 * Indicate that it's a value.
43 *
44 * Used when checking the possible child node for a node.
45 *
46 * It differentiate a category of a value by being appended to a value.
47 */
48 protected static final String NODE = "z"; //$NON-NLS-1$
49
50 /**
51 * Root node key.
52 */
53 protected static final String ROOT_NODE_KEY = mergeString(ROOT.get(0), NODE);
54
55 /**
56 * Default constructor. Creates base statistics tree for counting total
57 * number of events and number of events per event type.
58 */
59 public TmfBaseStatisticsTree() {
60 super();
61 Map<String, Set<String>> keys = getKeys();
62
63 // //////////// Adding category sets
64 // common
65 keys.put(HEADER_EVENT_TYPES, new HashSet<String>());
66
67 // /////////// Adding value sets
68 // Under a trace
69 Set<String> temp = new HashSet<String>(8);
70 temp.add(HEADER_EVENT_TYPES);
71 keys.put(ROOT_NODE_KEY, temp);
72 // Under an event type
73 temp = new HashSet<String>(16);
74 keys.put(mergeString(HEADER_EVENT_TYPES, NODE), temp);
75
76 // //////////// CREATE root
77 keys.put(ROOT.get(0), new HashSet<String>(2)); // 1 trace at the time
78 getOrCreate(ROOT);
79 }
80
81 /*
82 * (non-Javadoc)
83 *
84 * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#getChildren(org.eclipse.linuxtools.tmf.util.TmfFixedArray)
85 */
86 @Override
87 public Collection<TmfStatisticsTreeNode> getChildren(TmfFixedArray<String> path) {
88 LinkedList<TmfStatisticsTreeNode> result = new LinkedList<TmfStatisticsTreeNode>();
89
90 if (path.size() % 2 == 0) { // if we are at a Category
91 TmfStatisticsTreeNode current = null;
92 for (String value : getKeys().get(path.get(path.size() - 1))) {
93 current = get(path.append(value));
94 if (current != null && current.getValue().nbEvents != 0) {
95 result.add(current);
96 }
97 }
98 } else if (path.size() == 1) { // Special case.
99 if (path.equals(ROOT)) {
100 for (String value : getKeys().get(ROOT.get(0))) {
101 result.add(getOrCreate(new TmfFixedArray<String>(value)));
102 }
103 } else {
104 // Get value under the root
105 for (String value : getKeys().get(ROOT_NODE_KEY)) {
106 result.add(getOrCreate(path.append(value)));
107 }
108 }
109 } else {// If we are at a value
110 for (String value : getKeys().get(mergeString(path.get(path.size() - 2), NODE))) {
111 // Search the parent name + NODE
112 result.add(getOrCreate(path.append(value)));
113 }
114 }
115
116 return result;
117 }
118
119 /*
120 * (non-Javadoc)
121 *
122 * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#getAllChildren(org.eclipse.linuxtools.tmf.util.TmfFixedArray)
123 */
124 @Override
125 public Collection<TmfStatisticsTreeNode> getAllChildren(TmfFixedArray<String> path) {
126 LinkedList<TmfStatisticsTreeNode> result = new LinkedList<TmfStatisticsTreeNode>();
127
128 if (path.size() % 2 == 0) { // if we are at a Category
129 TmfStatisticsTreeNode current = null;
130 for (String value : getKeys().get(path.get(path.size() - 1))) {
131 current = get(path.append(value));
132 if (current != null) {
133 result.add(current);
134 }
135 }
136 } else if (path.size() == 1) { // Special case.
137 if (path.equals(ROOT)) {
138 for (String value : getKeys().get(ROOT.get(0))) {
139 result.add(getOrCreate(new TmfFixedArray<String>(value)));
140 }
141 } else {
142 // Get value under the root
143 for (String value : getKeys().get(ROOT_NODE_KEY)) {
144 result.add(getOrCreate(path.append(value)));
145 }
146 }
147 } else {// If we are at a value
148 for (String value : getKeys().get(mergeString(path.get(path.size() - 2), NODE))) {
149 // Search the parent name + NODE
150 result.add(getOrCreate(path.append(value)));
151 }
152 }
153 return result;
154 }
155
156 /**
157 * Get the event types paths.
158 *
159 * @param event
160 * Event to get the path for.
161 * @param extraInfo
162 * Extra information to pass along with the event
163 * @return Array of FixedArray representing the paths.
164 */
165 @SuppressWarnings({ "rawtypes", "unchecked" })
166 protected TmfFixedArray<String>[] getTypePaths(ITmfEvent event, ITmfExtraEventInfo extraInfo) {
167 String trace = extraInfo.getTraceName();
168 // String type = event.getType().getTypeId(); // Add too much
169 // informations
170 String type = event.getType().toString();
171
172 TmfFixedArray[] paths = { new TmfFixedArray<String>(trace, HEADER_EVENT_TYPES, type) };
173
174 return paths;
175 }
176
177 /**
178 * Get the standard paths for an event.
179 *
180 * @param event
181 * Event to get the path for.
182 * @param extraInfo
183 * Extra information to pass along with the event
184 * @return Array of FixedArray representing the paths.
185 */
186 @SuppressWarnings({ "rawtypes", "unchecked" })
187 protected TmfFixedArray<String>[] getNormalPaths(ITmfEvent event, ITmfExtraEventInfo extraInfo) {
188 String trace = extraInfo.getTraceName();
189
190 TmfFixedArray[] paths = { new TmfFixedArray<String>(trace) };
191 return paths;
192 }
193
194 /*
195 * (non-Javadoc)
196 *
197 * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#increase(org.eclipse.linuxtools.tmf.event.TmfEvent, org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfEventInfo, int)
198 */
199 @Override
200 public void increase(ITmfEvent event, ITmfExtraEventInfo extraInfo, int values) {
201 // Do nothing
202 }
203
204 /*
205 * (non-Javadoc)
206 *
207 * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#registerEvent(org.eclipse.linuxtools.tmf.event.TmfEvent, org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfEventInfo)
208 */
209 @Override
210 public void registerEvent(ITmfEvent event, ITmfExtraEventInfo extraInfo) {
211 TmfFixedArray<String>[] paths = getNormalPaths(event, extraInfo);
212 for (TmfFixedArray<String> path : paths) {
213 ++(getOrCreate(path).getValue().nbEvents);
214 }
215
216 paths = getTypePaths(event, extraInfo);
217 for (TmfFixedArray<String> path : paths) {
218 ++(getOrCreate(path).getValue().nbEvents);
219 }
220 }
221
222 /*
223 * (non-Javadoc)
224 *
225 * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree#registerName(org.eclipse.linuxtools.tmf.core.util.TmfFixedArray)
226 */
227 @Override
228 protected void registerName(TmfFixedArray<String> path) {
229 if (path.size() == 1) {
230 if (!path.equals(ROOT)) {
231 getKeys().get(ROOT.get(0)).add(path.get(0));
232 }
233 } else if (path.size() % 2 != 0) {
234 getKeys().get(path.get(path.size() - 2)).add(path.get(path.size() - 1));
235 }
236 }
237 }
This page took 0.037831 seconds and 6 git commands to generate.