2 * Copyright 2016-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils.time;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.ComparisonChain;
21 import java.util.Objects;
23 import static com.google.common.base.MoreObjects.toStringHelper;
26 * Timestamp based on logical sequence value.
28 * LogicalTimestamps are ordered by their sequence values.
30 public class LogicalTimestamp implements Timestamp {
33 * Returns a new logical timestamp for the given logical time.
35 * @param value the logical time for which to create a new logical timestamp
36 * @return the logical timestamp
38 public static LogicalTimestamp of(long value) {
39 return new LogicalTimestamp(value);
42 private final long value;
44 public LogicalTimestamp(long value) {
49 * Returns the sequence value.
51 * @return sequence value
58 * Returns the timestamp as a version.
60 * @return the timestamp as a version
62 public Version asVersion() {
63 return new Version(value);
67 public int compareTo(Timestamp o) {
68 Preconditions.checkArgument(o instanceof LogicalTimestamp,
69 "Must be LogicalTimestamp", o);
70 LogicalTimestamp that = (LogicalTimestamp) o;
72 return ComparisonChain.start()
73 .compare(this.value, that.value)
78 public int hashCode() {
79 return Long.hashCode(value);
83 public boolean equals(Object obj) {
87 if (!(obj instanceof LogicalTimestamp)) {
90 LogicalTimestamp that = (LogicalTimestamp) obj;
91 return Objects.equals(this.value, that.value);
95 public String toString() {
96 return toStringHelper(getClass())