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