1 | package org.amicofragile.time; |
2 | /** |
3 | * System time access abstraction class. |
4 | * @author Pietro Martinelli |
5 | * |
6 | */ |
7 | public final class SystemTime { |
8 | /** |
9 | * Default {@link TimeSource}: this {@link TimeSource} delegates to {@link System#currentTimeMillis()}. |
10 | */ |
11 | private static final TimeSource DEFAULT_TIME_SOURCE = new SystemTimeSource(); |
12 | /** |
13 | * Current {@link TimeSource} in use. |
14 | */ |
15 | private static TimeSource timeSource = DEFAULT_TIME_SOURCE; |
16 | /** |
17 | * Gets current time in milliseconds. |
18 | * @return current time in milliseconds |
19 | */ |
20 | public static long currentTimeInMillis() { |
21 | return timeSource.currentTimeInMillis(); |
22 | } |
23 | /** |
24 | * Sets {@linkplain TimeSource} provider to use. |
25 | * @param timeSource {@link TimeSource} provider to use |
26 | */ |
27 | public static void setTimeSource(final TimeSource timeSource) { |
28 | SystemTime.timeSource = timeSource; |
29 | } |
30 | /** |
31 | * Resets {@link TimeSource} provider to default. |
32 | */ |
33 | public static void reset() { |
34 | SystemTime.timeSource = DEFAULT_TIME_SOURCE; |
35 | } |
36 | /** |
37 | * This constructor throws InstantiationException at every invocation: this class is designed for static use only. |
38 | * @throws InstantiationException The class is designed for static use only |
39 | */ |
40 | protected SystemTime() throws InstantiationException { |
41 | throw new InstantiationException(); |
42 | } |
43 | } |