001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import com.google.common.annotations.Beta;
018import com.google.common.annotations.GwtCompatible;
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020
021/**
022 * A time source; returns a time value representing the number of nanoseconds elapsed since some
023 * fixed but arbitrary point in time. Note that most users should use {@link Stopwatch} instead of
024 * interacting with this class directly.
025 *
026 * <p><b>Warning:</b> this interface can only be used to measure elapsed time, not wall time.
027 *
028 * @author Kevin Bourrillion
029 * @since 10.0 (<a href="https://github.com/google/guava/wiki/Compatibility">mostly
030 *     source-compatible</a> since 9.0)
031 */
032@Beta
033@GwtCompatible
034public abstract class Ticker {
035  /** Constructor for use by subclasses. */
036  protected Ticker() {}
037
038  /** Returns the number of nanoseconds elapsed since this ticker's fixed point of reference. */
039  @CanIgnoreReturnValue // TODO(kak): Consider removing this
040  public abstract long read();
041
042  /**
043   * A ticker that reads the current time using {@link System#nanoTime}.
044   *
045   * @since 10.0
046   */
047  public static Ticker systemTicker() {
048    return SYSTEM_TICKER;
049  }
050
051  private static final Ticker SYSTEM_TICKER =
052      new Ticker() {
053        @Override
054        public long read() {
055          return Platform.systemNanoTime();
056        }
057      };
058}