diff options
author | Stiver <stiver.mail@gmail.com> | 2014-03-04 15:13:11 +0100 |
---|---|---|
committer | Stiver <stiver.mail@gmail.com> | 2014-03-04 15:13:11 +0100 |
commit | e2d0f5d9c38561d67f23754c00addb4a3547efb2 (patch) | |
tree | 1832f16037c086b48266b8566aecc61f45f4e5f1 /timer/com/vladium/utils/timing/JavaSystemTimer.java | |
download | fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar.gz fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar.lz fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.tar.xz fernflower-e2d0f5d9c38561d67f23754c00addb4a3547efb2.zip |
initial commit
Diffstat (limited to 'timer/com/vladium/utils/timing/JavaSystemTimer.java')
-rw-r--r-- | timer/com/vladium/utils/timing/JavaSystemTimer.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/timer/com/vladium/utils/timing/JavaSystemTimer.java b/timer/com/vladium/utils/timing/JavaSystemTimer.java new file mode 100644 index 0000000..e3f3b48 --- /dev/null +++ b/timer/com/vladium/utils/timing/JavaSystemTimer.java @@ -0,0 +1,74 @@ + +package com.vladium.utils.timing; + +// ---------------------------------------------------------------------------- +/** + * A package-private implementation of {@link ITimer} based around Java system + * timer [<code>System.currentTimeMillis()</code> method]. It is used when + * <code>HRTimer</code> implementation is unavailable.<P> + * + * {@link TimerFactory} acts as the Factory for this class.<P> + * + * MT-safety: an instance of this class is safe to be used within the same + * thread only. + * + * @author (C) <a href="mailto:vroubtsov@illinoisalumni.org">Vlad Roubtsov</a>, 2002 + */ +final class JavaSystemTimer implements ITimer, ITimerConstants +{ + // public: ................................................................ + + public void start () + { + if (DO_STATE_CHECKS) + { + if (m_state != STATE_READY) + throw new IllegalStateException (this + ": start() must be called from READY state, current state is " + STATE_NAMES [m_state]); + } + + if (DO_STATE_CHECKS) m_state = STATE_STARTED; + m_data = System.currentTimeMillis (); + } + + public void stop () + { + // latch stop time in a local var before doing anything else: + final long data = System.currentTimeMillis (); + + if (DO_STATE_CHECKS) + { + if (m_state != STATE_STARTED) + throw new IllegalStateException (this + ": stop() must be called from STARTED state, current state is " + STATE_NAMES [m_state]); + } + + m_data = data - m_data; + if (DO_STATE_CHECKS) m_state = STATE_STOPPED; + } + + public double getDuration () + { + if (DO_STATE_CHECKS) + { + if (m_state != STATE_STOPPED) + throw new IllegalStateException (this + ": getDuration() must be called from STOPPED state, current state is " + STATE_NAMES [m_state]); + } + + return m_data; + } + + public void reset () + { + if (DO_STATE_CHECKS) m_state = STATE_READY; + } + + // protected: ............................................................. + + // package: ............................................................... + + // private: ............................................................... + + private int m_state; // used to keep track of timer state + private long m_data; // timing data + +} // end of class +// ---------------------------------------------------------------------------- |