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