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
031public abstract class Ticker {
032  /** Constructor for use by subclasses. */
033  protected Ticker() {}
034
035  /** Returns the number of nanoseconds elapsed since this ticker's fixed point of reference. */
036  public abstract long read();
037
038  /**
039   * A ticker that reads the current time using {@link System#nanoTime}.
040   *
041   * @since 10.0
042   */
043  public static Ticker systemTicker() {
044    return SYSTEM_TICKER;
045  }
046
047  private static final Ticker SYSTEM_TICKER =
048      new Ticker() {
049        @Override
050        public long read() {
051          return Platform.systemNanoTime();
052        }
053      };
054}