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