(no commit message)
[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 public void addItem(TimeRangeEventProcess newItem) {
56 if (newItem != null) {
57 allProcesses.put(new ProcessKey(newItem), newItem);
58 }
59 }
60
61 /**
62 * Request a unique ID
63 *
64 * @return Integer
65 */
66 public Integer getUniqueId() {
67 return uniqueId++;
68 }
69
70 /**
71 * This method is intended for read only purposes in order to keep the
72 * internal data structure in synch
73 *
74 * @return TimeRangeEventProcess[]
75 */
76 public TimeRangeEventProcess[] readItems() {
77
78 // This allow us to return an Array of the correct type of the exact correct dimension, without looping
79 return allProcesses.values().toArray(new TimeRangeEventProcess[allProcesses.size()]);
80 }
81
82 /**
83 * Clear the children information for processes e.g. just before refreshing
84 * data with a new time range
85 */
86 public void clearChildren() {
87 TimeRangeEventProcess process = null;
88 Iterator<ProcessKey> iterator = allProcesses.keySet().iterator();
89
90 while (iterator.hasNext()) {
91 process = allProcesses.get(iterator.next());
92 process.reset();
93 }
94 }
95
96 /**
97 * Clear all process items
98 */
99 public void clearItems() {
100 allProcesses.clear();
101 }
102
103 /**
104 * Remove the process related to a specific trace e.g. during trace
105 * removal
106 *
107 * @param traceId The trace unique id (trace name?) on which we want to remove process
108 */
109 public void removeItems(String traceId) {
110 ProcessKey iterKey = null;
111
112 Iterator<ProcessKey> iterator = allProcesses.keySet().iterator();
113 while (iterator.hasNext()) {
114 iterKey = iterator.next();
115
116 if (allProcesses.get(iterKey).getTraceID().equals(traceId)) {
117 allProcesses.remove(iterKey);
118 }
119 }
120 }
121
122 /**
123 * Search by keys (pid, cpuId, traceId and creationTime)<p>
124 *
125 * A match is returned if the four arguments received match an entry
126 * Otherwise null is returned
127 *
128 * @param searchedPid The processId (Pid) we are looking for
129 * @param searchedCpuId The cpu Id we are looking for
130 * @param searchedTraceID The traceId (trace name?) we are looking for
131 * @param searchedCreationtime The creation time we are looking for
132 *
133 * @return TimeRangeEventProcess
134 */
135 public TimeRangeEventProcess findProcess(Long searchedPid, Long searchedCpuId, String searchedTraceID, Long searchedCreationtime) {
136 // Get the TimeRangeEventProcess associated to a key we create here
137 TimeRangeEventProcess foundProcess = allProcesses.get( new ProcessKey(searchedPid, searchedCpuId, searchedTraceID, searchedCreationtime) );
138
139 return foundProcess;
140 }
141 }
142
143
144 class ProcessKey {
145 private TimeRangeEventProcess valueRef = null;
146
147 private Long pid = null;
148 private Long cpuId = null;
149 private String traceId = null;
150 private Long creationtime = null;
151
152 @SuppressWarnings("unused")
153 private ProcessKey() { }
154
155 public ProcessKey(TimeRangeEventProcess newRef) {
156 valueRef = newRef;
157 }
158
159 public ProcessKey(Long newPid, Long newCpuId, String newTraceId, Long newCreationTime) {
160 pid = newPid;
161 cpuId = newCpuId;
162 traceId = newTraceId;
163 creationtime = newCreationTime;
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 boolean isSame = false;
169
170 if ( obj instanceof ProcessKey ) {
171 ProcessKey procKey = (ProcessKey) obj;
172
173 if ( valueRef != null ) {
174 if ( (procKey.getPid().equals(valueRef.getPid()) ) &&
175 (procKey.getTraceId().equals(valueRef.getTraceID()) ) &&
176 (procKey.getCpuId().equals(valueRef.getCpu()) ) &&
177 (procKey.getCreationtime().equals(valueRef.getCreationTime()) ) )
178 {
179 isSame = true;
180 }
181 }
182 else {
183 if ( (procKey.getPid().equals(this.pid ) ) &&
184 (procKey.getTraceId().equals(this.traceId ) ) &&
185 (procKey.getCpuId().equals(this.cpuId ) ) &&
186 (procKey.getCreationtime().equals(this.creationtime ) ) )
187 {
188 isSame = true;
189 }
190 }
191 }
192 else {
193 TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString());
194 }
195
196 return isSame;
197 }
198
199 // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter
200 // Do NOT change this check without checking.
201 public Long getPid() {
202 if ( valueRef != null ) {
203 return valueRef.getPid();
204 }
205 else {
206 return pid;
207 }
208 }
209
210 public Long getCpuId() {
211 if ( valueRef != null ) {
212 return valueRef.getCpu();
213 }
214 else {
215 return cpuId;
216 }
217 }
218
219 public String getTraceId() {
220 if ( valueRef != null ) {
221 return valueRef.getTraceID();
222 }
223 else {
224 return traceId;
225 }
226 }
227
228 public Long getCreationtime() {
229 if ( valueRef != null ) {
230 return valueRef.getCreationTime();
231 }
232 else {
233 return creationtime;
234 }
235 }
236
237 @Override
238 public int hashCode() {
239 return this.toString().hashCode();
240 }
241
242
243 @Override
244 public String toString() {
245 if ( valueRef != null ) {
246 return (valueRef.getPid().toString() + ":" + valueRef.getCpu().toString() + ":" + valueRef.getTraceID().toString() + ":" + valueRef.getCreationTime().toString());
247 }
248
249 return (pid.toString() + ":" + cpuId.toString() + ":" + traceId.toString() + ":" + creationtime.toString());
250 }
251 }
This page took 0.049554 seconds and 6 git commands to generate.