import time

This commit is contained in:
asofold 2014-06-04 21:54:48 +02:00
parent f4b109ec3b
commit 1ada725476
9 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package fr.neatmonster.nocheatplus.time;
/**
* Some kind of clock.
*
* @author mc_dev
*
*/
public interface Clock {
/**
* Get the clock counter. There is no guarantee that this is monotonic, nor
* need it be thread-safe.
*
* @return
*/
public long clock();
}

View File

@ -0,0 +1,58 @@
package fr.neatmonster.nocheatplus.time.monotonic;
/**
* Static provider for monotonic clocks. Note that some calls can only be made
* from the server/application main thread, thread safe methods are prefixed
* with "synch", however all call results may have different offsets.
*/
public class Monotonic {
private static final MonotonicClock nanos = new MonotonicNanosClock();
private static final MonotonicClock millis = new MonotonicMillisClock();
private static final MonotonicClock synchMillis = new MonotonicSynchClock(new MonotonicMillisClock());
private static final MonotonicClock synchNanos = new MonotonicSynchClock(new MonotonicNanosClock());
/**
* Monotonic nanoseconds time, corresponding to System.nanoTime(). <br>
* <b>Not thread-safe, only call from the main server/application
* thread.</b>
*
* @return Monotonic time in nanoseconds.
*/
public static long nanos() {
return nanos.clock();
}
/**
* Monotonic milliseconds time, corresponding to System.currentTimeMillis(). <br>
* <b>Not thread-safe, only call from the main server/application
* thread.</b>
*
* @return Monotonic time in milliseconds.
*/
public static long millis() {
return millis.clock();
}
/**
* Monotonic nanoseconds time, corresponding to System.nanoTime(). <br>
* Thread-safe.
*
* @return Monotonic time in nanoseconds.
*/
public static long synchNanos() {
return synchNanos.clock();
}
/**
* Monotonic milliseconds time, corresponding to System.currentTimeMillis(). <br>
* Thread-safe.
*
* @return Monotonic time in milliseconds.
*/
public static long synchMillis() {
return synchMillis.clock();
}
}

View File

@ -0,0 +1,43 @@
package fr.neatmonster.nocheatplus.time.monotonic;
/**
* Basic implementation of increasing a counter for a not necessarily monotonic
* underlying clock, fetched with fetchClock(). Not thread-safe.
*
*/
public abstract class MonotonicAbstractClock implements MonotonicClock {
private long clock;
private long lastFetch;
public MonotonicAbstractClock() {
clock = fetchClock();
lastFetch = clock;
}
public MonotonicAbstractClock(long clock) {
reset(clock);
}
protected abstract long fetchClock();
@Override
public long clock() {
// TODO: Add feature to detect running too fast and correction as well.
final long fetch = fetchClock();
final long diff = fetch - this.lastFetch;
if (diff > 0) {
this.clock += diff;
}
this.lastFetch = fetch;
return this.clock;
}
@Override
public void reset(long clock) {
this.clock = clock;
this.lastFetch = fetchClock();
}
}

View File

@ -0,0 +1,19 @@
package fr.neatmonster.nocheatplus.time.monotonic;
import fr.neatmonster.nocheatplus.time.Clock;
/**
* Monotonic clock. The clock() method will count since creation or call of
* reset, unless stated otherwise.
*
* @author mc_dev
*
*/
public interface MonotonicClock extends Clock {
/**
* Monotonic clock allow resetting for some reason.
*/
public void reset(long clock);
}

View File

@ -0,0 +1,16 @@
package fr.neatmonster.nocheatplus.time.monotonic;
/**
* Monotonic clock based on System.currentTimeMillis(). Not thread-safe.
*
* @author mc_dev
*
*/
public class MonotonicMillisClock extends MonotonicAbstractClock {
@Override
protected long fetchClock() {
return System.currentTimeMillis();
}
}

View File

@ -0,0 +1,16 @@
package fr.neatmonster.nocheatplus.time.monotonic;
/**
* Monotonic clock based on System.nanoTime(). Not thread-safe.
*
* @author mc_dev
*
*/
public class MonotonicNanosClock extends MonotonicAbstractClock {
@Override
protected long fetchClock() {
return System.nanoTime();
}
}

View File

@ -0,0 +1,29 @@
package fr.neatmonster.nocheatplus.time.monotonic;
/**
* Thread safe version of a monotonic clock, wrapping around a given clock.
* Since synchronized method bodies wrap around the underlying clock, the clock
* must not be used outside of this instance.
*
* @author mc_dev
*
*/
public class MonotonicSynchClock implements MonotonicClock {
private final MonotonicClock clock;
public MonotonicSynchClock(MonotonicClock clock) {
this.clock = clock;
}
@Override
public synchronized long clock() {
return clock.clock();
}
@Override
public synchronized void reset(long clock) {
this.clock.reset(clock);
}
}

View File

@ -0,0 +1,6 @@
/**
* Provides monotonic clocks. [Taken from NC4 Planning/Framework.]
* @author mc_dev
*
*/
package fr.neatmonster.nocheatplus.time.monotonic;

View File

@ -0,0 +1,4 @@
/**
* Time related functionality. [Taken from NC4 Planning/Framework.]
*/
package fr.neatmonster.nocheatplus.time;