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 - 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
.TraceDebug
;
18 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.TimeRangeEventResource
;
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
{
28 // ========================================================================
30 // ========================================================================
31 private final HashMap
<ResourceKey
, TimeRangeEventResource
> resources
= new HashMap
<ResourceKey
, TimeRangeEventResource
>();
32 private static Integer uniqueId
= 0;
35 // ========================================================================
37 // ========================================================================
39 * Package level constructor
41 public ResourceContainer() { }
44 * Interface to add resources.
48 public void addResource(TimeRangeEventResource newResource
) {
49 if (newResource
!= null) {
50 resources
.put( new ResourceKey(newResource
),newResource
);
54 // ========================================================================
56 // ========================================================================
62 public Integer
getUniqueId() {
67 * This method is intended for read only purposes in order to keep the
68 * internal data structure in Synch
72 public TimeRangeEventResource
[] readResources() {
73 return resources
.values().toArray(
74 new TimeRangeEventResource
[resources
.size()]);
78 * Clear the children information for resources related to a specific trace
79 * e.g. just before refreshing data with a new time range
83 public void clearChildren(String traceId
) {
84 TimeRangeEventResource newRes
= null;
85 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
87 while (iterator
.hasNext()) {
88 newRes
= resources
.get(iterator
.next());
90 if (newRes
.getTraceId().equals(traceId
)) {
97 * Clear all resources items e.g. when a new experiment is selected
99 public void clearResources() {
104 * Remove the resources related to a specific trace e.g. during trace
109 public void removeResources(String traceId
) {
110 ResourceKey newKey
= null;
112 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
113 while (iterator
.hasNext()) {
114 newKey
= iterator
.next();
116 if (resources
.get(newKey
).getTraceId().equals(traceId
)) {
117 resources
.remove(newKey
);
124 * Search by keys (resourceId, traceId and type)<p>
126 * A match is returned if the three arguments received match an entry
127 * Otherwise null is returned
129 * @param searchedId The ressourceId we are looking for
130 * @param searchedType The ressourceType we are looking for
131 * @param searchedTraceId The traceId (trace name?) we are looking for
133 * @return TimeRangeEventResource
135 public TimeRangeEventResource
findResource(Long searchedId
, ResourceTypes searchedType
, String searchedTraceId
) {
136 // Get the EventResource associated to a key we create here
137 TimeRangeEventResource foundResource
= resources
.get( new ResourceKey(searchedId
, searchedTraceId
, searchedType
) );
139 return foundResource
;
145 private TimeRangeEventResource valueRef
= null;
147 private Long resourceId
= null;
148 private String traceId
= null;
149 private ResourceTypes type
= null;
151 @SuppressWarnings("unused")
152 private ResourceKey() { }
154 public ResourceKey(TimeRangeEventResource newRef
) {
158 public ResourceKey(Long newId
, String newTraceId
, ResourceTypes newType
) {
160 traceId
= newTraceId
;
165 public boolean equals(Object obj
) {
166 boolean isSame
= false;
168 if ( obj
instanceof ResourceKey
) {
169 if ( valueRef
!= null ) {
170 if ( ( ((ResourceKey
)obj
).getResourceId().equals(valueRef
.getResourceId()) ) &&
171 ( ((ResourceKey
)obj
).getTraceId().equals(valueRef
.getTraceId()) ) &&
172 ( ((ResourceKey
)obj
).getType().equals(valueRef
.getType()) ) )
178 if ( ( ((ResourceKey
)obj
).getResourceId().equals(this.resourceId
)) &&
179 ( ((ResourceKey
)obj
).getTraceId().equals(this.traceId
)) &&
180 ( ((ResourceKey
)obj
).getType().equals(this.traceId
)) )
187 TraceDebug
.debug("ERROR : The given key is not of the type ProcessKey!" + obj
.getClass().toString());
193 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
194 // Do NOT change this check without checking.
195 public Long
getResourceId() {
196 if ( valueRef
!= null ) {
197 return valueRef
.getResourceId();
204 public String
getTraceId() {
205 if ( valueRef
!= null ) {
206 return valueRef
.getTraceId();
213 public ResourceTypes
getType() {
214 if ( valueRef
!= null ) {
215 return valueRef
.getType();
223 public int hashCode() {
224 return this.toString().hashCode();
229 public String
toString() {
230 if ( valueRef
!= null ) {
231 return (valueRef
.getResourceId().toString() + ":" + valueRef
.getTraceId().toString() + ":" + valueRef
.getType().toString());
233 return (resourceId
+ ":" + traceId
+ ":" + type
);