blob: 3ce8f11a48621f674e42e5ca01451e75a9734e37 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
Madan Jampanifd26ffb2014-10-13 14:08:55 -070016package org.onlab.onos.store.impl;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.Objects;
21
22import org.onlab.onos.store.Timestamp;
23
24import com.google.common.base.MoreObjects;
25import com.google.common.collect.ComparisonChain;
26
27/**
28 * A Timestamp that derives its value from the prevailing
29 * wallclock time on the controller where it is generated.
30 */
31public class WallClockTimestamp implements Timestamp {
32
33 private final long unixTimestamp;
34
35 public WallClockTimestamp() {
36 unixTimestamp = System.currentTimeMillis();
37 }
38
39 @Override
40 public int compareTo(Timestamp o) {
41 checkArgument(o instanceof WallClockTimestamp,
42 "Must be WallClockTimestamp", o);
43 WallClockTimestamp that = (WallClockTimestamp) o;
44
45 return ComparisonChain.start()
46 .compare(this.unixTimestamp, that.unixTimestamp)
47 .result();
48 }
49 @Override
50 public int hashCode() {
51 return Objects.hash(unixTimestamp);
52 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (this == obj) {
57 return true;
58 }
59 if (!(obj instanceof WallClockTimestamp)) {
60 return false;
61 }
62 WallClockTimestamp that = (WallClockTimestamp) obj;
63 return Objects.equals(this.unixTimestamp, that.unixTimestamp);
64 }
65
66 @Override
67 public String toString() {
68 return MoreObjects.toStringHelper(getClass())
69 .add("unixTimestamp", unixTimestamp)
70 .toString();
71 }
72
73 /**
74 * Returns the unixTimestamp.
75 *
76 * @return unix timestamp
77 */
78 public long unixTimestamp() {
79 return unixTimestamp;
80 }
81}