blob: 38d23c1a9315479f8951ce4fb2805cf506183653 [file] [log] [blame]
Madan Jampanifd26ffb2014-10-13 14:08:55 -07001package org.onlab.onos.store.impl;
2
3import static com.google.common.base.Preconditions.checkArgument;
4
5import java.util.Objects;
6
7import org.onlab.onos.store.Timestamp;
8
9import com.google.common.base.MoreObjects;
10import com.google.common.collect.ComparisonChain;
11
12/**
13 * A Timestamp that derives its value from the prevailing
14 * wallclock time on the controller where it is generated.
15 */
16public class WallClockTimestamp implements Timestamp {
17
18 private final long unixTimestamp;
19
20 public WallClockTimestamp() {
21 unixTimestamp = System.currentTimeMillis();
22 }
23
24 @Override
25 public int compareTo(Timestamp o) {
26 checkArgument(o instanceof WallClockTimestamp,
27 "Must be WallClockTimestamp", o);
28 WallClockTimestamp that = (WallClockTimestamp) o;
29
30 return ComparisonChain.start()
31 .compare(this.unixTimestamp, that.unixTimestamp)
32 .result();
33 }
34 @Override
35 public int hashCode() {
36 return Objects.hash(unixTimestamp);
37 }
38
39 @Override
40 public boolean equals(Object obj) {
41 if (this == obj) {
42 return true;
43 }
44 if (!(obj instanceof WallClockTimestamp)) {
45 return false;
46 }
47 WallClockTimestamp that = (WallClockTimestamp) obj;
48 return Objects.equals(this.unixTimestamp, that.unixTimestamp);
49 }
50
51 @Override
52 public String toString() {
53 return MoreObjects.toStringHelper(getClass())
54 .add("unixTimestamp", unixTimestamp)
55 .toString();
56 }
57
58 /**
59 * Returns the unixTimestamp.
60 *
61 * @return unix timestamp
62 */
63 public long unixTimestamp() {
64 return unixTimestamp;
65 }
66}