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
.lttng
.ui
.views
.resources
.model
;
15 import java
.util
.HashMap
;
16 import java
.util
.Iterator
;
18 import org
.eclipse
.linuxtools
.lttng
.core
.TraceDebug
;
19 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.ItemContainer
;
20 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.TimeRangeEventResource
;
21 import org
.eclipse
.linuxtools
.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
) {
168 boolean isSame
= false;
170 if ( obj
instanceof ResourceKey
) {
171 if ( valueRef
!= null ) {
172 if ( ( ((ResourceKey
)obj
).getResourceId().equals(valueRef
.getResourceId()) ) &&
173 ( ((ResourceKey
)obj
).getTraceId().equals(valueRef
.getTraceId()) ) &&
174 ( ((ResourceKey
)obj
).getType().equals(valueRef
.getType()) ) )
180 if ( ( ((ResourceKey
)obj
).getResourceId().equals(this.resourceId
)) &&
181 ( ((ResourceKey
)obj
).getTraceId().equals(this.traceId
)) &&
182 ( ((ResourceKey
)obj
).getType().equals(this.type
)) )
189 TraceDebug
.debug("ERROR : The given key is not of the type ProcessKey!" + obj
.getClass().toString()); //$NON-NLS-1$
195 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
196 // Do NOT change this check without checking.
197 public Long
getResourceId() {
198 if ( valueRef
!= null ) {
199 return valueRef
.getResourceId();
206 public String
getTraceId() {
207 if ( valueRef
!= null ) {
208 return valueRef
.getTraceId();
215 public ResourceTypes
getType() {
216 if ( valueRef
!= null ) {
217 return valueRef
.getType();
225 public int hashCode() {
226 return this.toString().hashCode();
231 @SuppressWarnings("nls")
232 public String
toString() {
233 if ( valueRef
!= null ) {
234 return (valueRef
.getResourceId().toString() + ":" + valueRef
.getTraceId().toString() + ":" + valueRef
.getType().toString());
236 return (resourceId
+ ":" + traceId
+ ":" + type
);