-
-
Save unexist/1cecb9716e28b19f884ad39df5efaa24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tanapoln.utils; | |
import java.time.Clock; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
/** | |
* {@code TimeMachine} provide a central method for accessing system time with an ease on testing.<br> | |
* You can set system clock to be at a specific time. When your class using this class to retrieve time, | |
* it will break a coupling with real clock which cannot be tested. | |
*/ | |
public class TimeMachine { | |
private static Clock INSTANCE = Clock.systemDefaultZone(); | |
/** | |
* Prevent from create {@code TimeMachine} instance. | |
*/ | |
private TimeMachine() { | |
} | |
/** | |
* Set {@code Clock} instance. | |
* | |
* @param clock clock instance | |
*/ | |
public static void set(Clock clock) { | |
INSTANCE = clock; | |
} | |
/** | |
* Get current {@code Clock} instance. | |
* | |
* @return a clock instance | |
*/ | |
public static Clock get() { | |
return INSTANCE; | |
} | |
/** | |
* Fixed {@code Clock} instance at number of millisecond(s) since epoch time with default system zone. | |
* | |
* @param millis time in millisecond(s) | |
*/ | |
public static void fixedMillisecondDefaultZone(long millis) { | |
INSTANCE = Clock.fixed(Instant.ofEpochMilli(millis), ZoneId.systemDefault()); | |
} | |
/** | |
* Fixed {@code Clock} instance at number of millisecond(s) since epoch time with a specific zone. | |
* | |
* @param millis time in millisecond(s) | |
* @param zone zone id | |
*/ | |
public static void fixedMillisecond(long millis, ZoneId zone) { | |
INSTANCE = Clock.fixed(Instant.ofEpochMilli(millis), zone); | |
} | |
/** | |
* Reset {@code Clock} instance to be real system time with default system zone. | |
*/ | |
public static void resetToSystemDefaultZone() { | |
INSTANCE = Clock.systemDefaultZone(); | |
} | |
/** | |
* Reset {@code Clock} instance to be real system time with a specific zone. | |
* | |
* @param zone zone | |
*/ | |
public static void resetToSystem(ZoneId zone) { | |
INSTANCE = Clock.system(zone); | |
} | |
/** | |
* Create {@code LocalDateTime} instance with a {@code Clock} instance in this {@code TimeMachine}. | |
* | |
* @return local date time of current clock | |
*/ | |
public static LocalDateTime now() { | |
return LocalDateTime.now(INSTANCE); | |
} | |
/** | |
* @see Clock#instant() | |
*/ | |
public static Instant instant() { | |
return INSTANCE.instant(); | |
} | |
/** | |
* @see Clock#millis() | |
*/ | |
public static long millis() { | |
return INSTANCE.millis(); | |
} | |
/** | |
* @see Clock#getZone() | |
*/ | |
public static ZoneId getZone() { | |
return INSTANCE.getZone(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment