001    package edu.jhu.mb.ernst.engine.event;
002
003    import edu.jhu.mb.ernst.engine.DiscreteEvent;
004    
005    /***********************************************************************
006    * DiscreteEvent implementation that runs periodically.
007    * 
008    * @version
009    *   $Date: 2012-04-15 13:06:25 -0500 (Sun, 15 Apr 2012) $
010    *   $Rev: 7 $
011    *   $Author: croft $
012    * @since
013    *   2012-02-19
014    * @author
015    *   David Wallace Croft
016    ***********************************************************************/
017    public final class  PeriodicDiscreteEventImp
018      implements DiscreteEvent
019    ////////////////////////////////////////////////////////////////////////
020    ////////////////////////////////////////////////////////////////////////
021    {
022    
023    private final Runnable  runnable;
024    
025    private final double    period;
026    
027    private final double    stop;
028    
029    //
030    
031    private double  time;
032    
033    ////////////////////////////////////////////////////////////////////////
034    ////////////////////////////////////////////////////////////////////////
035    
036    public  PeriodicDiscreteEventImp (
037      final Runnable  runnable,
038      final double    period,
039      final double    start,
040      final double    stop )
041    ////////////////////////////////////////////////////////////////////////
042    {
043      this.runnable = runnable;
044      
045      this.period = period;
046      
047      this.stop = stop;
048      
049      time = start;
050    }
051    
052    ////////////////////////////////////////////////////////////////////////
053    ////////////////////////////////////////////////////////////////////////
054    
055    @Override
056    public double  getTime ( )
057    ////////////////////////////////////////////////////////////////////////
058    {
059      return time;
060    }
061
062    @Override
063    public boolean  process ( )
064    ////////////////////////////////////////////////////////////////////////
065    {
066      runnable.run ( );
067      
068      // TODO:  subject to drift; eliminate using integer tick
069      
070      time += period;
071      
072      return time < stop;
073    }
074    
075    ////////////////////////////////////////////////////////////////////////
076    ////////////////////////////////////////////////////////////////////////
077    }