001         package com.croftsoft.core.animation.clock;
002    
003         import com.croftsoft.core.animation.Clock;
004         import com.croftsoft.core.lang.NullArgumentException;
005         import com.croftsoft.core.math.MathConstants;
006    
007         /*********************************************************************
008         * Calculates the time delta.
009         *
010         * @version
011         *   2003-09-10
012         * @since
013         *   2003-04-02
014         * @author
015         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
016         *********************************************************************/
017    
018         public final class  Timekeeper
019         //////////////////////////////////////////////////////////////////////
020         //////////////////////////////////////////////////////////////////////
021         {
022    
023         private Clock   clock;
024    
025         private double  timeFactor;
026    
027         private long    previousTimeNanos;
028    
029         private double  timeDelta;
030    
031         //////////////////////////////////////////////////////////////////////
032         //////////////////////////////////////////////////////////////////////
033    
034         public  Timekeeper (
035           Clock   clock,
036           double  timeFactor )
037         //////////////////////////////////////////////////////////////////////
038         {
039           NullArgumentException.check ( this.clock = clock );
040    
041           this.timeFactor = timeFactor;
042    
043           previousTimeNanos = clock.currentTimeNanos ( );
044         }
045    
046         public  Timekeeper ( )
047         //////////////////////////////////////////////////////////////////////
048         {
049           this ( SystemClock.INSTANCE, 1.0 );
050         }
051    
052         //////////////////////////////////////////////////////////////////////
053         //////////////////////////////////////////////////////////////////////
054    
055         public double  getTimeFactor ( ) { return timeFactor; }
056    
057         public void  setTimeFactor ( double  timeFactor )
058         //////////////////////////////////////////////////////////////////////
059         {
060           this.timeFactor = timeFactor;
061         }
062    
063         //////////////////////////////////////////////////////////////////////
064         //////////////////////////////////////////////////////////////////////
065    
066         public void  update ( )
067         //////////////////////////////////////////////////////////////////////
068         {
069           long  currentTimeNanos = clock.currentTimeNanos ( );
070    
071           long  timeDeltaNanos = currentTimeNanos - previousTimeNanos;
072    
073           previousTimeNanos = currentTimeNanos;
074    
075           timeDelta = MathConstants.SECONDS_PER_NANOSECOND
076             * timeDeltaNanos * timeFactor;
077         }
078    
079         public double  getTimeDelta ( )
080         //////////////////////////////////////////////////////////////////////
081         {
082           return timeDelta;
083         }
084    
085         //////////////////////////////////////////////////////////////////////
086         //////////////////////////////////////////////////////////////////////
087         }