blob: 500a09c77d1d03b2025499b4be4eebbecfe61c2d [file] [log] [blame]
Brian O'Connor2ba63fd2015-02-09 22:48:11 -08001/*
2 * Copyright 2015 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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080017
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ComparisonChain;
20import org.onosproject.store.Timestamp;
21
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26/**
27 * A Timestamp that derives its value from the system clock time (in ns)
28 * on the controller where it is generated.
29 */
30public class SystemClockTimestamp implements Timestamp {
31
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070032 private final long nanoTimestamp;
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080033
34 public SystemClockTimestamp() {
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070035 nanoTimestamp = System.nanoTime();
36 }
37
38 public SystemClockTimestamp(long timestamp) {
39 nanoTimestamp = timestamp;
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080040 }
41
42 @Override
43 public int compareTo(Timestamp o) {
44 checkArgument(o instanceof SystemClockTimestamp,
45 "Must be SystemClockTimestamp", o);
46 SystemClockTimestamp that = (SystemClockTimestamp) o;
47
48 return ComparisonChain.start()
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070049 .compare(this.nanoTimestamp, that.nanoTimestamp)
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080050 .result();
51 }
52 @Override
53 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070054 return Long.hashCode(nanoTimestamp);
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080055 }
56
57 @Override
58 public boolean equals(Object obj) {
59 if (this == obj) {
60 return true;
61 }
62 if (!(obj instanceof SystemClockTimestamp)) {
63 return false;
64 }
65 SystemClockTimestamp that = (SystemClockTimestamp) obj;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070066 return Objects.equals(this.nanoTimestamp, that.nanoTimestamp);
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080067 }
68
69 @Override
70 public String toString() {
71 return MoreObjects.toStringHelper(getClass())
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070072 .add("nanoTimestamp", nanoTimestamp)
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080073 .toString();
74 }
75
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070076 public long nanoTimestamp() {
77 return nanoTimestamp;
78 }
79
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080080 public long systemTimestamp() {
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070081 return nanoTimestamp / 1_000_000; // convert ns to ms
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080082 }
83}