2
* Copyright (C) 2008 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.testing;
19
import static com.google.common.base.Preconditions.checkArgument;
21
import com.google.common.annotations.GwtCompatible;
22
import com.google.common.annotations.GwtIncompatible;
23
import com.google.common.annotations.J2ktIncompatible;
24
import com.google.common.base.Ticker;
25
import com.google.errorprone.annotations.CanIgnoreReturnValue;
26
import java.time.Duration;
27
import java.util.concurrent.TimeUnit;
28
import java.util.concurrent.atomic.AtomicLong;
31
* A Ticker whose value can be advanced programmatically in test.
33
* <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called:
34
* see {@link #setAutoIncrementStep}.
36
* <p>This class is thread-safe.
41
@ElementTypesAreNonnullByDefault
43
public class FakeTicker extends Ticker {
45
private final AtomicLong nanos = new AtomicLong();
46
private volatile long autoIncrementStepNanos;
48
/** Advances the ticker value by {@code time} in {@code timeUnit}. */
49
@SuppressWarnings("GoodTime") // should accept a java.time.Duration
51
public FakeTicker advance(long time, TimeUnit timeUnit) {
52
return advance(timeUnit.toNanos(time));
55
/** Advances the ticker value by {@code nanoseconds}. */
56
@SuppressWarnings("GoodTime") // should accept a java.time.Duration
58
public FakeTicker advance(long nanoseconds) {
59
nanos.addAndGet(nanoseconds);
64
* Advances the ticker value by {@code duration}.
71
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
72
public FakeTicker advance(Duration duration) {
73
return advance(duration.toNanos());
77
* Sets the increment applied to the ticker whenever it is queried.
79
* <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
82
@SuppressWarnings("GoodTime") // should accept a java.time.Duration
84
public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) {
85
checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount");
86
this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep);
91
* Sets the increment applied to the ticker whenever it is queried.
93
* <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
100
@CanIgnoreReturnValue
101
@SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
102
public FakeTicker setAutoIncrementStep(Duration autoIncrementStep) {
103
return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS);
108
return nanos.getAndAdd(autoIncrementStepNanos);