Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2008 The Guava Authors
      3  *
      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
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package com.google.common.testing;
     18 
     19 import com.google.common.annotations.Beta;
     20 import com.google.common.annotations.GwtCompatible;
     21 import com.google.common.base.Ticker;
     22 
     23 import java.util.concurrent.TimeUnit;
     24 import java.util.concurrent.atomic.AtomicLong;
     25 
     26 /**
     27  * A Ticker whose value can be advanced programmatically in test.
     28  * <p>
     29  * This class is thread-safe.
     30  *
     31  * @author Jige Yu
     32  * @since 10.0
     33  */
     34 @Beta
     35 @GwtCompatible
     36 public class FakeTicker extends Ticker {
     37 
     38   private final AtomicLong nanos = new AtomicLong();
     39 
     40   /** Advances the ticker value by {@code time} in {@code timeUnit}. */
     41   public FakeTicker advance(long time, TimeUnit timeUnit) {
     42     return advance(timeUnit.toNanos(time));
     43   }
     44 
     45   /** Advances the ticker value by {@code nanoseconds}. */
     46   public FakeTicker advance(long nanoseconds) {
     47     nanos.addAndGet(nanoseconds);
     48     return this;
     49   }
     50 
     51   @Override public long read() {
     52     return nanos.get();
     53   }
     54 }
     55