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 * Michel Dagenais (michel.dagenais@polymtl.ca) - Reference C implementation, used with permission
12 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.resources
.model
;
15 import java
.util
.HashMap
;
16 import java
.util
.Iterator
;
18 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.TraceDebug
;
19 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.model
.trange
.ItemContainer
;
20 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.model
.trange
.TimeRangeEventResource
;
21 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.model
.trange
.TimeRangeEventResource
.ResourceTypes
;
24 * Common location to allocate the resources in use by the resource view
29 public class ResourceContainer
implements ItemContainer
<TimeRangeEventResource
> {
30 // ========================================================================
32 // ========================================================================
33 private final HashMap
<ResourceKey
, TimeRangeEventResource
> resources
= new HashMap
<ResourceKey
, TimeRangeEventResource
>();
34 private static Integer uniqueId
= 0;
37 // ========================================================================
39 // ========================================================================
41 * Package level constructor
43 public ResourceContainer() { }
50 * org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#addItem
51 * (org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.
52 * ITmfTimeAnalysisEntry)
55 public void addItem(TimeRangeEventResource newItem
) {
56 if (newItem
!= null) {
57 resources
.put( new ResourceKey(newItem
),newItem
);
61 // ========================================================================
63 // ========================================================================
65 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#getUniqueId()
68 public Integer
getUniqueId() {
73 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#readItems()
76 public TimeRangeEventResource
[] readItems() {
77 return resources
.values().toArray(
78 new TimeRangeEventResource
[resources
.size()]);
82 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearChildren()
85 public void clearChildren() {
86 TimeRangeEventResource newRes
= null;
87 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
89 while (iterator
.hasNext()) {
90 newRes
= resources
.get(iterator
.next());
96 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearItems()
99 public void clearItems() {
104 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#removeItems(java.lang.String)
107 public void removeItems(String traceId
) {
108 ResourceKey newKey
= null;
110 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
111 while (iterator
.hasNext()) {
112 newKey
= iterator
.next();
114 if (resources
.get(newKey
).getTraceId().equals(traceId
)) {
115 resources
.remove(newKey
);
122 * Search by keys (resourceId, traceId and type)
125 * A match is returned if the three arguments received match an entry
126 * Otherwise null is returned
129 * The ressourceId we are looking for
130 * @param searchedType
131 * The ressourceType we are looking for
132 * @param searchedTraceId
133 * The traceId (trace name?) we are looking for
135 * @return TimeRangeEventResource
137 public TimeRangeEventResource
findItem(Long searchedId
, ResourceTypes searchedType
, String searchedTraceId
) {
138 // Get the EventResource associated to a key we create here
139 TimeRangeEventResource foundResource
= resources
.get( new ResourceKey(searchedId
, searchedTraceId
, searchedType
) );
141 return foundResource
;
147 private TimeRangeEventResource valueRef
= null;
149 private Long resourceId
= null;
150 private String traceId
= null;
151 private ResourceTypes type
= null;
153 @SuppressWarnings("unused")
154 private ResourceKey() { }
156 public ResourceKey(TimeRangeEventResource newRef
) {
160 public ResourceKey(Long newId
, String newTraceId
, ResourceTypes newType
) {
162 traceId
= newTraceId
;
167 public boolean equals(Object obj
) {
173 boolean isSame
= false;
175 if ( obj
instanceof ResourceKey
) {
176 if ( valueRef
!= null ) {
177 if ( ( ((ResourceKey
)obj
).getResourceId().equals(valueRef
.getResourceId()) ) &&
178 ( ((ResourceKey
)obj
).getTraceId().equals(valueRef
.getTraceId()) ) &&
179 ( ((ResourceKey
)obj
).getType().equals(valueRef
.getType()) ) )
185 if ( ( ((ResourceKey
)obj
).getResourceId().equals(this.resourceId
)) &&
186 ( ((ResourceKey
)obj
).getTraceId().equals(this.traceId
)) &&
187 ( ((ResourceKey
)obj
).getType().equals(this.type
)) )
194 TraceDebug
.debug("ERROR : The given key is not of the type ProcessKey!" + obj
.getClass().toString()); //$NON-NLS-1$
200 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
201 // Do NOT change this check without checking.
202 public Long
getResourceId() {
203 if ( valueRef
!= null ) {
204 return valueRef
.getResourceId();
211 public String
getTraceId() {
212 if ( valueRef
!= null ) {
213 return valueRef
.getTraceId();
220 public ResourceTypes
getType() {
221 if ( valueRef
!= null ) {
222 return valueRef
.getType();
230 public int hashCode() {
231 return this.toString().hashCode();
236 @SuppressWarnings("nls")
237 public String
toString() {
238 if ( valueRef
!= null ) {
239 return (valueRef
.getResourceId().toString() + ":" + valueRef
.getTraceId().toString() + ":" + valueRef
.getType().toString());
241 return (resourceId
+ ":" + traceId
+ ":" + type
);