\r
private final List<Consumer<TimelineEvent>> eventAddedListener;\r
\r
+ public final LongSupplier stepByStepExec = () -> lastTimeUpdated;\r
+ public final LongSupplier realTimeExec = () -> System.currentTimeMillis();\r
+\r
+ /**\r
+ * Constructs a Timeline object. Per default the time function is set to step by step execution.\r
+ * \r
+ * @param initCapacity The initial capacity of the event queue.\r
+ */\r
public Timeline(int initCapacity)\r
{\r
events = new PriorityQueue<>(initCapacity);\r
-\r
eventAddedListener = new ArrayList<>();\r
- time = () -> lastTimeUpdated;\r
+ time = stepByStepExec;\r
}\r
\r
/**\r
* @param timestamp exclusive\r
- * @return true if the first event is later than the timestamp\r
+ * @return <code>true</code> if the first event in queue is later than the given timestamp\r
*/\r
public BooleanSupplier laterThan(long timestamp)\r
{\r
return () -> timeCmp(events.peek().getTiming(), timestamp) > 0;\r
}\r
\r
+ /**\r
+ * @return <code>true</code> if there is at least one event enqueued. <code>false</code> otherwise\r
+ */\r
public boolean hasNext()\r
{\r
return !events.isEmpty();\r
executeUntil(laterThan(first.getTiming()), -1);\r
}\r
\r
+ /**\r
+ * Executes all events enqueued in the {@link Timeline}. Use very carefully! Events may generate new events, causing an infinite loop.\r
+ */\r
public void executeAll()\r
{\r
while (hasNext())\r
* <code>EXEC_UNTIL_CONDITION</code> if the condition was met\r
* <code>EXEC_UNTIL_EMPTY</code> if events were executed until the {@link Timeline} was empty\r
* @formatter:on\r
- * @author Christian Femers, Fabian Stemmler\r
*/\r
public ExecutionResult executeUntil(BooleanSupplier condition, long stopMillis)\r
{\r
return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION;\r
}\r
\r
+ /**\r
+ * Sets the function, which defines the current simulation time at any time.\r
+ * \r
+ * @param time The return value of calling this function is the current simulation time.\r
+ */\r
public void setTimeFunction(LongSupplier time)\r
{\r
this.time = time;\r
}\r
\r
+ /**\r
+ * Calculates the current simulation time.\r
+ * \r
+ * @return The simulation time as defined by the time function.\r
+ */\r
public long getSimulationTime()\r
{\r
return time.getAsLong();\r
}\r
\r
+ /**\r
+ * Retrieves the timestamp of the next event.\r
+ * \r
+ * @return The timestamp of the next enqueued event, if the {@link Timeline} is not empty, -1 otherwise.\r
+ */\r
public long nextEventTime()\r
{\r
if (!hasNext())\r
return events.peek().getTiming();\r
}\r
\r
+ /**\r
+ * Clears the {@link Timeline} of enqueued events.\r
+ */\r
public void reset()\r
{\r
events.clear();\r
lastTimeUpdated = 0;\r
}\r
\r
+ /**\r
+ * Adds a listener, that is called when a {@link TimelineEvent} is added.\r
+ */\r
public void addEventAddedListener(Consumer<TimelineEvent> listener)\r
{\r
eventAddedListener.add(listener);\r
}\r
\r
+ /**\r
+ * Removes the listener, if possible. It will no longer be called when a {@link TimelineEvent} is added.\r
+ */\r
public void removeEventAddedListener(Consumer<TimelineEvent> listener)\r
{\r
eventAddedListener.remove(listener);\r