Commit | Line | Data |
---|---|---|
6e512b93 ASL |
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: | |
b513223f | 10 | * Alvaro Sanchez-Leon - Initial API and implementation |
6e512b93 ASL |
11 | *******************************************************************************/ |
12 | package org.eclipse.linuxtools.lttng.ui.views.resources.model; | |
13 | ||
14 | import java.util.HashMap; | |
15 | import java.util.Iterator; | |
16 | ||
2211df66 | 17 | import org.eclipse.linuxtools.lttng.TraceDebug; |
6e512b93 ASL |
18 | import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeEventResource; |
19 | import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeEventResource.ResourceTypes; | |
20 | ||
21 | /** | |
22 | * Common location to allocate the resources in use by the resource view | |
23 | * | |
24 | * @author alvaro | |
25 | * | |
26 | */ | |
8035003b | 27 | public class ResourceContainer { |
b513223f FC |
28 | // ======================================================================== |
29 | // Data | |
30 | // ======================================================================== | |
6e512b93 ASL |
31 | private final HashMap<ResourceKey, TimeRangeEventResource> resources = new HashMap<ResourceKey, TimeRangeEventResource>(); |
32 | private static Integer uniqueId = 0; | |
33 | ||
b513223f FC |
34 | |
35 | // ======================================================================== | |
36 | // Constructor | |
37 | // ======================================================================== | |
6e512b93 ASL |
38 | /** |
39 | * Package level constructor | |
40 | */ | |
41 | public ResourceContainer() { } | |
42 | ||
8035003b ASL |
43 | /** |
44 | * Interface to add resources. | |
6e512b93 | 45 | * |
8035003b | 46 | * @param process |
6e512b93 | 47 | */ |
8035003b ASL |
48 | public void addResource(TimeRangeEventResource newResource) { |
49 | if (newResource != null) { | |
50 | resources.put( new ResourceKey(newResource),newResource); | |
6e512b93 ASL |
51 | } |
52 | } | |
53 | ||
b513223f FC |
54 | // ======================================================================== |
55 | // Methods | |
56 | // ======================================================================== | |
8035003b ASL |
57 | /** |
58 | * Request a unique ID | |
59 | * | |
60 | * @return Integer | |
61 | */ | |
6e512b93 ASL |
62 | public Integer getUniqueId() { |
63 | return uniqueId++; | |
64 | } | |
65 | ||
8035003b ASL |
66 | /** |
67 | * This method is intended for read only purposes in order to keep the | |
68 | * internal data structure in Synch | |
69 | * | |
70 | * @return | |
6e512b93 | 71 | */ |
8035003b | 72 | public TimeRangeEventResource[] readResources() { |
6e512b93 ASL |
73 | return resources.values().toArray( |
74 | new TimeRangeEventResource[resources.size()]); | |
75 | } | |
76 | ||
8035003b ASL |
77 | /** |
78 | * Clear the children information for resources related to a specific trace | |
79 | * e.g. just before refreshing data with a new time range | |
80 | * | |
81 | * @param traceId | |
6e512b93 | 82 | */ |
8035003b | 83 | public void clearChildren(String traceId) { |
6e512b93 ASL |
84 | TimeRangeEventResource newRes = null; |
85 | Iterator<ResourceKey> iterator = resources.keySet().iterator(); | |
86 | ||
87 | while (iterator.hasNext()) { | |
8035003b ASL |
88 | newRes = resources.get(iterator.next()); |
89 | ||
90 | if (newRes.getTraceId().equals(traceId)) { | |
91 | newRes.reset(); | |
92 | } | |
6e512b93 ASL |
93 | } |
94 | } | |
d712a5f3 FC |
95 | |
96 | /** | |
97 | * Clear all resources items e.g. when a new experiment is selected | |
98 | */ | |
99 | public void clearResources() { | |
100 | resources.clear(); | |
101 | } | |
102 | ||
8035003b | 103 | /** |
b513223f | 104 | * Remove the resources related to a specific trace e.g. during trace |
8035003b ASL |
105 | * removal |
106 | * | |
107 | * @param traceId | |
6e512b93 | 108 | */ |
8035003b | 109 | public void removeResources(String traceId) { |
6e512b93 ASL |
110 | ResourceKey newKey = null; |
111 | ||
112 | Iterator<ResourceKey> iterator = resources.keySet().iterator(); | |
113 | while (iterator.hasNext()) { | |
114 | newKey = iterator.next(); | |
115 | ||
116 | if (resources.get(newKey).getTraceId().equals(traceId)) { | |
117 | resources.remove(newKey); | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | ||
123 | /** | |
b513223f FC |
124 | * Search by keys (resourceId, traceId and type)<p> |
125 | * | |
8035003b ASL |
126 | * A match is returned if the three arguments received match an entry |
127 | * Otherwise null is returned | |
b513223f FC |
128 | * |
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 | |
8035003b | 132 | * |
b513223f | 133 | * @return TimeRangeEventResource |
8035003b ASL |
134 | */ |
135 | public TimeRangeEventResource findResource(Long searchedId, ResourceTypes searchedType, String searchedTraceId) { | |
6e512b93 | 136 | // Get the EventResource associated to a key we create here |
b513223f | 137 | TimeRangeEventResource foundResource = resources.get( new ResourceKey(searchedId, searchedTraceId, searchedType) ); |
6e512b93 ASL |
138 | |
139 | return foundResource; | |
140 | } | |
141 | } | |
142 | ||
143 | class ResourceKey { | |
144 | ||
145 | private TimeRangeEventResource valueRef = null; | |
146 | ||
147 | private Long resourceId = null; | |
148 | private String traceId = null; | |
149 | private ResourceTypes type = null; | |
150 | ||
151 | @SuppressWarnings("unused") | |
152 | private ResourceKey() { } | |
153 | ||
154 | public ResourceKey(TimeRangeEventResource newRef) { | |
155 | valueRef = newRef; | |
156 | } | |
157 | ||
158 | public ResourceKey(Long newId, String newTraceId, ResourceTypes newType) { | |
159 | resourceId = newId; | |
160 | traceId = newTraceId; | |
161 | type = newType; | |
162 | } | |
163 | ||
164 | @Override | |
165 | public boolean equals(Object obj) { | |
166 | boolean isSame = false; | |
167 | ||
168 | if ( obj instanceof ResourceKey ) { | |
2211df66 FC |
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()) ) ) | |
173 | { | |
174 | isSame = true; | |
175 | } | |
176 | } | |
177 | else { | |
178 | if ( ( ((ResourceKey)obj).getResourceId().equals(this.resourceId)) && | |
179 | ( ((ResourceKey)obj).getTraceId().equals(this.traceId)) && | |
28b94d61 | 180 | ( ((ResourceKey)obj).getType().equals(this.type)) ) |
2211df66 FC |
181 | { |
182 | isSame = true; | |
183 | } | |
184 | } | |
185 | } | |
186 | else { | |
187 | TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString()); | |
6e512b93 ASL |
188 | } |
189 | ||
190 | return isSame; | |
191 | } | |
192 | ||
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(); | |
198 | } | |
199 | else { | |
200 | return resourceId; | |
201 | } | |
202 | } | |
203 | ||
204 | public String getTraceId() { | |
205 | if ( valueRef != null ) { | |
206 | return valueRef.getTraceId(); | |
207 | } | |
208 | else { | |
209 | return traceId; | |
210 | } | |
211 | } | |
212 | ||
213 | public ResourceTypes getType() { | |
214 | if ( valueRef != null ) { | |
215 | return valueRef.getType(); | |
216 | } | |
217 | else { | |
218 | return type; | |
219 | } | |
220 | } | |
221 | ||
222 | @Override | |
223 | public int hashCode() { | |
224 | return this.toString().hashCode(); | |
225 | } | |
226 | ||
227 | ||
228 | @Override | |
229 | public String toString() { | |
b513223f FC |
230 | if ( valueRef != null ) { |
231 | return (valueRef.getResourceId().toString() + ":" + valueRef.getTraceId().toString() + ":" + valueRef.getType().toString()); | |
232 | } | |
233 | return (resourceId + ":" + traceId + ":" + type); | |
6e512b93 ASL |
234 | } |
235 | } |