tmf: Use tabs in statistics view for each traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / internal / 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 * Michel Dagenais (michel.dagenais@polymtl.ca) - Reference C implementation, used with permission
12 *******************************************************************************/
13 package org.eclipse.linuxtools.internal.lttng.ui.views.resources.model;
14
15 import java.util.HashMap;
16 import java.util.Iterator;
17
18 import org.eclipse.linuxtools.internal.lttng.core.TraceDebug;
19 import org.eclipse.linuxtools.internal.lttng.ui.model.trange.ItemContainer;
20 import org.eclipse.linuxtools.internal.lttng.ui.model.trange.TimeRangeEventResource;
21 import org.eclipse.linuxtools.internal.lttng.ui.model.trange.TimeRangeEventResource.ResourceTypes;
22
23 /**
24 * Common location to allocate the resources in use by the resource view
25 *
26 * @author alvaro
27 *
28 */
29 public class ResourceContainer implements ItemContainer<TimeRangeEventResource> {
30 // ========================================================================
31 // Data
32 // ========================================================================
33 private final HashMap<ResourceKey, TimeRangeEventResource> resources = new HashMap<ResourceKey, TimeRangeEventResource>();
34 private static Integer uniqueId = 0;
35
36
37 // ========================================================================
38 // Constructor
39 // ========================================================================
40 /**
41 * Package level constructor
42 */
43 public ResourceContainer() { }
44
45
46 /*
47 * (non-Javadoc)
48 *
49 * @see
50 * org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#addItem
51 * (org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.
52 * ITmfTimeAnalysisEntry)
53 */
54 @Override
55 public void addItem(TimeRangeEventResource newItem) {
56 if (newItem != null) {
57 resources.put( new ResourceKey(newItem),newItem);
58 }
59 }
60
61 // ========================================================================
62 // Methods
63 // ========================================================================
64 /* (non-Javadoc)
65 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#getUniqueId()
66 */
67 @Override
68 public Integer getUniqueId() {
69 return uniqueId++;
70 }
71
72 /* (non-Javadoc)
73 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#readItems()
74 */
75 @Override
76 public TimeRangeEventResource[] readItems() {
77 return resources.values().toArray(
78 new TimeRangeEventResource[resources.size()]);
79 }
80
81 /* (non-Javadoc)
82 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearChildren()
83 */
84 @Override
85 public void clearChildren() {
86 TimeRangeEventResource newRes = null;
87 Iterator<ResourceKey> iterator = resources.keySet().iterator();
88
89 while (iterator.hasNext()) {
90 newRes = resources.get(iterator.next());
91 newRes.reset();
92 }
93 }
94
95 /* (non-Javadoc)
96 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#clearItems()
97 */
98 @Override
99 public void clearItems() {
100 resources.clear();
101 }
102
103 /* (non-Javadoc)
104 * @see org.eclipse.linuxtools.lttng.ui.views.resources.model.ItemContainer#removeItems(java.lang.String)
105 */
106 @Override
107 public void removeItems(String traceId) {
108 ResourceKey newKey = null;
109
110 Iterator<ResourceKey> iterator = resources.keySet().iterator();
111 while (iterator.hasNext()) {
112 newKey = iterator.next();
113
114 if (resources.get(newKey).getTraceId().equals(traceId)) {
115 resources.remove(newKey);
116 }
117 }
118 }
119
120
121 /**
122 * Search by keys (resourceId, traceId and type)
123 * <p>
124 *
125 * A match is returned if the three arguments received match an entry
126 * Otherwise null is returned
127 *
128 * @param searchedId
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
134 *
135 * @return TimeRangeEventResource
136 */
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) );
140
141 return foundResource;
142 }
143 }
144
145 class ResourceKey {
146
147 private TimeRangeEventResource valueRef = null;
148
149 private Long resourceId = null;
150 private String traceId = null;
151 private ResourceTypes type = null;
152
153 @SuppressWarnings("unused")
154 private ResourceKey() { }
155
156 public ResourceKey(TimeRangeEventResource newRef) {
157 valueRef = newRef;
158 }
159
160 public ResourceKey(Long newId, String newTraceId, ResourceTypes newType) {
161 resourceId = newId;
162 traceId = newTraceId;
163 type = newType;
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (obj == this)
169 return true;
170 if (obj == null)
171 return false;
172
173 boolean isSame = false;
174
175 if ( obj instanceof ResourceKey ) {
176 if ( valueRef != null ) {
177 if ( ( ((ResourceKey)obj).getResourceId().equals(valueRef.getResourceId()) ) &&
178 ( ((ResourceKey)obj).getTraceId().equals(valueRef.getTraceId()) ) &&
179 ( ((ResourceKey)obj).getType().equals(valueRef.getType()) ) )
180 {
181 isSame = true;
182 }
183 }
184 else {
185 if ( ( ((ResourceKey)obj).getResourceId().equals(this.resourceId)) &&
186 ( ((ResourceKey)obj).getTraceId().equals(this.traceId)) &&
187 ( ((ResourceKey)obj).getType().equals(this.type)) )
188 {
189 isSame = true;
190 }
191 }
192 }
193 else {
194 TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString()); //$NON-NLS-1$
195 }
196
197 return isSame;
198 }
199
200 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
201 // Do NOT change this check without checking.
202 public Long getResourceId() {
203 if ( valueRef != null ) {
204 return valueRef.getResourceId();
205 }
206 else {
207 return resourceId;
208 }
209 }
210
211 public String getTraceId() {
212 if ( valueRef != null ) {
213 return valueRef.getTraceId();
214 }
215 else {
216 return traceId;
217 }
218 }
219
220 public ResourceTypes getType() {
221 if ( valueRef != null ) {
222 return valueRef.getType();
223 }
224 else {
225 return type;
226 }
227 }
228
229 @Override
230 public int hashCode() {
231 return this.toString().hashCode();
232 }
233
234
235 @Override
236 @SuppressWarnings("nls")
237 public String toString() {
238 if ( valueRef != null ) {
239 return (valueRef.getResourceId().toString() + ":" + valueRef.getTraceId().toString() + ":" + valueRef.getType().toString());
240 }
241 return (resourceId + ":" + traceId + ":" + type);
242 }
243 }
This page took 0.036001 seconds and 5 git commands to generate.