b2406292db54133899c78b014a274bc0e7953175
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / timechart / TimeChartEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.views.timechart;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17
18 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
19 import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSettingsManager;
20 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
21 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
22 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
23
24 /**
25 * Event in the time chart view
26 *
27 * @version 1.0
28 * @author Patrick Tasse
29 */
30 public class TimeChartEvent implements ITimeEvent {
31
32 private static final byte TIMESTAMP_SCALE = -9;
33
34 private final TimeChartAnalysisEntry fParentEntry;
35 private long fTime;
36 private long fDuration;
37 private long fFirstRank;
38 private long fLastRank;
39 private final RankRangeList fRankRangeList;
40 private long fNbEvents;
41 private int fColorSettingPriority;
42 private boolean fIsBookmark;
43 private boolean fIsVisible;
44 private boolean fIsSearchMatch;
45 private TimeChartAnalysisEntry fItemizedEntry;
46 private boolean fItemizing;
47
48 /**
49 * Standard constructor
50 *
51 * @param parentEntry
52 * The parent entry
53 * @param event
54 * The event from which this time chart event originates
55 * @param rank
56 * The rank of the event in the trace
57 * @param decorationProvider
58 * The decoration provider to use
59 */
60 public TimeChartEvent(TimeChartAnalysisEntry parentEntry, ITmfEvent event,
61 long rank, TimeChartDecorationProvider decorationProvider) {
62 fParentEntry = parentEntry;
63 fTime = event.getTimestamp().normalize(0, TIMESTAMP_SCALE).getValue();
64 fDuration = 0;
65 fFirstRank = fLastRank = rank;
66 fRankRangeList = new RankRangeList(rank);
67 fNbEvents = 1;
68 fColorSettingPriority = ColorSettingsManager.getColorSettingPriority(event);
69 fIsBookmark = decorationProvider.isBookmark(rank);
70 fIsVisible = decorationProvider.isVisible(event);
71 fIsSearchMatch = decorationProvider.isSearchMatch(event);
72 }
73
74 @Override
75 public ITimeGraphEntry getEntry() {
76 return fParentEntry;
77 }
78
79 @Override
80 public long getTime() {
81 return fTime;
82 }
83
84 @Override
85 public long getDuration() {
86 return fDuration;
87 }
88
89 @Override
90 public ITimeEvent splitBefore(long splitTime) {
91 return (splitTime > fTime ?
92 new TimeEvent(getEntry(), fTime, Math.min(fDuration, splitTime - fTime)) :
93 null);
94 }
95
96 @Override
97 public ITimeEvent splitAfter(long splitTime) {
98 return (splitTime < fTime + fDuration ?
99 new TimeEvent(getEntry(), Math.max(fTime, splitTime), fDuration - Math.max(0, splitTime - fTime)) :
100 null);
101 }
102
103 /**
104 * Retrieve the rank of the trace event which started this time event.
105 *
106 * @return The rank of the beginning
107 */
108 public long getFirstRank() {
109 return fFirstRank;
110 }
111
112 /**
113 * Retrieve the rank of the trace event which *finished* this time event.
114 *
115 * @return The rank of the end
116 */
117 public long getLastRank() {
118 return fLastRank;
119 }
120
121 /**
122 * Get the list of rank ranges corresponding to this time event.
123 *
124 * @return The rank range list
125 */
126 public RankRangeList getRankRangeList() {
127 return fRankRangeList;
128 }
129
130 /**
131 * Merge another time event with this one.
132 *
133 * @param event
134 * The other event
135 */
136 public void merge(TimeChartEvent event) {
137 mergeDecorations(event);
138 if (fTime == event.getTime() && fDuration == event.getDuration()) {
139 return;
140 }
141 long endTime = Math.max(fTime + fDuration, event.getTime() + event.getDuration());
142 fTime = Math.min(fTime, event.getTime());
143 fDuration = endTime - fTime;
144 fFirstRank = Math.min(fFirstRank, event.fFirstRank);
145 fLastRank = Math.max(fLastRank, event.fLastRank);
146 fNbEvents += event.fNbEvents;
147 fItemizedEntry = null;
148 synchronized (fRankRangeList) {
149 fRankRangeList.merge(event.getRankRangeList());
150 }
151 }
152
153 /**
154 * Merge the decorations of another time event with the decorations of this
155 * one.
156 *
157 * @param event
158 * The other event
159 */
160 public void mergeDecorations(TimeChartEvent event) {
161 fColorSettingPriority = Math.min(fColorSettingPriority, event.getColorSettingPriority());
162 fIsBookmark |= event.fIsBookmark;
163 fIsVisible |= event.fIsVisible;
164 fIsSearchMatch |= event.fIsSearchMatch;
165 }
166
167 /**
168 * Get the number of time events that have been merged with this one (starts
169 * counting at 1 if no merge happened).
170 *
171 * @return The current number of events in the bath
172 */
173 public long getNbEvents() {
174 return fNbEvents;
175 }
176
177 /**
178 * Retrieve the color setting priority.
179 *
180 * @return The priority
181 */
182 public int getColorSettingPriority() {
183 return fColorSettingPriority;
184 }
185
186 /**
187 * Set the color setting priority.
188 *
189 * @param priority
190 * The priority to set
191 */
192 public void setColorSettingPriority(int priority) {
193 fColorSettingPriority = priority;
194 }
195
196 /**
197 * Check if this time event is bookmarked
198 *
199 * @return Y/N
200 */
201 public boolean isBookmarked() {
202 return fIsBookmark;
203 }
204
205 /**
206 * Set this time event to be bookmarked or not.
207 *
208 * @param isBookmarked
209 * Should time time event become a bookmark, or not
210 */
211 public void setIsBookmarked(boolean isBookmarked) {
212 fIsBookmark = isBookmarked;
213 }
214
215 /**
216 * Check if this time is currently visible or not.
217 *
218 * @return If the event is visible
219 */
220 public boolean isVisible() {
221 return fIsVisible;
222 }
223
224 /**
225 * Set this time event to visible (or to non-visible).
226 *
227 * @param isVisible The new status
228 */
229 public void setIsVisible(boolean isVisible) {
230 fIsVisible = isVisible;
231 }
232
233 /**
234 * Check if the time event matches the current search.
235 *
236 * @return If it matches, Y/N
237 */
238 public boolean isSearchMatch() {
239 return fIsSearchMatch;
240 }
241
242 /**
243 * Mark this event as matching (or non-matching) the current search.
244 *
245 * @param isSearchMatch
246 * The new matching status
247 */
248 public void setIsSearchMatch(boolean isSearchMatch) {
249 fIsSearchMatch = isSearchMatch;
250 }
251
252 /**
253 * Set this event's itemized entry.
254 *
255 * @param timeAnalysisEntry
256 * The entry to set
257 */
258 public void setItemizedEntry(TimeChartAnalysisEntry timeAnalysisEntry) {
259 fItemizedEntry = timeAnalysisEntry;
260 }
261
262 /**
263 * Retrieve this event's itemized entry.
264 *
265 * @return The itemized entry that was previously set
266 */
267 public TimeChartAnalysisEntry getItemizedEntry() {
268 return fItemizedEntry;
269 }
270
271 /**
272 * @return Has this time event been set to itemizing?
273 */
274 public boolean isItemizing() {
275 return fItemizing;
276 }
277
278 /**
279 * Set this event's itemizing flag to true or false.
280 *
281 * @param itemizing
282 * The new value
283 */
284 public void setItemizing(boolean itemizing) {
285 fItemizing = itemizing;
286 }
287
288 /**
289 * Inner class to define a range in terms of ranks in the trace.
290 *
291 * @version 1.0
292 * @author Patrick Tasse
293 */
294 public class RankRange {
295 private long firstRank;
296 private long lastRank;
297
298 /**
299 * Standard constructor
300 *
301 * @param firstRank
302 * The first (earliest) rank of the range
303 * @param lastRank
304 * The last (latest) rank of the range
305 */
306 public RankRange(long firstRank, long lastRank) {
307 this.firstRank = firstRank;
308 this.lastRank = lastRank;
309 }
310
311 /**
312 * Retrieve the start rank of this range.
313 *
314 * @return The first rank
315 */
316 public long getFirstRank() {
317 return firstRank;
318 }
319
320 /**
321 * Retrieve the end rank of this range
322 *
323 * @return The end rank
324 */
325 public long getLastRank() {
326 return lastRank;
327 }
328
329 /**
330 * Calculate the minimal distance between two RankRange's
331 *
332 * @param range
333 * The other range
334 * @return The distance, in "number of events" between the two ranges
335 */
336 public long distanceFrom(RankRange range) {
337 if (range.lastRank < fFirstRank) {
338 return fFirstRank - range.lastRank;
339 } else if (range.firstRank > fLastRank) {
340 return range.firstRank - fLastRank;
341 } else {
342 return 0;
343 }
344 }
345
346 @Override
347 public String toString() {
348 return "["+firstRank+","+lastRank+"]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
349 }
350 }
351
352 private class RankRangeList extends ArrayList<RankRange> {
353
354 private static final long serialVersionUID = 6060485531208535986L;
355
356 public RankRangeList(long rank) {
357 super(1);
358 add(new RankRange(rank, rank));
359 }
360
361 public void merge(RankRangeList rankRangeList) {
362 long threshold = fParentEntry.getTrace().getCacheSize();
363 for (RankRange newRange : rankRangeList) {
364 boolean merged = false;
365 for (RankRange oldRange : fRankRangeList) {
366 if (newRange.distanceFrom(oldRange) <= threshold) {
367 oldRange.firstRank = Math.min(oldRange.firstRank, newRange.firstRank);
368 oldRange.lastRank = Math.max(oldRange.lastRank, newRange.lastRank);
369 merged = true;
370 break;
371 }
372 }
373 if (!merged) {
374 add(newRange);
375 }
376 }
377 Iterator<RankRange> iterator = fRankRangeList.iterator();
378 RankRange previous = null;
379 while (iterator.hasNext()) {
380 RankRange range = iterator.next();
381 if (previous != null && range.distanceFrom(previous) <= threshold) {
382 previous.firstRank = Math.min(previous.firstRank, range.firstRank);
383 previous.lastRank = Math.max(previous.lastRank, range.lastRank);
384 iterator.remove();
385 }
386 previous = range;
387 }
388 }
389 }
390 }
This page took 0.041086 seconds and 4 git commands to generate.