1 package era.mi.logic.timeline;
3 import java.util.PriorityQueue;
6 * Orders Events by the time they are due to be executed. Can execute Events individually.
7 * @author Fabian Stemmler
12 private PriorityQueue<InnerEvent> events;
13 private long currentTime = 0;
15 public Timeline(int initCapacity)
17 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {
18 long difference = a.getTiming() - b.getTiming();
21 return difference < 0 ? -1 : 1;
25 public boolean hasNext()
27 return !events.isEmpty();
30 public void executeNext()
32 InnerEvent first = events.poll();
33 currentTime = first.getTiming();
37 public void executeAll()
43 public long getSimulationTime()
55 * Adds an Event to the {@link Timeline}
56 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
57 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
59 public void addEvent(TimelineEventHandler function, int relativeTiming)
61 long timing = currentTime + relativeTiming;
62 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
65 private class InnerEvent
68 private final long timing;
69 private final TimelineEventHandler function;
70 private final TimelineEvent event;
73 * Creates an {@link InnerEvent}
74 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
75 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
77 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
79 this.function = function;
84 public long getTiming()
91 function.handle(event);