Internalize lttng.core APIs
[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.internal.lttng.core.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 if (obj == this)
176 return true;
177 if (obj == null)
178 return false;
179
180 boolean isSame = false;
181
182 if ( obj instanceof ProcessKey ) {
183 ProcessKey procKey = (ProcessKey) obj;
184
185 if (valueRef != null) {
186 if ((procKey.getPid().equals(valueRef.getPid()))
187 && (procKey.getTraceId().equals(valueRef.getTraceID()))
188 && (procKey.getCreationtime().equals(valueRef.getCreationTime()))) {
189 // use the cpu value to validate pid 0
190 if (valueRef.getPid().longValue() == 0L && !procKey.getCpuId().equals(valueRef.getCpu())) {
191 isSame = false;
192 } else {
193 isSame = true;
194 }
195 }
196 } else {
197 if ((procKey.getPid().equals(this.pid)) && (procKey.getTraceId().equals(this.traceId))
198 && (procKey.getCreationtime().equals(this.creationtime))) {
199 // use the cpu value to validate pid 0
200 if (this.pid.longValue() == 0L && !procKey.getCpuId().equals(this.cpuId)) {
201 isSame = false;
202 } else {
203 isSame = true;
204 }
205 }
206 }
207 }
208 else {
209 TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString()); //$NON-NLS-1$
210 }
211
212 return isSame;
213 }
214
215 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
216 // Do NOT change this check without checking.
217 public Long getPid() {
218 if ( valueRef != null ) {
219 return valueRef.getPid();
220 }
221 else {
222 return pid;
223 }
224 }
225
226 public Long getCpuId() {
227 if ( valueRef != null ) {
228 return valueRef.getCpu();
229 }
230 else {
231 return cpuId;
232 }
233 }
234
235 public String getTraceId() {
236 if ( valueRef != null ) {
237 return valueRef.getTraceID();
238 }
239 else {
240 return traceId;
241 }
242 }
243
244 public Long getCreationtime() {
245 if ( valueRef != null ) {
246 return valueRef.getCreationTime();
247 }
248 else {
249 return creationtime;
250 }
251 }
252
253 @Override
254 public int hashCode() {
255 return this.toString().hashCode();
256 }
257
258
259 @Override
260 @SuppressWarnings("nls")
261 public String toString() {
262 if ( valueRef != null ) {
263 // return (valueRef.getPid().toString() + ":" +
264 // valueRef.getCpu().toString() + ":"
265 // + valueRef.getTraceID().toString() + ":" +
266 // valueRef.getCreationTime().toString());
267 return (valueRef.getPid().toString() + ":" + valueRef.getTraceID().toString() + ":" + valueRef
268 .getCreationTime().toString());
269 }
270
271 // return (pid.toString() + ":" + cpuId.toString() + ":" +
272 // traceId.toString() + ":" + creationtime.toString());
273
274 return (pid.toString() + ":" + traceId.toString() + ":" + creationtime.toString());
275 }
276 }
This page took 0.037662 seconds and 5 git commands to generate.