Removed unused import
[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 void executeAll()
38         {
39                 while (hasNext())
40                         executeNext();
41         }
42
43         public long getSimulationTime()
44         {
45                 return currentTime;
46         }
47         
48         public void reset()
49         {
50                 events.clear();
51                 currentTime = 0;
52         }
53         
54         /**
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.
58          */
59         public void addEvent(TimelineEventHandler function, int relativeTiming)
60         {
61                 long timing = currentTime + relativeTiming;
62                 events.add(new InnerEvent(function, new TimelineEvent(timing), timing));
63         }
64         
65         private class InnerEvent
66         {
67
68                 private final long timing;
69                 private final TimelineEventHandler function;
70                 private final TimelineEvent event;
71                 
72                 /**
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;
76                  */
77                 InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
78                 {
79                         this.function = function;
80                         this.event = event;
81                         this.timing = timing;
82                 }
83
84                 public long getTiming()
85                 {
86                         return timing;
87                 }
88                 
89                 public void run()
90                 {
91                         function.handle(event);
92                 }
93                 
94                 @Override
95                 public String toString()
96                 {
97                         return event.toString();
98                 }
99         }
100         
101         @Override
102         public String toString()
103         {
104                 return "simulation time: " + currentTime + ", " + events.toString();
105         }
106         
107         public static long toNanoseconds(long ticks)
108         {
109                 return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time.
110         }
111 }