001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.testing;
018
019import static com.google.common.base.Preconditions.checkArgument;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import com.google.common.base.Ticker;
025import com.google.errorprone.annotations.CanIgnoreReturnValue;
026import java.time.Duration;
027import java.util.concurrent.TimeUnit;
028import java.util.concurrent.atomic.AtomicLong;
029
030/**
031 * A Ticker whose value can be advanced programmatically in test.
032 *
033 * <p>The ticker can be configured so that the time is incremented whenever {@link #read} is called:
034 * see {@link #setAutoIncrementStep}.
035 *
036 * <p>This class is thread-safe.
037 *
038 * @author Jige Yu
039 * @since 10.0
040 */
041@ElementTypesAreNonnullByDefault
042@GwtCompatible
043public class FakeTicker extends Ticker {
044
045  private final AtomicLong nanos = new AtomicLong();
046  private volatile long autoIncrementStepNanos;
047
048  /** Advances the ticker value by {@code time} in {@code timeUnit}. */
049  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
050  @CanIgnoreReturnValue
051  public FakeTicker advance(long time, TimeUnit timeUnit) {
052    return advance(timeUnit.toNanos(time));
053  }
054
055  /** Advances the ticker value by {@code nanoseconds}. */
056  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
057  @CanIgnoreReturnValue
058  public FakeTicker advance(long nanoseconds) {
059    nanos.addAndGet(nanoseconds);
060    return this;
061  }
062
063  /**
064   * Advances the ticker value by {@code duration}.
065   *
066   * @since 28.0
067   */
068  @GwtIncompatible
069  @J2ktIncompatible
070  @CanIgnoreReturnValue
071  @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now.
072  public FakeTicker advance(Duration duration) {
073    return advance(duration.toNanos());
074  }
075
076  /**
077   * Sets the increment applied to the ticker whenever it is queried.
078   *
079   * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
080   * queried.
081   */
082  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
083  @CanIgnoreReturnValue
084  public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit) {
085    checkArgument(autoIncrementStep >= 0, "May not auto-increment by a negative amount");
086    this.autoIncrementStepNanos = timeUnit.toNanos(autoIncrementStep);
087    return this;
088  }
089
090  /**
091   * Sets the increment applied to the ticker whenever it is queried.
092   *
093   * <p>The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when
094   * queried.
095   *
096   * @since 28.0
097   */
098  @GwtIncompatible
099  @J2ktIncompatible
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);
104  }
105
106  @Override
107  public long read() {
108    return nanos.getAndAdd(autoIncrementStepNanos);
109  }
110}