X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Ftimeline%2FTimeline.java;h=186e9465fda2326beef4cb197760a6bfad48eb34;hb=bc46b5d9664f097be44f5147c5cc774d1410ad93;hp=2392f4c20d7329c00ba9891db67837d66c772acb;hpb=af660226c30929777520ffefea850ab890086716;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/timeline/Timeline.java b/era.mi/src/era/mi/logic/timeline/Timeline.java index 2392f4c2..186e9465 100644 --- a/era.mi/src/era/mi/logic/timeline/Timeline.java +++ b/era.mi/src/era/mi/logic/timeline/Timeline.java @@ -1,95 +1,111 @@ -package era.mi.logic.timeline; - -import java.util.PriorityQueue; - -/** - * Orders Events by the time they are due to be executed. Can execute Events individually. - * @author Fabian Stemmler - * - */ -public class Timeline -{ - private PriorityQueue events; - private long currentTime = 0; - - public Timeline(int initCapacity) - { - events = new PriorityQueue(initCapacity, (a, b) -> { - long difference = a.getTiming() - b.getTiming(); - if(difference == 0) - return 0; - return difference < 0 ? -1 : 1; - }); - } - - public boolean hasNext() - { - return !events.isEmpty(); - } - - public void executeNext() - { - InnerEvent first = events.poll(); - currentTime = first.getTiming(); - first.run(); - } - - public void executeAll() - { - while (hasNext()) - executeNext(); - } - - public long getSimulationTime() - { - return currentTime; - } - - public void reset() - { - events.clear(); - currentTime = 0; - } - - /** - * Adds an Event to the {@link Timeline} - * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline. - * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time. - */ - public void addEvent(TimelineEventHandler function, int relativeTiming) - { - long timing = currentTime + relativeTiming; - events.add(new InnerEvent(function, new TimelineEvent(timing), timing)); - } - - private class InnerEvent - { - - private final long timing; - private final TimelineEventHandler function; - private final TimelineEvent event; - - /** - * Creates an {@link InnerEvent} - * @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) - { - this.function = function; - this.event = event; - this.timing = timing; - } - - public long getTiming() - { - return timing; - } - - public void run() - { - function.handle(event); - } - - } +package era.mi.logic.timeline; + +import java.util.PriorityQueue; + +/** + * Orders Events by the time they are due to be executed. Can execute Events individually. + * @author Fabian Stemmler + * + */ +public class Timeline +{ + private PriorityQueue events; + private long currentTime = 0; + + public Timeline(int initCapacity) + { + events = new PriorityQueue(initCapacity, (a, b) -> { + long difference = a.getTiming() - b.getTiming(); + if(difference == 0) + return 0; + return difference < 0 ? -1 : 1; + }); + } + + public boolean hasNext() + { + return !events.isEmpty(); + } + + public void executeNext() + { + InnerEvent first = events.poll(); + currentTime = first.getTiming(); + first.run(); + } + + public void executeAll() + { + while (hasNext()) + executeNext(); + } + + public long getSimulationTime() + { + return currentTime; + } + + public void reset() + { + events.clear(); + currentTime = 0; + } + + /** + * Adds an Event to the {@link Timeline} + * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline. + * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time. + */ + public void addEvent(TimelineEventHandler function, int relativeTiming) + { + long timing = currentTime + relativeTiming; + events.add(new InnerEvent(function, new TimelineEvent(timing), timing)); + } + + private class InnerEvent + { + + private final long timing; + private final TimelineEventHandler function; + private final TimelineEvent event; + + /** + * Creates an {@link InnerEvent} + * @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) + { + this.function = function; + this.event = event; + this.timing = timing; + } + + public long getTiming() + { + return timing; + } + + public void run() + { + function.handle(event); + } + + @Override + public String toString() + { + return event.toString(); + } + } + + @Override + public String toString() + { + return "simulation time: " + currentTime + ", " + events.toString(); + } + + public static long toNanoseconds(long ticks) + { + return ticks; //TODO: Alter this when it has been determined how ticks should relate to real time. + } } \ No newline at end of file