001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.wake.time.runtime; 020 021import org.apache.reef.wake.time.Time; 022 023import javax.inject.Inject; 024 025/** 026 * Implementation of the Timer that uses the system clock. 027 */ 028public final class RealTimer implements Timer { 029 030 /** 031 * Instances of the timer should only be created automatically by Tang. 032 */ 033 @Inject 034 private RealTimer() { 035 } 036 037 /** 038 * Get current time in milliseconds since the beginning of the epoch (01/01/1970). 039 * @return Current system time in milliseconds since the start of the epoch. 040 */ 041 @Override 042 public long getCurrent() { 043 return System.currentTimeMillis(); 044 } 045 046 /** 047 * Get the number of milliseconds from current system time to the given event. 048 * Can return a negative number if the event is already in the past. 049 * @param time Timestamp in milliseconds. 050 * @return Difference in milliseconds between the given timestamp and current system time. 051 * The result is a negative number if the timestamp is in the past. 052 * @deprecated [REEF-1532] Prefer passing Time object instead of the numeric timestamp. 053 * Remove after release 0.16. 054 */ 055 @Override 056 public long getDuration(final long time) { 057 return time - getCurrent(); 058 } 059 060 /** 061 * Get the number of milliseconds from current system time to the given event. 062 * Can return a negative number if the event is already in the past. 063 * @param time Timestamp object that wraps time in milliseconds. 064 * @return Difference in milliseconds between the given timestamp and current system time. 065 * The result is a negative number if the timestamp is in the past. 066 */ 067 @Override 068 public long getDuration(final Time time) { 069 return time.getTimestamp() - getCurrent(); 070 } 071 072 /** 073 * Check if the event with a given timestamp has occurred. Return true if the timestamp 074 * equals or less than the current system time, and false if it is still in the future. 075 * @param time Timestamp in milliseconds. 076 * @return False if the given timestamp is still in the future. 077 * @deprecated [REEF-1532] Prefer passing Time object instead of the numeric timestamp. 078 * Remove after release 0.16. 079 */ 080 @Override 081 public boolean isReady(final long time) { 082 return getDuration(time) <= 0; 083 } 084 085 /** 086 * Check if the event with a given timestamp has occurred. Return true if the timestamp 087 * equals or less than the current system time, and false if it is still in the future. 088 * @param time Timestamp object that wraps time in milliseconds. 089 * @return False if the given timestamp is still in the future. 090 */ 091 @Override 092 public boolean isReady(final Time time) { 093 return getDuration(time) <= 0; 094 } 095}