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