Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.impl; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkArgument; |
| 4 | |
| 5 | import java.util.Objects; |
| 6 | |
| 7 | import org.onlab.onos.store.Timestamp; |
| 8 | |
| 9 | import com.google.common.base.MoreObjects; |
| 10 | import 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 | */ |
| 16 | public 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 | } |