Event queue where simulation objects can request an update to happen at the specified simulation time. Multiple updates at the same time are merged to a single update.
| 13 | * are merged to a single update. |
| 14 | */ |
| 15 | public class ScheduledUpdatesQueue implements EventQueue { |
| 16 | /** Time of the event (simulated seconds) */ |
| 17 | private ExternalEvent nextEvent; |
| 18 | private List<ExternalEvent> updates; |
| 19 | |
| 20 | /** |
| 21 | * Constructor. Creates an empty update queue. |
| 22 | */ |
| 23 | public ScheduledUpdatesQueue(){ |
| 24 | this.nextEvent = new ExternalEvent(Double.MAX_VALUE); |
| 25 | this.updates = new ArrayList<ExternalEvent>(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Returns the next scheduled event or event with time Double.MAX_VALUE |
| 30 | * if there aren't any. |
| 31 | * @return the next scheduled event |
| 32 | */ |
| 33 | public ExternalEvent nextEvent() { |
| 34 | ExternalEvent event = this.nextEvent; |
| 35 | |
| 36 | if (this.updates.size() == 0) { |
| 37 | this.nextEvent = new ExternalEvent(Double.MAX_VALUE); |
| 38 | } |
| 39 | else { |
| 40 | this.nextEvent = this.updates.remove(0); |
| 41 | } |
| 42 | |
| 43 | return event; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the next scheduled event's time or Double.MAX_VALUE if there |
| 48 | * aren't any events left |
| 49 | * @return the next scheduled event's time |
| 50 | */ |
| 51 | public double nextEventsTime() { |
| 52 | return this.nextEvent.getTime(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Add a new update request for the given time |
| 57 | * @param simTime The time when the update should happen |
| 58 | */ |
| 59 | public void addUpdate(double simTime) { |
| 60 | ExternalEvent ee = new ExternalEvent(simTime); |
| 61 | |
| 62 | if (ee.compareTo(nextEvent) == 0) { // this event is already next |
| 63 | return; |
| 64 | } |
| 65 | else if (this.nextEvent.getTime() > simTime) { // new nextEvent |
| 66 | putToQueue(this.nextEvent); // put the old nextEvent back to q |
| 67 | this.nextEvent = ee; |
| 68 | } |
| 69 | else { // given event happens later.. |
| 70 | putToQueue(ee); |
| 71 | } |
| 72 | } |
nothing calls this directly
no outgoing calls
no test coverage detected