blob: eb634da9c71149386e6bab52d385e9d89b4a9500 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Jonathan Hart63939a32015-05-08 11:57:03 -070016package org.onosproject.store.service;
Madan Jampanifd26ffb2014-10-13 14:08:55 -070017
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.Objects;
21
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.store.Timestamp;
Madan Jampanifd26ffb2014-10-13 14:08:55 -070023
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
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070039 public WallClockTimestamp(long timestamp) {
40 unixTimestamp = timestamp;
41 }
42
Madan Jampanifd26ffb2014-10-13 14:08:55 -070043 @Override
44 public int compareTo(Timestamp o) {
45 checkArgument(o instanceof WallClockTimestamp,
46 "Must be WallClockTimestamp", o);
47 WallClockTimestamp that = (WallClockTimestamp) o;
48
49 return ComparisonChain.start()
50 .compare(this.unixTimestamp, that.unixTimestamp)
51 .result();
52 }
53 @Override
54 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070055 return Long.hashCode(unixTimestamp);
Madan Jampanifd26ffb2014-10-13 14:08:55 -070056 }
57
58 @Override
59 public boolean equals(Object obj) {
60 if (this == obj) {
61 return true;
62 }
63 if (!(obj instanceof WallClockTimestamp)) {
64 return false;
65 }
66 WallClockTimestamp that = (WallClockTimestamp) obj;
67 return Objects.equals(this.unixTimestamp, that.unixTimestamp);
68 }
69
70 @Override
71 public String toString() {
72 return MoreObjects.toStringHelper(getClass())
73 .add("unixTimestamp", unixTimestamp)
74 .toString();
75 }
76
77 /**
78 * Returns the unixTimestamp.
79 *
80 * @return unix timestamp
81 */
82 public long unixTimestamp() {
83 return unixTimestamp;
84 }
85}