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
.ItemContainer
;
19 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.TimeRangeEventResource
;
20 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.TimeRangeEventResource
.ResourceTypes
;
23 * Common location to allocate the resources in use by the resource view
28 public class ResourceContainer
implements ItemContainer
<TimeRangeEventResource
> {
29 // ========================================================================
31 // ========================================================================
32 private final HashMap
<ResourceKey
, TimeRangeEventResource
> resources
= new HashMap
<ResourceKey
, TimeRangeEventResource
>();
33 private static Integer uniqueId
= 0;
36 // ========================================================================
38 // ========================================================================
40 * Package level constructor
42 public ResourceContainer() { }
49 * org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#addItem
50 * (org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.
51 * ITmfTimeAnalysisEntry)
53 public void addItem(TimeRangeEventResource newItem
) {
54 if (newItem
!= null) {
55 resources
.put( new ResourceKey(newItem
),newItem
);
59 // ========================================================================
61 // ========================================================================
63 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#getUniqueId()
65 public Integer
getUniqueId() {
70 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#readItems()
72 public TimeRangeEventResource
[] readItems() {
73 return resources
.values().toArray(
74 new TimeRangeEventResource
[resources
.size()]);
78 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearChildren()
80 public void clearChildren() {
81 TimeRangeEventResource newRes
= null;
82 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
84 while (iterator
.hasNext()) {
85 newRes
= resources
.get(iterator
.next());
91 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearItems()
93 public void clearItems() {
98 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#removeItems(java.lang.String)
100 public void removeItems(String traceId
) {
101 ResourceKey newKey
= null;
103 Iterator
<ResourceKey
> iterator
= resources
.keySet().iterator();
104 while (iterator
.hasNext()) {
105 newKey
= iterator
.next();
107 if (resources
.get(newKey
).getTraceId().equals(traceId
)) {
108 resources
.remove(newKey
);
115 * Search by keys (resourceId, traceId and type)
118 * A match is returned if the three arguments received match an entry
119 * Otherwise null is returned
122 * The ressourceId we are looking for
123 * @param searchedType
124 * The ressourceType we are looking for
125 * @param searchedTraceId
126 * The traceId (trace name?) we are looking for
128 * @return TimeRangeEventResource
130 public TimeRangeEventResource
findItem(Long searchedId
, ResourceTypes searchedType
, String searchedTraceId
) {
131 // Get the EventResource associated to a key we create here
132 TimeRangeEventResource foundResource
= resources
.get( new ResourceKey(searchedId
, searchedTraceId
, searchedType
) );
134 return foundResource
;
140 private TimeRangeEventResource valueRef
= null;
142 private Long resourceId
= null;
143 private String traceId
= null;
144 private ResourceTypes type
= null;
146 @SuppressWarnings("unused")
147 private ResourceKey() { }
149 public ResourceKey(TimeRangeEventResource newRef
) {
153 public ResourceKey(Long newId
, String newTraceId
, ResourceTypes newType
) {
155 traceId
= newTraceId
;
160 public boolean equals(Object obj
) {
161 boolean isSame
= false;
163 if ( obj
instanceof ResourceKey
) {
164 if ( valueRef
!= null ) {
165 if ( ( ((ResourceKey
)obj
).getResourceId().equals(valueRef
.getResourceId()) ) &&
166 ( ((ResourceKey
)obj
).getTraceId().equals(valueRef
.getTraceId()) ) &&
167 ( ((ResourceKey
)obj
).getType().equals(valueRef
.getType()) ) )
173 if ( ( ((ResourceKey
)obj
).getResourceId().equals(this.resourceId
)) &&
174 ( ((ResourceKey
)obj
).getTraceId().equals(this.traceId
)) &&
175 ( ((ResourceKey
)obj
).getType().equals(this.type
)) )
182 TraceDebug
.debug("ERROR : The given key is not of the type ProcessKey!" + obj
.getClass().toString());
188 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
189 // Do NOT change this check without checking.
190 public Long
getResourceId() {
191 if ( valueRef
!= null ) {
192 return valueRef
.getResourceId();
199 public String
getTraceId() {
200 if ( valueRef
!= null ) {
201 return valueRef
.getTraceId();
208 public ResourceTypes
getType() {
209 if ( valueRef
!= null ) {
210 return valueRef
.getType();
218 public int hashCode() {
219 return this.toString().hashCode();
224 public String
toString() {
225 if ( valueRef
!= null ) {
226 return (valueRef
.getResourceId().toString() + ":" + valueRef
.getTraceId().toString() + ":" + valueRef
.getType().toString());
228 return (resourceId
+ ":" + traceId
+ ":" + type
);