[219097] LTTng updates
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / resources / model / ResourceContainer.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.resources.model;
13
14 import java.util.HashMap;
15 import java.util.Iterator;
16
17 import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeEventResource;
18 import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeResourceFactory;
19 import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeEventResource.ResourceTypes;
20
21 /**
22 * Common location to allocate the resources in use by the resource view
23 *
24 * @author alvaro
25 *
26 */
27 public class ResourceContainer {
28
29 private final HashMap<ResourceKey, TimeRangeEventResource> resources = new HashMap<ResourceKey, TimeRangeEventResource>();
30 private static Integer uniqueId = 0;
31
32 /**
33 * Package level constructor
34 */
35 public ResourceContainer() { }
36
37 /**
38 * Interface to add resources.
39 *
40 * @param process
41 */
42 public void addResource(TimeRangeEventResource newResource) {
43 if (newResource != null) {
44 resources.put( new ResourceKey(newResource),newResource);
45 }
46 }
47
48
49 /**
50 * Request a unique ID
51 *
52 * @return Integer
53 */
54 public Integer getUniqueId() {
55 return uniqueId++;
56 }
57
58 /**
59 * This method is intended for read only purposes in order to keep the
60 * internal data structure in Synch
61 *
62 * @return
63 */
64 public TimeRangeEventResource[] readResources() {
65 return resources.values().toArray(
66 new TimeRangeEventResource[resources.size()]);
67 }
68
69 /**
70 * Clear the children information for resources related to a specific trace
71 * e.g. just before refreshing data with a new time range
72 *
73 * @param traceId
74 */
75 public void clearChildren(String traceId) {
76
77 TimeRangeEventResource newRes = null;
78 Iterator<ResourceKey> iterator = resources.keySet().iterator();
79
80 while (iterator.hasNext()) {
81 newRes = resources.get(iterator.next());
82
83 if (newRes.getTraceId().equals(traceId)) {
84 newRes.reset();
85 }
86 }
87 }
88
89 /**
90 * remove the resources related to a specific trace e.g. during trace
91 * removal
92 *
93 * @param traceId
94 */
95 public void removeResources(String traceId) {
96 ResourceKey newKey = null;
97
98 Iterator<ResourceKey> iterator = resources.keySet().iterator();
99 while (iterator.hasNext()) {
100 newKey = iterator.next();
101
102 if (resources.get(newKey).getTraceId().equals(traceId)) {
103 resources.remove(newKey);
104 }
105 }
106 }
107
108
109 /**
110 * Obtain a resource id from resource attributes<br>
111 * <br>
112 * Note : Slow as hell and defeat the purpose of the map.
113 * This function probably shouldn't be used, except for testing.
114 *
115 */
116 public Long findUniqueIdOfresource(Long startTime, Long endTime, String name, String groupName, String className, ResourceTypes type, String traceId) {
117
118 Long foundId = null;
119
120 ResourceKey newKey = null;
121 TimeRangeEventResource newContent = null;
122
123 Iterator<ResourceKey> iterator = resources.keySet().iterator();
124 while ( (iterator.hasNext()) && (foundId == null) ) {
125 newKey = iterator.next();
126 newContent = resources.get(newKey);
127
128 if ( ( newContent.getStartTime() == startTime ) && ( newContent.getStopTime() == endTime ) && ( newContent.getName() == name ) &&
129 ( newContent.getGroupName() == groupName ) && ( newContent.getClassName() == className ) && ( newContent.getType() == type ) &&
130 ( newContent.getTraceId() == traceId ) )
131 {
132 foundId = newKey.getResourceId();
133 }
134 }
135
136 return foundId;
137
138 }
139
140 /**
141 * Search by keys (resourceId, traceId and type)<br>
142 * <br>
143 * A match is returned if the three arguments received match an entry
144 * Otherwise null is returned
145 *
146 * @return
147 */
148 public TimeRangeEventResource findResource(Long searchedId, ResourceTypes searchedType, String searchedTraceId) {
149
150 TimeRangeEventResource foundResource = null;
151
152 // Get the EventResource associated to a key we create here
153 TimeRangeEventResource tmpRes = resources.get( new ResourceKey(searchedId, searchedTraceId, searchedType) );
154
155 if ( tmpRes != null ) {
156 foundResource = tmpRes;
157 }
158
159 return foundResource;
160 }
161
162 /**
163 * Search by name<br>
164 * <br>
165 * A match is returned if the four arguments received match an entry in the
166 * Otherwise null is returned
167 *
168 * @return
169 */
170 public TimeRangeEventResource findResourceFilterByName(Long searchedId, ResourceTypes searchedType, String searchedTraceId, String searchedName) {
171
172 TimeRangeEventResource foundResource = null;
173
174 // Get the EventResource asociated to a key we create here
175 TimeRangeEventResource tmpRes = resources.get( new ResourceKey(searchedId, searchedTraceId, searchedType) );
176
177 if ( tmpRes != null ) {
178 if ( tmpRes.getName().equals(searchedName) ) {
179 foundResource = tmpRes;
180 }
181 }
182
183 return foundResource;
184 }
185
186 /*
187 * MAIN : For testing only!
188 */
189 public static void main(String[] args) {
190
191 System.out.println("**** TEST PART 1 WITH STANDALONE *** ");
192 HashMap<ResourceKey, String> newMap = new HashMap<ResourceKey, String>();
193
194 ResourceKey test1 = new ResourceKey(0L,"trace1", ResourceTypes.CPU);
195 ResourceKey test2 = new ResourceKey(0L, "trace1", ResourceTypes.CPU);
196 newMap.put(test2, "BUG BUG BUG");
197 newMap.put(test1, "JOY JOY JOY");
198
199 // Test1 and TestKey return the same value!
200 ResourceKey testKey = new ResourceKey(0L, "trace1", ResourceTypes.CPU);
201 System.out.println( newMap.get(testKey) + " == " + newMap.get(test1) );
202
203 // Test2 should return the same as Test1
204 System.out.println( "JOY JOY JOY == " + newMap.get(test2) );
205
206
207
208
209 System.out.println("**** TEST PART 2 WITH TimeRangeEventResource *** ");
210 newMap.clear();
211 TimeRangeResourceFactory rfactory = TimeRangeResourceFactory
212 .getInstance();
213 TimeRangeEventResource tmpRes1 = rfactory.createResource(0, 0, 0,
214 "name1", "trace1", "classname1", ResourceTypes.CPU, 0L, 0l);
215 TimeRangeEventResource tmpRes2 = rfactory.createResource(0, 0, 0,
216 "name2", "trace1", "classname2", ResourceTypes.CPU, 0L, 0l);
217
218 ResourceKey test3 = new ResourceKey(tmpRes1);
219 ResourceKey test4 = new ResourceKey(tmpRes2);
220
221 newMap.put(test3, "BUG BUG BUG");
222 newMap.put(test4, "JOY JOY JOY");
223
224 // Test3 and Test4 return the same value!
225 System.out.println( newMap.get(test3) + " == " + newMap.get(test4) );
226
227
228 ResourceKey testKey2 = new ResourceKey(0L, "trace1", ResourceTypes.CPU);
229 // TestKey2 should return the same as Test3 AND Test4
230 System.out.println( newMap.get(test4) + " == " + newMap.get(test4) + " == " + newMap.get(testKey2) );
231 }
232 }
233
234
235 class ResourceKey {
236
237 private TimeRangeEventResource valueRef = null;
238
239 private Long resourceId = null;
240 private String traceId = null;
241 private ResourceTypes type = null;
242
243 @SuppressWarnings("unused")
244 private ResourceKey() { }
245
246 public ResourceKey(TimeRangeEventResource newRef) {
247 valueRef = newRef;
248 }
249
250 public ResourceKey(Long newId, String newTraceId, ResourceTypes newType) {
251 resourceId = newId;
252 traceId = newTraceId;
253 type = newType;
254 }
255
256 @Override
257 public boolean equals(Object obj) {
258 boolean isSame = false;
259
260 if ( obj instanceof ResourceKey ) {
261 if ( ( ((ResourceKey)obj).getResourceId().equals(this.getResourceId()) ) &&
262 ( ((ResourceKey)obj).getTraceId().equals(this.getTraceId()) ) &&
263 ( ((ResourceKey)obj).getType().equals(this.getType()) ) )
264 {
265 isSame = true;
266 }
267 }
268
269 return isSame;
270 }
271
272 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
273 // Do NOT change this check without checking.
274 public Long getResourceId() {
275 if ( valueRef != null ) {
276 return valueRef.getResourceId();
277 }
278 else {
279 return resourceId;
280 }
281 }
282
283 public String getTraceId() {
284 if ( valueRef != null ) {
285 return valueRef.getTraceId();
286 }
287 else {
288 return traceId;
289 }
290 }
291
292 public ResourceTypes getType() {
293 if ( valueRef != null ) {
294 return valueRef.getType();
295 }
296 else {
297 return type;
298 }
299 }
300
301 @Override
302 public int hashCode() {
303 return this.toString().hashCode();
304 }
305
306
307 @Override
308 public String toString() {
309 return (getResourceId().toString() + ":" + getTraceId().toString() + ":" + getType().toString());
310
311 }
312 }
313
This page took 0.037702 seconds and 5 git commands to generate.