001 package com.croftsoft.core.sim;
002
003 import com.croftsoft.core.lang.lifecycle.Startable;
004 import com.croftsoft.core.lang.lifecycle.Updatable;
005 import com.croftsoft.core.math.MathConstants;
006
007 /***********************************************************************
008 * DeltaClock implementation.
009 *
010 * @version
011 * $Id: NanoDeltaClock.java,v 1.3 2008/08/17 22:39:46 croft Exp $
012 * @since
013 * 2008-07-06
014 * @author
015 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
016 ***********************************************************************/
017
018 public final class NanoDeltaClock
019 implements Startable, Updatable, DeltaClock
020 ////////////////////////////////////////////////////////////////////////
021 ////////////////////////////////////////////////////////////////////////
022 {
023
024 private long nanoTime;
025
026 private double deltaTime;
027
028 ////////////////////////////////////////////////////////////////////////
029 ////////////////////////////////////////////////////////////////////////
030
031 public NanoDeltaClock ( final long nanoTime )
032 ////////////////////////////////////////////////////////////////////////
033 {
034 this.nanoTime = nanoTime;
035 }
036
037 public NanoDeltaClock ( )
038 ////////////////////////////////////////////////////////////////////////
039 {
040 this ( System.nanoTime ( ) );
041 }
042
043 ////////////////////////////////////////////////////////////////////////
044 // accessor methods
045 ////////////////////////////////////////////////////////////////////////
046
047 public long getNanoTime ( )
048 ////////////////////////////////////////////////////////////////////////
049 {
050 return nanoTime;
051 }
052
053 public double getDeltaTime ( )
054 ////////////////////////////////////////////////////////////////////////
055 {
056 return deltaTime;
057 }
058
059 ////////////////////////////////////////////////////////////////////////
060 // mutator methods
061 ////////////////////////////////////////////////////////////////////////
062
063 public void setNanoTime ( final long nanoTime )
064 ////////////////////////////////////////////////////////////////////////
065 {
066 this.nanoTime = nanoTime;
067 }
068
069 public void setDeltaTime ( final double deltaTime )
070 ////////////////////////////////////////////////////////////////////////
071 {
072 this.deltaTime = deltaTime;
073 }
074
075 ////////////////////////////////////////////////////////////////////////
076 // lifecycle methods
077 ////////////////////////////////////////////////////////////////////////
078
079 public void start ( )
080 ////////////////////////////////////////////////////////////////////////
081 {
082 nanoTime = System.nanoTime ( );
083 }
084
085 public void update ( )
086 ////////////////////////////////////////////////////////////////////////
087 {
088 final long oldNanoTime = nanoTime;
089
090 nanoTime = System.nanoTime ( );
091
092 deltaTime = ( double ) ( nanoTime - oldNanoTime )
093 / MathConstants.NANOSECONDS_PER_SECOND;
094 }
095
096 ////////////////////////////////////////////////////////////////////////
097 ////////////////////////////////////////////////////////////////////////
098 }