tmf: Use tabs in statistics view for each traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / internal / lttng / core / latency / analyzer / StackWrapper.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 * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation
11 * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code
12 *******************************************************************************/
13 package org.eclipse.linuxtools.internal.lttng.core.latency.analyzer;
14
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.Set;
19 import java.util.Stack;
20
21 import org.eclipse.linuxtools.internal.lttng.core.Activator;
22 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent;
23
24 /**
25 * <b><u>StackWrapper</u></b>
26 * <p>
27 * Stack pile.
28 *
29 * TODO Change the types of the HashMaps from <String,String> to <Integer,Integer>, in order to take advantage of the
30 * compilation-time String.hashCode() speedup over execution-time String hash computation.
31 *
32 * @author Philippe Sawicki
33 */
34 public class StackWrapper {
35
36 // ------------------------------------------------------------------------
37 // Attributes
38 // ------------------------------------------------------------------------
39
40 /**
41 * Hash map of event stacks.
42 */
43 private HashMap<String, Stack<LttngEvent>> fStacks = null;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Constructor.
51 */
52 public StackWrapper() {
53 fStacks = new HashMap<String, Stack<LttngEvent>>();
54 }
55
56 // ------------------------------------------------------------------------
57 // Operations
58 // ------------------------------------------------------------------------
59
60 /**
61 * Adds an event to the list of events of the same type.
62 * @param event
63 * The event to add to the list.
64 */
65 public void put(LttngEvent event) {
66 String key = event.getMarkerName();
67
68 if (fStacks.containsKey(key)) {
69 fStacks.get(key).add(event);
70 } else {
71 Stack<LttngEvent> newStack = new Stack<LttngEvent>();
72 newStack.add(event);
73 fStacks.put(key, newStack);
74 }
75 }
76
77 /**
78 * Checks if the stack contains a list of events of the given type.
79 * @param key
80 * The type of events to check for.
81 * @return "true" if the stack contains events of the given type, "false" otherwise.
82 */
83 public boolean containsKey(String key) {
84 return fStacks.containsKey(key);
85 }
86
87 /**
88 * Returns the list of events of the given type.
89 * @param key
90 * The type of events to return.
91 * @return The list of events of the given type, or null.
92 */
93 public Stack<LttngEvent> getStackOf(String key) {
94 return fStacks.get(key);
95 }
96
97 /**
98 * Removes the given event from the given stack list.
99 * @param key
100 * The given stack type.
101 * @param event
102 * The event to remove from the given stack type.
103 * @return "true" if the event was removed, "false" otherwise.
104 */
105 public boolean removeEvent(String key, LttngEvent event) {
106 Stack<LttngEvent> stack = fStacks.get(key);
107
108 boolean removed = false;
109
110 try {
111 /**
112 * TODO Correct this... Here, no matter what CPU or other content field, we always remove the last event
113 * added to the stack. Should be something like : return stack.remove(event);
114 */
115 stack.pop();
116 removed = true;
117 } catch (Exception e) {
118 Activator.getDefault().logError("Error removing Event", e); //$NON-NLS-1$
119 }
120
121 // Remove the stack from the stack list if it is empty
122 if (stack.isEmpty()) {
123 fStacks.remove(key);
124 }
125
126 return removed;
127 }
128
129 /**
130 * Clears the stack content.
131 */
132 public void clear() {
133 fStacks.clear();
134 }
135
136 /**
137 * Prints the content of the stack to the console.
138 */
139 @SuppressWarnings("nls")
140 public void printContent() {
141 Collection<Stack<LttngEvent>> values = fStacks.values();
142 Iterator<Stack<LttngEvent>> valueIt = values.iterator();
143
144 Set<String> keys = fStacks.keySet();
145 Iterator<String> keyIt = keys.iterator();
146
147 while (valueIt.hasNext() && keyIt.hasNext()) {
148 Stack<LttngEvent> stack = valueIt.next();
149
150 System.out.println(" " + keyIt.next() + " [" + stack.size() + "] : " + stack);
151 }
152 }
153 }
This page took 0.035091 seconds and 5 git commands to generate.