2010-10-26 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug309042
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / controlflow / model / FlowProcessContainer.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.controlflow.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.TimeRangeEventProcess;
20
21 /**
22 * Contains the processes in use by the Control flow view
23 *
24 * @author alvaro
25 *
26 */
27 public class FlowProcessContainer implements ItemContainer<TimeRangeEventProcess> {
28 // ========================================================================
29 // Data
30 // ========================================================================
31 private final HashMap<ProcessKey, TimeRangeEventProcess> allProcesses = new HashMap<ProcessKey, TimeRangeEventProcess>();
32 private static Integer uniqueId = 0;
33
34 // ========================================================================
35 // Constructor
36 // ========================================================================
37
38 /**
39 * Package level constructor
40 */
41 FlowProcessContainer() {
42
43 }
44
45 // ========================================================================
46 // Methods
47 // ========================================================================
48 /**
49 * Interface to add a new process.<p>
50 *
51 * Note : Process with the same key will be overwritten, it's calling function job to make sure the new process is unique.
52 *
53 * @param newProcess The process to add
54 */
55 @Override
56 public void addItem(TimeRangeEventProcess newItem) {
57 if (newItem != null) {
58 allProcesses.put(new ProcessKey(newItem), newItem);
59 }
60 }
61
62 /**
63 * Request a unique ID
64 *
65 * @return Integer
66 */
67 @Override
68 public Integer getUniqueId() {
69 return uniqueId++;
70 }
71
72 /**
73 * This method is intended for read only purposes in order to keep the
74 * internal data structure in synch
75 *
76 * @return TimeRangeEventProcess[]
77 */
78 @Override
79 public TimeRangeEventProcess[] readItems() {
80
81 // This allow us to return an Array of the correct type of the exact correct dimension, without looping
82 return allProcesses.values().toArray(new TimeRangeEventProcess[allProcesses.size()]);
83 }
84
85 /**
86 * Clear the children information for processes e.g. just before refreshing
87 * data with a new time range
88 */
89 @Override
90 public void clearChildren() {
91 TimeRangeEventProcess process = null;
92 Iterator<ProcessKey> iterator = allProcesses.keySet().iterator();
93
94 while (iterator.hasNext()) {
95 process = allProcesses.get(iterator.next());
96 process.reset();
97 }
98 }
99
100 /**
101 * Clear all process items
102 */
103 @Override
104 public void clearItems() {
105 allProcesses.clear();
106 }
107
108 /**
109 * Remove the process related to a specific trace e.g. during trace
110 * removal
111 *
112 * @param traceId The trace unique id (trace name?) on which we want to remove process
113 */
114 @Override
115 public void removeItems(String traceId) {
116 ProcessKey iterKey = null;
117
118 Iterator<ProcessKey> iterator = allProcesses.keySet().iterator();
119 while (iterator.hasNext()) {
120 iterKey = iterator.next();
121
122 if (allProcesses.get(iterKey).getTraceID().equals(traceId)) {
123 allProcesses.remove(iterKey);
124 }
125 }
126 }
127
128 /**
129 * Search by keys (pid, cpuId, traceId and creationTime)<p>
130 *
131 * A match is returned if the four arguments received match an entry
132 * Otherwise null is returned
133 *
134 * @param searchedPid The processId (Pid) we are looking for
135 * @param searchedCpuId The cpu Id we are looking for
136 * @param searchedTraceID The traceId (trace name?) we are looking for
137 * @param searchedCreationtime The creation time we are looking for
138 *
139 * @return TimeRangeEventProcess
140 */
141 public TimeRangeEventProcess findProcess(Long searchedPid, Long searchedCpuId, String searchedTraceID, Long searchedCreationtime) {
142 // Get the TimeRangeEventProcess associated to a key we create here
143 TimeRangeEventProcess foundProcess = allProcesses.get( new ProcessKey(searchedPid, searchedCpuId, searchedTraceID, searchedCreationtime) );
144
145 return foundProcess;
146 }
147 }
148
149
150 class ProcessKey {
151 private TimeRangeEventProcess valueRef = null;
152
153 private Long pid = null;
154 private Long cpuId = null;
155 private String traceId = null;
156 private Long creationtime = null;
157
158 @SuppressWarnings("unused")
159 private ProcessKey() { }
160
161 public ProcessKey(TimeRangeEventProcess newRef) {
162 valueRef = newRef;
163 }
164
165 public ProcessKey(Long newPid, Long newCpuId, String newTraceId, Long newCreationTime) {
166 pid = newPid;
167 cpuId = newCpuId;
168 traceId = newTraceId;
169 creationtime = newCreationTime;
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 boolean isSame = false;
175
176 if ( obj instanceof ProcessKey ) {
177 ProcessKey procKey = (ProcessKey) obj;
178
179 if (valueRef != null) {
180 if ((procKey.getPid().equals(valueRef.getPid()))
181 && (procKey.getTraceId().equals(valueRef.getTraceID()))
182 && (procKey.getCreationtime().equals(valueRef.getCreationTime()))) {
183 // use the cpu value to validate pid 0
184 if (valueRef.getPid().longValue() == 0L && !procKey.getCpuId().equals(valueRef.getCpu())) {
185 isSame = false;
186 } else {
187 isSame = true;
188 }
189 }
190 } else {
191 if ((procKey.getPid().equals(this.pid)) && (procKey.getTraceId().equals(this.traceId))
192 && (procKey.getCreationtime().equals(this.creationtime))) {
193 // use the cpu value to validate pid 0
194 if (this.pid.longValue() == 0L && !procKey.getCpuId().equals(this.cpuId)) {
195 isSame = false;
196 } else {
197 isSame = true;
198 }
199 }
200 }
201 }
202 else {
203 TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString());
204 }
205
206 return isSame;
207 }
208
209 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
210 // Do NOT change this check without checking.
211 public Long getPid() {
212 if ( valueRef != null ) {
213 return valueRef.getPid();
214 }
215 else {
216 return pid;
217 }
218 }
219
220 public Long getCpuId() {
221 if ( valueRef != null ) {
222 return valueRef.getCpu();
223 }
224 else {
225 return cpuId;
226 }
227 }
228
229 public String getTraceId() {
230 if ( valueRef != null ) {
231 return valueRef.getTraceID();
232 }
233 else {
234 return traceId;
235 }
236 }
237
238 public Long getCreationtime() {
239 if ( valueRef != null ) {
240 return valueRef.getCreationTime();
241 }
242 else {
243 return creationtime;
244 }
245 }
246
247 @Override
248 public int hashCode() {
249 return this.toString().hashCode();
250 }
251
252
253 @Override
254 public String toString() {
255 if ( valueRef != null ) {
256 // return (valueRef.getPid().toString() + ":" +
257 // valueRef.getCpu().toString() + ":"
258 // + valueRef.getTraceID().toString() + ":" +
259 // valueRef.getCreationTime().toString());
260 return (valueRef.getPid().toString() + ":" + valueRef.getTraceID().toString() + ":" + valueRef
261 .getCreationTime().toString());
262 }
263
264 // return (pid.toString() + ":" + cpuId.toString() + ":" +
265 // traceId.toString() + ":" + creationtime.toString());
266
267 return (pid.toString() + ":" + traceId.toString() + ":" + creationtime.toString());
268 }
269 }
This page took 0.035294 seconds and 5 git commands to generate.