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