001package org.javasimon.clock;
002
003/**
004 * Interface for getting current time. By default {@link SimonClock#SYSTEM} is used to match System timers,
005 * but different implementation can be used to provide different time for {@link org.javasimon.Manager}.
006 * This also allows easier testing without sleeping.
007 *
008 * @author <a href="mailto:ivan.mushketyk@gmail.com">Ivan Mushketyk</a>
009 * @since 3.5
010 * @noinspection UnusedDeclaration
011 */
012public interface SimonClock {
013
014        /** Number of milliseconds in one second. */
015        long MILLIS_IN_SECOND = 1000;
016
017        /** Number of nanoseconds in one millisecond. */
018        long NANOS_IN_MILLIS = SimonUnit.MILLISECOND.getDivisor();
019
020        /** Number of nanoseconds in one second. */
021        long NANOS_IN_SECOND = SimonUnit.SECOND.getDivisor();
022
023        /**
024         * Gets current time in nanoseconds.
025         *
026         * @return current time in nanoseconds
027         */
028        long nanoTime();
029
030        /**
031         * Gets current time in milliseconds.
032         *
033         * @return current time in milliseconds
034         */
035        long milliTime();
036
037        /**
038         * Converts nano timer value into millis timestamp compatible with {@link #milliTime()} without actually
039         * using the timer. Method does not just divide nanos by one million, but also works with remembered
040         * values for milli- and nano-timers at one particular moment.
041         *
042         * @param nanos nano timer value
043         * @return ms timestamp
044         * @since 3.5 (before in SimonUtils since 3.3, before in SimonManager since 3.1)
045         */
046        long millisForNano(long nanos);
047
048        /** Default implementation using {@link java.lang.System#currentTimeMillis()} and {@link java.lang.System#nanoTime()}. */
049        SimonClock SYSTEM = new SimonClock() {
050                @Override
051                public long nanoTime() {
052                        return System.nanoTime();
053                }
054
055                @Override
056                public long milliTime() {
057                        return System.currentTimeMillis();
058                }
059
060                @Override
061                public long millisForNano(long nanos) {
062                        return SimonClockUtils.millisForNano(nanos);
063                }
064        };
065
066        /** Clock implementation measuring CPU time. */
067        SimonClock CPU = new CpuClock();
068}