125b69c3ff376cd4aa7a95dc9ae6669b32a395af
[Mograsim.git] / era.mi / src / era / mi / logic / timeline / Timeline.java
1 package era.mi.logic.timeline;
2
3 import java.util.PriorityQueue;
4
5 /**
6  * Orders Events by the time they are due to be executed. Can execute Events individually.
7  * @author Fabian Stemmler
8  *
9  */
10 public class Timeline
11 {
12         private PriorityQueue<InnerEvent> events;
13         private long currentTime = 0;
14         
15         public Timeline(int initCapacity)
16         {
17                 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {
18                         long difference = a.getTiming() - b.getTiming();
19                         if(difference == 0)
20                                 return 0;
21                         return difference < 0 ? -1 : 1;
22                 });
23         }
24         
25         public boolean hasNext()
26         {
27                 return !events.isEmpty();
28         }
29
30         public void executeNext()
31         {
32                 InnerEvent first = events.poll();
33                 currentTime = first.getTiming();
34                 first.run();
35         }
36
37         public long getSimulationTime()
38         {
39                 return currentTime;
40         }
41         
42         public void reset()
43         {
44                 events.clear();
45                 currentTime = 0;
46         }
47         
48         /**
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.
52          */
53         public void addEvent(TimelineEventHandler function, int relativeTiming)
54         {
55                 long timing = currentTime + relativeTiming;
56                 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
57         }
58         
59         private class InnerEvent
60         {
61
62                 private final long timing;
63                 private final TimelineEventHandler function;
64                 private final TimelineEvent event;
65                 
66                 /**
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;
70                  */
71                 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
72                 {
73                         this.function = function;
74                         this.event = event;
75                         this.timing = timing;
76                 }
77
78                 public long getTiming()
79                 {
80                         return timing;
81                 }
82                 
83                 public void run()
84                 {
85                         function.handle(event);
86                 }
87                 
88         }
89 }