Timeline now passed via constructor
[Mograsim.git] / era.mi / src / era / mi / logic / timeline / Timeline.java
index 63d7395..c22e1a4 100644 (file)
@@ -20,13 +20,7 @@ public class Timeline
 
        public Timeline(int initCapacity)
        {
-               events = new PriorityQueue<InnerEvent>(initCapacity, (a, b) ->
-               {
-                       long difference = a.getTiming() - b.getTiming();
-                       if (difference == 0)
-                               return 0;
-                       return difference < 0 ? -1 : 1;
-               });
+               events = new PriorityQueue<InnerEvent>(initCapacity);
 
                eventAddedListener = new ArrayList<>();
        }
@@ -38,9 +32,9 @@ public class Timeline
 
        public void executeNext()
        {
-               InnerEvent first = events.poll();
-               currentTime = first.getTiming();
-               first.run();
+               InnerEvent first = events.peek();
+               if (first != null)
+                       executeUpTo(first.getTiming(), -1);
        }
 
        public void executeAll()
@@ -53,8 +47,8 @@ public class Timeline
         * Executes all events up to a given simulation timestamp. The simulation process can be constrained by a real world timestamp.
         * 
         * @param timestamp  the simulation timestamp up to which the events will be processed
-        * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop.
-        * @return if it was possible to fulfil the goal in the given real world time.
+        * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout.
+        * @return if it was possible to fulfill the goal in the given real world time.
         * @author Christian Femers
         */
        public ExecutionResult executeUpTo(long timestamp, long stopMillis)
@@ -90,8 +84,7 @@ public class Timeline
        {
                if (!hasNext())
                        return -1;
-               else
-                       return events.peek().timing;
+               return events.peek().getTiming();
        }
 
        public void reset()
@@ -120,14 +113,12 @@ public class Timeline
        {
                long timing = currentTime + relativeTiming;
                TimelineEvent event = new TimelineEvent(timing);
-               events.add(new InnerEvent(function, event, timing));
+               events.add(new InnerEvent(function, event));
                eventAddedListener.forEach(l -> l.accept(event));
        }
 
-       private class InnerEvent
+       private class InnerEvent implements Runnable, Comparable<InnerEvent>
        {
-
-               private final long timing;
                private final TimelineEventHandler function;
                private final TimelineEvent event;
 
@@ -137,18 +128,18 @@ public class Timeline
                 * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs
                 * @param timing   Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed;
                 */
-               InnerEvent(TimelineEventHandler function, TimelineEvent event, long timing)
+               InnerEvent(TimelineEventHandler function, TimelineEvent event)
                {
                        this.function = function;
                        this.event = event;
-                       this.timing = timing;
                }
 
                public long getTiming()
                {
-                       return timing;
+                       return event.getTiming();
                }
 
+               @Override
                public void run()
                {
                        function.handle(event);
@@ -159,6 +150,15 @@ public class Timeline
                {
                        return event.toString();
                }
+
+               @Override
+               public int compareTo(InnerEvent o)
+               {
+                       long difference = getTiming() - o.getTiming();
+                       if (difference == 0)
+                               return 0;
+                       return difference < 0 ? -1 : 1;
+               }
        }
 
        @Override