Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2011 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.base;
     18 
     19 import com.google.common.annotations.Beta;
     20 import com.google.common.annotations.GwtCompatible;
     21 
     22 /**
     23  * A time source; returns a time value representing the number of nanoseconds
     24  * elapsed since some fixed but arbitrary point in time.
     25  *
     26  * @author Kevin Bourrillion
     27  * @since 10.0
     28  *     (<a href="http://code.google.com/p/guava-libraries/wiki/Compatibility"
     29  *     >mostly source-compatible</a> since 9.0)
     30  */
     31 @Beta
     32 @GwtCompatible
     33 public abstract class Ticker {
     34   /**
     35    * Constructor for use by subclasses.
     36    */
     37   protected Ticker() {}
     38 
     39   /**
     40    * Returns the number of nanoseconds elapsed since this ticker's fixed
     41    * point of reference.
     42    */
     43   public abstract long read();
     44 
     45   /**
     46    * A ticker that reads the current time using {@link System#nanoTime}.
     47    *
     48    * @since 10.0
     49    */
     50   public static Ticker systemTicker() {
     51     return SYSTEM_TICKER;
     52   }
     53 
     54   private static final Ticker SYSTEM_TICKER = new Ticker() {
     55     @Override
     56     public long read() {
     57       return Platform.systemNanoTime();
     58     }
     59   };
     60 }
     61