Removed unused import
[Mograsim.git] / era.mi / src / era / mi / logic / timeline / Timeline.java
1 package era.mi.logic.timeline;\r
2 \r
3 import java.util.PriorityQueue;\r
4 \r
5 /**\r
6  * Orders Events by the time they are due to be executed. Can execute Events individually.\r
7  * @author Fabian Stemmler\r
8  *\r
9  */\r
10 public class Timeline\r
11 {\r
12         private PriorityQueue<InnerEvent> events;\r
13         private long currentTime = 0;\r
14         \r
15         public Timeline(int initCapacity)\r
16         {\r
17                 events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) -> {\r
18                         long difference = a.getTiming() - b.getTiming();\r
19                         if(difference == 0)\r
20                                 return 0;\r
21                         return difference < 0 ? -1 : 1;\r
22                 });\r
23         }\r
24         \r
25         public boolean hasNext()\r
26         {\r
27                 return !events.isEmpty();\r
28         }\r
29 \r
30         public void executeNext()\r
31         {\r
32                 InnerEvent first = events.poll();\r
33                 currentTime = first.getTiming();\r
34                 first.run();\r
35         }\r
36         \r
37         public void executeAll()\r
38         {\r
39                 while (hasNext())\r
40                         executeNext();\r
41         }\r
42 \r
43         public long getSimulationTime()\r
44         {\r
45                 return currentTime;\r
46         }\r
47         \r
48         public void reset()\r
49         {\r
50                 events.clear();\r
51                 currentTime = 0;\r
52         }\r
53         \r
54         /**\r
55          * Adds an Event to the {@link Timeline}\r
56          * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline.\r
57          * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time.\r
58          */\r
59         public void addEvent(TimelineEventHandler function, int relativeTiming)\r
60         {\r
61                 long timing = currentTime + relativeTiming;\r
62                 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));\r
63         }\r
64         \r
65         private class InnerEvent\r
66         {\r
67 \r
68                 private final long timing;\r
69                 private final TimelineEventHandler function;\r
70                 private final TimelineEvent event;\r
71                 \r
72                 /**\r
73                  * Creates an {@link InnerEvent}\r
74                  * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs\r
75                  * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;\r
76                  */\r
77                 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)\r
78                 {\r
79                         this.function = function;\r
80                         this.event = event;\r
81                         this.timing = timing;\r
82                 }\r
83 \r
84                 public long getTiming()\r
85                 {\r
86                         return timing;\r
87                 }\r
88                 \r
89                 public void run()\r
90                 {\r
91                         function.handle(event);\r
92                 }\r
93                 \r
94                 @Override\r
95                 public String toString()\r
96                 {\r
97                         return event.toString();\r
98                 }\r
99         }\r
100         \r
101         @Override\r
102         public String toString()\r
103         {\r
104                 return "simulation time: " + currentTime + ", " + events.toString();\r
105         }\r
106         \r
107         public static long toNanoseconds(long ticks)\r
108         {\r
109                 return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time.\r
110         }\r
111 }