2 * Copyright 2015-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.utils.time;
18 import com.google.common.collect.ComparisonChain;
19 import io.atomix.utils.misc.TimestampPrinter;
21 import java.util.Objects;
23 import static com.google.common.base.Preconditions.checkArgument;
26 * A Timestamp that derives its value from the prevailing
27 * wallclock time on the controller where it is generated.
29 public class WallClockTimestamp implements Timestamp {
32 * Returns a new wall clock timestamp for the given unix timestamp.
34 * @param unixTimestamp the unix timestamp for which to create a new wall clock timestamp
35 * @return the wall clock timestamp
37 public static WallClockTimestamp from(long unixTimestamp) {
38 return new WallClockTimestamp(unixTimestamp);
41 private final long unixTimestamp;
43 public WallClockTimestamp() {
44 unixTimestamp = System.currentTimeMillis();
47 public WallClockTimestamp(long timestamp) {
48 unixTimestamp = timestamp;
52 public int compareTo(Timestamp o) {
53 checkArgument(o instanceof WallClockTimestamp,
54 "Must be WallClockTimestamp", o);
55 WallClockTimestamp that = (WallClockTimestamp) o;
57 return ComparisonChain.start()
58 .compare(this.unixTimestamp, that.unixTimestamp)
63 public int hashCode() {
64 return Long.hashCode(unixTimestamp);
68 public boolean equals(Object obj) {
72 if (!(obj instanceof WallClockTimestamp)) {
75 WallClockTimestamp that = (WallClockTimestamp) obj;
76 return Objects.equals(this.unixTimestamp, that.unixTimestamp);
80 public String toString() {
81 return new TimestampPrinter(unixTimestamp).toString();
85 * Returns the unixTimestamp.
87 * @return unix timestamp
89 public long unixTimestamp() {