1 /*******************************************************************************
2 * Copyright (c) 2009 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 * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.resources
.model
;
14 import java
.util
.HashMap
;
15 import java
.util
.Iterator
;
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
;
22 * Common location to allocate the resources in use by the resource view
27 public class ResourceContainer
{
29 private final HashMap
<ResourceKey
, TimeRangeEventResource
> resources
= new HashMap
<ResourceKey
, TimeRangeEventResource
>();
30 private static Integer uniqueId
= 0;
33 * Package level constructor
35 public ResourceContainer() { }
38 * Interface to add resources.
42 public void addResource(TimeRangeEventResource newResource
) {
43 if (newResource
!= null) {
44 resources
.put( new ResourceKey(newResource
),newResource
);
54 public Integer
getUniqueId() {
59 * This method is intended for read only purposes in order to keep the
60 * internal data structure in Synch
64 public TimeRangeEventResource
[] readResources() {
65 return resources
.values().toArray(
66 new TimeRangeEventResource
[resources
.size()]);
70 * Clear the children information for resources related to a specific trace
71 * e.g. just before refreshing data with a new time range
75 public void clearChildren(String traceId
) {
77 TimeRangeEventResource newRes
= null;
78 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
80 while (iterator
.hasNext()) {
81 newRes
= resources
.get(iterator
.next());
83 if (newRes
.getTraceId().equals(traceId
)) {
90 * remove the resources related to a specific trace e.g. during trace
95 public void removeResources(String traceId
) {
96 ResourceKey newKey
= null;
98 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
99 while (iterator
.hasNext()) {
100 newKey
= iterator
.next();
102 if (resources
.get(newKey
).getTraceId().equals(traceId
)) {
103 resources
.remove(newKey
);
110 * Obtain a resource id from resource attributes<br>
112 * Note : Slow as hell and defeat the purpose of the map.
113 * This function probably shouldn't be used, except for testing.
116 public Long
findUniqueIdOfresource(Long startTime
, Long endTime
, String name
, String groupName
, String className
, ResourceTypes type
, String traceId
) {
120 ResourceKey newKey
= null;
121 TimeRangeEventResource newContent
= null;
123 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
124 while ( (iterator
.hasNext()) && (foundId
== null) ) {
125 newKey
= iterator
.next();
126 newContent
= resources
.get(newKey
);
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
) )
132 foundId
= newKey
.getResourceId();
141 * Search by keys (resourceId, traceId and type)<br>
143 * A match is returned if the three arguments received match an entry
144 * Otherwise null is returned
148 public TimeRangeEventResource
findResource(Long searchedId
, ResourceTypes searchedType
, String searchedTraceId
) {
150 TimeRangeEventResource foundResource
= null;
152 // Get the EventResource associated to a key we create here
153 TimeRangeEventResource tmpRes
= resources
.get( new ResourceKey(searchedId
, searchedTraceId
, searchedType
) );
155 if ( tmpRes
!= null ) {
156 foundResource
= tmpRes
;
159 return foundResource
;
165 * A match is returned if the four arguments received match an entry in the
166 * Otherwise null is returned
170 public TimeRangeEventResource
findResourceFilterByName(Long searchedId
, ResourceTypes searchedType
, String searchedTraceId
, String searchedName
) {
172 TimeRangeEventResource foundResource
= null;
174 // Get the EventResource asociated to a key we create here
175 TimeRangeEventResource tmpRes
= resources
.get( new ResourceKey(searchedId
, searchedTraceId
, searchedType
) );
177 if ( tmpRes
!= null ) {
178 if ( tmpRes
.getName().equals(searchedName
) ) {
179 foundResource
= tmpRes
;
183 return foundResource
;
187 * MAIN : For testing only!
189 public static void main(String
[] args
) {
191 System
.out
.println("**** TEST PART 1 WITH STANDALONE *** ");
192 HashMap
<ResourceKey
, String
> newMap
= new HashMap
<ResourceKey
, String
>();
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");
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
) );
203 // Test2 should return the same as Test1
204 System
.out
.println( "JOY JOY JOY == " + newMap
.get(test2
) );
209 System
.out
.println("**** TEST PART 2 WITH TimeRangeEventResource *** ");
211 TimeRangeResourceFactory rfactory
= TimeRangeResourceFactory
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);
218 ResourceKey test3
= new ResourceKey(tmpRes1
);
219 ResourceKey test4
= new ResourceKey(tmpRes2
);
221 newMap
.put(test3
, "BUG BUG BUG");
222 newMap
.put(test4
, "JOY JOY JOY");
224 // Test3 and Test4 return the same value!
225 System
.out
.println( newMap
.get(test3
) + " == " + newMap
.get(test4
) );
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
) );
237 private TimeRangeEventResource valueRef
= null;
239 private Long resourceId
= null;
240 private String traceId
= null;
241 private ResourceTypes type
= null;
243 @SuppressWarnings("unused")
244 private ResourceKey() { }
246 public ResourceKey(TimeRangeEventResource newRef
) {
250 public ResourceKey(Long newId
, String newTraceId
, ResourceTypes newType
) {
252 traceId
= newTraceId
;
257 public boolean equals(Object obj
) {
258 boolean isSame
= false;
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()) ) )
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();
283 public String
getTraceId() {
284 if ( valueRef
!= null ) {
285 return valueRef
.getTraceId();
292 public ResourceTypes
getType() {
293 if ( valueRef
!= null ) {
294 return valueRef
.getType();
302 public int hashCode() {
303 return this.toString().hashCode();
308 public String
toString() {
309 return (getResourceId().toString() + ":" + getTraceId().toString() + ":" + getType().toString());