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