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 long getSimulationTime()
49 * Adds an Event to the {@link Timeline}
50 * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.
51 * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.
53 public void addEvent(TimelineEventHandler function, int relativeTiming)
55 long timing = currentTime + relativeTiming;
56 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
59 private class InnerEvent
62 private final long timing;
63 private final TimelineEventHandler function;
64 private final TimelineEvent event;
67 * Creates an {@link InnerEvent}
68 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
69 * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
71 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
73 this.function = function;
78 public long getTiming()
85 function.handle(event);