tmf: Switch tmf.core.tests to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / model / EventIterator.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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.linuxtools.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 if (event instanceof ITimeEvent2) {
95 fNext = ((ITimeEvent2) event).split(fZoomedEndTime).getSecond();
96 } else {
97 fNext = new TimeEvent(event.getEntry(), fZoomedEndTime, event.getTime() + event.getDuration() - fZoomedEndTime);
98 }
99 }
100 if (event.getTime() < fZoomedStartTime && fZoomedStartTime > fStartTime) {
101 // the start of the event is partially hidden by the zoomed events and is visible
102 fSplitNext = fNext;
103 if (event instanceof ITimeEvent2) {
104 fNext = ((ITimeEvent2) event).split(fZoomedStartTime).getFirst();
105 } else {
106 fNext = new TimeEvent(event.getEntry(), event.getTime(), fZoomedStartTime - event.getTime());
107 }
108 }
109 }
110 if (fNext != null) {
111 break;
112 }
113 }
114 }
115 if (fNext == null) {
116 fEventList = null;
117 }
118 }
119
120 if (fZoomedNext == null && fZoomedEventList != null) {
121 while (fZoomedIndex < fZoomedEventList.size()) {
122 ITimeEvent event = fZoomedEventList.get(fZoomedIndex++);
123 if (event.getTime() + event.getDuration() >= fStartTime && event.getTime() <= fEndTime) {
124 // the zoomed event is visible
125 fZoomedNext = event;
126 break;
127 }
128 }
129 if (fZoomedNext == null) {
130 fZoomedEventList = null;
131 }
132 }
133
134 return fNext != null || fZoomedNext != null;
135 }
136
137 @Override
138 public ITimeEvent next() {
139 if (hasNext()) {
140 if (fZoomedNext != null && (fNext == null || fZoomedNext.getTime() <= fNext.getTime())) {
141 ITimeEvent event = fZoomedNext;
142 fZoomedNext = null;
143 return event;
144 }
145 ITimeEvent event = fNext;
146 fNext = fSplitNext;
147 fSplitNext = null;
148 return event;
149 }
150 throw new NoSuchElementException();
151 }
152
153 @Override
154 public void remove() {
155 throw new UnsupportedOperationException();
156 }
157 }
This page took 0.052927 seconds and 5 git commands to generate.