001/* 002 * Copyright (C) 2011 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.base; 018 019import com.google.common.annotations.Beta; 020import com.google.common.annotations.GwtCompatible; 021 022import javax.annotation.CheckReturnValue; 023 024/** 025 * A time source; returns a time value representing the number of nanoseconds elapsed since some 026 * fixed but arbitrary point in time. Note that most users should use {@link Stopwatch} instead of 027 * interacting with this class directly. 028 * 029 * <p><b>Warning:</b> this interface can only be used to measure elapsed time, not wall time. 030 * 031 * @author Kevin Bourrillion 032 * @since 10.0 033 * (<a href="https://github.com/google/guava/wiki/Compatibility" 034 * >mostly source-compatible</a> since 9.0) 035 */ 036@Beta 037@GwtCompatible 038public abstract class Ticker { 039 /** 040 * Constructor for use by subclasses. 041 */ 042 protected Ticker() {} 043 044 /** 045 * Returns the number of nanoseconds elapsed since this ticker's fixed 046 * point of reference. 047 */ 048 public abstract long read(); 049 050 /** 051 * A ticker that reads the current time using {@link System#nanoTime}. 052 * 053 * @since 10.0 054 */ 055 @CheckReturnValue 056 public static Ticker systemTicker() { 057 return SYSTEM_TICKER; 058 } 059 060 private static final Ticker SYSTEM_TICKER = 061 new Ticker() { 062 @Override 063 public long read() { 064 return Platform.systemNanoTime(); 065 } 066 }; 067}