blob: dc8817ca4a6ed09817255224e5579552067c541b [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 */
16package org.onosproject.store.impl;
17
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
32 private final long unixTimestamp;
33
34 public SystemClockTimestamp() {
35 unixTimestamp = System.nanoTime();
36 }
37
38 @Override
39 public int compareTo(Timestamp o) {
40 checkArgument(o instanceof SystemClockTimestamp,
41 "Must be SystemClockTimestamp", o);
42 SystemClockTimestamp that = (SystemClockTimestamp) o;
43
44 return ComparisonChain.start()
45 .compare(this.unixTimestamp, that.unixTimestamp)
46 .result();
47 }
48 @Override
49 public int hashCode() {
50 return Objects.hash(unixTimestamp);
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (this == obj) {
56 return true;
57 }
58 if (!(obj instanceof SystemClockTimestamp)) {
59 return false;
60 }
61 SystemClockTimestamp that = (SystemClockTimestamp) obj;
62 return Objects.equals(this.unixTimestamp, that.unixTimestamp);
63 }
64
65 @Override
66 public String toString() {
67 return MoreObjects.toStringHelper(getClass())
68 .add("unixTimestamp", unixTimestamp)
69 .toString();
70 }
71
72 public long systemTimestamp() {
73 return unixTimestamp;
74 }
75}