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