[292393] Revisited header search function
[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.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 */
27 public class ResourceContainer {
28 // ========================================================================
29 // Data
30 // ========================================================================
31 private final HashMap<ResourceKey, TimeRangeEventResource> resources = new HashMap<ResourceKey, TimeRangeEventResource>();
32 private static Integer uniqueId = 0;
33
34
35 // ========================================================================
36 // Constructor
37 // ========================================================================
38 /**
39 * Package level constructor
40 */
41 public ResourceContainer() { }
42
43 /**
44 * Interface to add resources.
45 *
46 * @param process
47 */
48 public void addResource(TimeRangeEventResource newResource) {
49 if (newResource != null) {
50 resources.put( new ResourceKey(newResource),newResource);
51 }
52 }
53
54 // ========================================================================
55 // Methods
56 // ========================================================================
57 /**
58 * Request a unique ID
59 *
60 * @return Integer
61 */
62 public Integer getUniqueId() {
63 return uniqueId++;
64 }
65
66 /**
67 * This method is intended for read only purposes in order to keep the
68 * internal data structure in Synch
69 *
70 * @return
71 */
72 public TimeRangeEventResource[] readResources() {
73 return resources.values().toArray(
74 new TimeRangeEventResource[resources.size()]);
75 }
76
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
82 */
83 public void clearChildren(String traceId) {
84 TimeRangeEventResource newRes = null;
85 Iterator<ResourceKey> iterator = resources.keySet().iterator();
86
87 while (iterator.hasNext()) {
88 newRes = resources.get(iterator.next());
89
90 if (newRes.getTraceId().equals(traceId)) {
91 newRes.reset();
92 }
93 }
94 }
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
103 /**
104 * Remove the resources related to a specific trace e.g. during trace
105 * removal
106 *
107 * @param traceId
108 */
109 public void removeResources(String traceId) {
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 /**
124 * Search by keys (resourceId, traceId and type)<p>
125 *
126 * A match is returned if the three arguments received match an entry
127 * Otherwise null is returned
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
132 *
133 * @return TimeRangeEventResource
134 */
135 public TimeRangeEventResource findResource(Long searchedId, ResourceTypes searchedType, String searchedTraceId) {
136 // Get the EventResource associated to a key we create here
137 TimeRangeEventResource foundResource = resources.get( new ResourceKey(searchedId, searchedTraceId, searchedType) );
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 ) {
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)) &&
180 ( ((ResourceKey)obj).getType().equals(this.traceId)) )
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());
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() {
230 if ( valueRef != null ) {
231 return (valueRef.getResourceId().toString() + ":" + valueRef.getTraceId().toString() + ":" + valueRef.getType().toString());
232 }
233 return (resourceId + ":" + traceId + ":" + type);
234 }
235 }
This page took 0.03651 seconds and 5 git commands to generate.