public void sample()
    {
        // Loop attempts to find a measurement that is accurate to a given threshold
        long bestInitialCurrentNanoTime = 0, bestInitialNanoTime = 0;
        long bestNanoTimeWindow = Long.MAX_VALUE;

        final int maxMeasurementRetries = this.maxMeasurementRetries;
        final long measurementThresholdNs = this.measurementThresholdNs;

        for (int i = 0; i < maxMeasurementRetries; i++)
        {
            final long firstNanoTime = System.nanoTime();
            final long initialCurrentTimeMillis = System.currentTimeMillis();
            final long secondNanoTime = System.nanoTime();

            final long nanoTimeWindow = secondNanoTime - firstNanoTime;
            if (nanoTimeWindow < measurementThresholdNs)
            {
                timeFields = new TimeFields(
                    MILLISECONDS.toNanos(initialCurrentTimeMillis),
                    (firstNanoTime + secondNanoTime) >> 1,
                    true);
                return;
            }
            else if (nanoTimeWindow < bestNanoTimeWindow)
            {
                bestInitialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis);
                bestInitialNanoTime = (firstNanoTime + secondNanoTime) >> 1;
                bestNanoTimeWindow = nanoTimeWindow;
            }
        }

        // If we never get a time below the threshold, pick the narrowest window we've seen so far.
        timeFields = new TimeFields(
            bestInitialCurrentNanoTime,
            bestInitialNanoTime,
            false);
    }
@Test
    void shouldResampleSaneEpochTimestamp()
    {
        final OffsetEpochNanoClock clock = new OffsetEpochNanoClock();

        clock.sample();

        assertSaneEpochTimestamp(clock);
    }