Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / model / EventIterator.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 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.widgets.timegraph.model;
14
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.NoSuchElementException;
18
19
20 /**
21 * An iterator for time events. Events from the zoomed event list override any
22 * events from the underlying event list.
23 * @since 2.0
24 */
25 public class EventIterator implements Iterator<ITimeEvent> {
26
27 private final long fStartTime;
28 private final long fEndTime;
29 private List<ITimeEvent> fEventList;
30 private List<ITimeEvent> fZoomedEventList;
31 private long fZoomedStartTime;
32 private long fZoomedEndTime;
33 private int fIndex = 0;
34 private int fZoomedIndex= 0;
35 private ITimeEvent fNext = null;
36 private ITimeEvent fSplitNext = null;
37 private ITimeEvent fZoomedNext = null;
38
39 /**
40 * Basic constructor, with start time and end times equal to the lowest and
41 * highest values possible, respectively.
42 *
43 * @param eventList
44 * The list on which this iterator will iterate
45 * @param zoomedEventList
46 * The "zoomed" list
47 */
48 public EventIterator(List<ITimeEvent> eventList, List<ITimeEvent> zoomedEventList) {
49 this(eventList, zoomedEventList, Long.MIN_VALUE, Long.MAX_VALUE);
50 }
51
52 /**
53 * Complete constructor, where we specify start and end times.
54 *
55 * @param eventList
56 * The list on which this iterator will iterate
57 * @param zoomedEventList
58 * The "zoomed" list
59 * @param startTime
60 * The start time
61 * @param endTime
62 * The end time
63 */
64 public EventIterator(List<ITimeEvent> eventList,
65 List<ITimeEvent> zoomedEventList, long startTime, long endTime) {
66 fEventList = eventList;
67 fZoomedEventList = zoomedEventList;
68 if (zoomedEventList != null && zoomedEventList.size() > 0) {
69 fZoomedStartTime = zoomedEventList.get(0).getTime();
70 ITimeEvent lastEvent = zoomedEventList.get(zoomedEventList.size() - 1);
71 fZoomedEndTime = lastEvent.getTime() + lastEvent.getDuration();
72 } else {
73 fZoomedStartTime = Long.MAX_VALUE;
74 fZoomedEndTime = Long.MIN_VALUE;
75 }
76 fStartTime = startTime;
77 fEndTime = endTime;
78 }
79
80 @Override
81 public boolean hasNext() {
82 if (fNext == null && fEventList != null) {
83 while (fIndex < fEventList.size()) {
84 ITimeEvent event = fEventList.get(fIndex++);
85 if (event.getTime() + event.getDuration() >= fStartTime && event.getTime() <= fEndTime &&
86 (event.getTime() < fZoomedStartTime || event.getTime() + event.getDuration() > fZoomedEndTime)) {
87 // the event is visible and is not completely hidden by the zoomed events
88 fNext = event;
89 if (event.getTime() < fZoomedEndTime && event.getTime() + event.getDuration() > fZoomedStartTime) {
90 // the event is partially hidden by the zoomed events and must be split
91 fNext = null;
92 if (event.getTime() + event.getDuration() > fZoomedEndTime && fZoomedEndTime < fEndTime) {
93 // the end of the event is partially hidden by the zoomed events and is visible
94 fNext = event.splitAfter(fZoomedEndTime);
95 }
96 if (event.getTime() < fZoomedStartTime && fZoomedStartTime > fStartTime) {
97 // the start of the event is partially hidden by the zoomed events and is visible
98 fSplitNext = fNext;
99 fNext = event.splitBefore(fZoomedStartTime);
100 }
101 }
102 if (fNext != null) {
103 break;
104 }
105 }
106 }
107 if (fNext == null) {
108 fEventList = null;
109 }
110 }
111
112 if (fZoomedNext == null && fZoomedEventList != null) {
113 while (fZoomedIndex < fZoomedEventList.size()) {
114 ITimeEvent event = fZoomedEventList.get(fZoomedIndex++);
115 if (event.getTime() + event.getDuration() >= fStartTime && event.getTime() <= fEndTime) {
116 // the zoomed event is visible
117 fZoomedNext = event;
118 break;
119 }
120 }
121 if (fZoomedNext == null) {
122 fZoomedEventList = null;
123 }
124 }
125
126 return fNext != null || fZoomedNext != null;
127 }
128
129 @Override
130 public ITimeEvent next() {
131 if (hasNext()) {
132 if (fZoomedNext != null && (fNext == null || fZoomedNext.getTime() <= fNext.getTime())) {
133 ITimeEvent event = fZoomedNext;
134 fZoomedNext = null;
135 return event;
136 }
137 ITimeEvent event = fNext;
138 fNext = fSplitNext;
139 fSplitNext = null;
140 return event;
141 }
142 throw new NoSuchElementException();
143 }
144
145 @Override
146 public void remove() {
147 throw new UnsupportedOperationException();
148 }
149 }
This page took 0.035605 seconds and 6 git commands to generate.