aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/Timer.java
blob: b5e32a0a297fb5c8762e6eea62b9d498f10bc2e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc;

import com.google.inject.ImplementedBy;
import com.yahoo.jdisc.core.SystemTimer;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;

/**
 * <p>This class provides access to the current time in milliseconds, as viewed by the {@link Container}. Inject an
 * instance of this class into any component that needs to access time, instead of using
 * <code>System.currentTimeMillis()</code>.</p>
 *
 * @author Simon Thoresen Hult
 */
@ImplementedBy(SystemTimer.class)
public interface Timer {

    /**
     * <p>Returns the current time in milliseconds. Note that while the unit of time of the return value is a
     * millisecond, the granularity of the value depends on the underlying operating system and may be larger. For
     * example, many operating systems measure time in units of tens of milliseconds.</p>
     *
     * <p> See the description of the class <code>Date</code> for a discussion of slight discrepancies that may arise
     * between "computer time" and coordinated universal time (UTC).</p>
     *
     * @return The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
     * @see java.util.Date
     */
    long currentTimeMillis();

    /** Convenience method for getting an java.util.Instance from currentTimeMillis(). */
    default Instant currentTime() {
        return Instant.ofEpochMilli(currentTimeMillis());
    }

    /** Return a UTC Clock backed by this timer. */
    default Clock toUtcClock() {
        return new ClockAdapter(this, ZoneOffset.UTC);
    }

    /** Create a Timer backed by the given Clock. */
    static Timer fromClock(Clock clock) {
        return clock::millis;
    }

    class ClockAdapter extends Clock {
        private final Timer timer;
        private final ZoneId zoneId;

        private ClockAdapter(Timer timer, ZoneId zoneId) {
            this.timer = timer;
            this.zoneId = zoneId;
        }

        @Override public ZoneId getZone() { return zoneId; }
        @Override public Clock withZone(ZoneId zone) { return new ClockAdapter(timer, zoneId); }
        @Override public Instant instant() { return timer.currentTime(); }
    }

}