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