// Simulation.java import java.util.PriorityQueue; /** Simulation framework * @author Douglas W. Jones * @version 2019-04-12 */ public class Simulation { /** Users create subclasses of Event for each scheduled action */ public abstract class Event { /** The time at which the event will happen */ public float time; // the action to be taken when the even occurs abstract void trigger(); } // the central organizing data structure of the simulation private static final PriorityQueue eventSet = new PriorityQueue ( ( Event e1, Event e2 ) -> Float.compare( e1.time, e2.time ) ); /** schedule a new event *

The minimal call will look like this: *

Simulation.schedule( e ); *

More typically, a call will use an anonymous subclass like this: *

Simulation.schedule( new Simulation.event() { *
  time = ... *
  void trigger() { ... } *
} );
* @param e the event to schedule */ public static void schedule( Event e ) { eventSet.add( e ); } /** run a simulation * Call this after scheduling at least one event. *

The simulation will run until either there are no more events or * some event terminates the program. * From this point on, a typical simulation program will be event driven * with the ordering of computations determined by chronological ordering * of scheduled events. */ public static void run() { while (!eventSet.isEmpty()) { Event e = eventSet.remove(); e.trigger(); } } }