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; 020 021import java.util.Date; 022 023/** 024 * An abstract object that has a timestamp. 025 * That allows us to compare and order objects by the time. 026 */ 027public abstract class Time implements Comparable<Time> { 028 029 private final long timestamp; 030 031 /** 032 * Initialize the internal timestamp. Timestamp remains constant 033 * for the entire lifecycle of the object. 034 * @param timestamp timestamp in milliseconds since the beginning 035 * of the epoch (01/01/1970). 036 */ 037 public Time(final long timestamp) { 038 this.timestamp = timestamp; 039 } 040 041 /** 042 * Get timestamp in milliseconds since the beginning of the epoch (01/01/1970). 043 * @return Object's timestamp in milliseconds since the start of the epoch. 044 */ 045 public final long getTimestamp() { 046 return this.timestamp; 047 } 048 049 /** 050 * Get timestamp in milliseconds since the beginning of the epoch (01/01/1970). 051 * @return Object's timestamp in milliseconds since the start of the epoch. 052 * @deprecated [REEF-1532] Prefer using getTimestamp() instead. 053 * Remove after release 0.16. 054 */ 055 public final long getTimeStamp() { 056 return this.timestamp; 057 } 058 059 @Override 060 public String toString() { 061 return this.getClass().getName() 062 + ":[" + this.timestamp + '|' + new Date(this.timestamp) + ']'; 063 } 064 065 @Override 066 public int compareTo(final Time other) { 067 final int cmp = Long.compare(this.timestamp, other.timestamp); 068 return cmp != 0 ? cmp : Integer.compare(this.hashCode(), other.hashCode()); 069 } 070 071 @Override 072 public boolean equals(final Object other) { 073 return other instanceof Time && compareTo((Time) other) == 0; 074 } 075 076 @Override 077 public int hashCode() { 078 return super.hashCode(); 079 } 080}