(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / resources / model / ResourceContainer.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Alvaro Sanchez-Leon - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.resources.model;
13
14 import java.util.HashMap;
15 import java.util.Iterator;
16
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;
21
22 /**
23 * Common location to allocate the resources in use by the resource view
24 *
25 * @author alvaro
26 *
27 */
28 public class ResourceContainer implements ItemContainer<TimeRangeEventResource> {
29 // ========================================================================
30 // Data
31 // ========================================================================
32 private final HashMap<ResourceKey, TimeRangeEventResource> resources = new HashMap<ResourceKey, TimeRangeEventResource>();
33 private static Integer uniqueId = 0;
34
35
36 // ========================================================================
37 // Constructor
38 // ========================================================================
39 /**
40 * Package level constructor
41 */
42 public ResourceContainer() { }
43
44
45 /*
46 * (non-Javadoc)
47 *
48 * @see
49 * org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#addItem
50 * (org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.
51 * ITmfTimeAnalysisEntry)
52 */
53 public void addItem(TimeRangeEventResource newItem) {
54 if (newItem != null) {
55 resources.put( new ResourceKey(newItem),newItem);
56 }
57 }
58
59 // ========================================================================
60 // Methods
61 // ========================================================================
62 /* (non-Javadoc)
63 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#getUniqueId()
64 */
65 public Integer getUniqueId() {
66 return uniqueId++;
67 }
68
69 /* (non-Javadoc)
70 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#readItems()
71 */
72 public TimeRangeEventResource[] readItems() {
73 return resources.values().toArray(
74 new TimeRangeEventResource[resources.size()]);
75 }
76
77 /* (non-Javadoc)
78 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearChildren()
79 */
80 public void clearChildren() {
81 TimeRangeEventResource newRes = null;
82 Iterator<ResourceKey> iterator = resources.keySet().iterator();
83
84 while (iterator.hasNext()) {
85 newRes = resources.get(iterator.next());
86 newRes.reset();
87 }
88 }
89
90 /* (non-Javadoc)
91 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearItems()
92 */
93 public void clearItems() {
94 resources.clear();
95 }
96
97 /* (non-Javadoc)
98 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#removeItems(java.lang.String)
99 */
100 public void removeItems(String traceId) {
101 ResourceKey newKey = null;
102
103 Iterator<ResourceKey> iterator = resources.keySet().iterator();
104 while (iterator.hasNext()) {
105 newKey = iterator.next();
106
107 if (resources.get(newKey).getTraceId().equals(traceId)) {
108 resources.remove(newKey);
109 }
110 }
111 }
112
113
114 /**
115 * Search by keys (resourceId, traceId and type)
116 * <p>
117 *
118 * A match is returned if the three arguments received match an entry
119 * Otherwise null is returned
120 *
121 * @param searchedId
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
127 *
128 * @return TimeRangeEventResource
129 */
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) );
133
134 return foundResource;
135 }
136 }
137
138 class ResourceKey {
139
140 private TimeRangeEventResource valueRef = null;
141
142 private Long resourceId = null;
143 private String traceId = null;
144 private ResourceTypes type = null;
145
146 @SuppressWarnings("unused")
147 private ResourceKey() { }
148
149 public ResourceKey(TimeRangeEventResource newRef) {
150 valueRef = newRef;
151 }
152
153 public ResourceKey(Long newId, String newTraceId, ResourceTypes newType) {
154 resourceId = newId;
155 traceId = newTraceId;
156 type = newType;
157 }
158
159 @Override
160 public boolean equals(Object obj) {
161 boolean isSame = false;
162
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()) ) )
168 {
169 isSame = true;
170 }
171 }
172 else {
173 if ( ( ((ResourceKey)obj).getResourceId().equals(this.resourceId)) &&
174 ( ((ResourceKey)obj).getTraceId().equals(this.traceId)) &&
175 ( ((ResourceKey)obj).getType().equals(this.type)) )
176 {
177 isSame = true;
178 }
179 }
180 }
181 else {
182 TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString());
183 }
184
185 return isSame;
186 }
187
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();
193 }
194 else {
195 return resourceId;
196 }
197 }
198
199 public String getTraceId() {
200 if ( valueRef != null ) {
201 return valueRef.getTraceId();
202 }
203 else {
204 return traceId;
205 }
206 }
207
208 public ResourceTypes getType() {
209 if ( valueRef != null ) {
210 return valueRef.getType();
211 }
212 else {
213 return type;
214 }
215 }
216
217 @Override
218 public int hashCode() {
219 return this.toString().hashCode();
220 }
221
222
223 @Override
224 public String toString() {
225 if ( valueRef != null ) {
226 return (valueRef.getResourceId().toString() + ":" + valueRef.getTraceId().toString() + ":" + valueRef.getType().toString());
227 }
228 return (resourceId + ":" + traceId + ":" + type);
229 }
230 }
This page took 0.036833 seconds and 5 git commands to generate.