blob: 43341bd7c6a822a7fad6e8e34165fb5fe67ce134 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store;
Madan Jampani3e033bd2015-04-08 13:03:49 -070017
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.Objects;
21
Madan Jampani3e033bd2015-04-08 13:03:49 -070022import com.google.common.base.MoreObjects;
23import com.google.common.collect.ComparisonChain;
24
25/**
26 * Timestamp based on logical sequence value.
27 * <p>
28 * LogicalTimestamps are ordered by their sequence values.
29 */
30public class LogicalTimestamp implements Timestamp {
31
32 private final long value;
33
34 public LogicalTimestamp(long value) {
35 this.value = value;
36 }
37
38 @Override
39 public int compareTo(Timestamp o) {
40 checkArgument(o instanceof LogicalTimestamp,
41 "Must be LogicalTimestamp", o);
42 LogicalTimestamp that = (LogicalTimestamp) o;
43
44 return ComparisonChain.start()
45 .compare(this.value, that.value)
46 .result();
47 }
48
49 @Override
50 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070051 return Long.hashCode(value);
Madan Jampani3e033bd2015-04-08 13:03:49 -070052 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (this == obj) {
57 return true;
58 }
59 if (!(obj instanceof LogicalTimestamp)) {
60 return false;
61 }
62 LogicalTimestamp that = (LogicalTimestamp) obj;
63 return Objects.equals(this.value, that.value);
64 }
65
66 @Override
67 public String toString() {
68 return MoreObjects.toStringHelper(getClass())
69 .add("value", value)
70 .toString();
71 }
72
73 /**
74 * Returns the sequence value.
75 *
76 * @return sequence value
77 */
78 public long value() {
79 return this.value;
80 }
81}