2 * Copyright 2017-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.collect.ComparisonChain;
20 import java.util.Objects;
22 import static com.google.common.base.MoreObjects.toStringHelper;
23 import static com.google.common.base.Preconditions.checkArgument;
26 * Logical timestamp for versions.
28 * The version is a logical timestamp that represents a point in logical time at which an event occurs.
29 * This is used in both pessimistic and optimistic locking protocols to ensure that the state of a shared resource
30 * has not changed at the end of a transaction.
32 public class Version implements Timestamp {
33 private final long version;
35 public Version(long version) {
36 this.version = version;
40 * Returns the version.
49 public int compareTo(Timestamp o) {
50 checkArgument(o instanceof Version,
51 "Must be LockVersion", o);
52 Version that = (Version) o;
54 return ComparisonChain.start()
55 .compare(this.version, that.version)
60 public int hashCode() {
61 return Long.hashCode(version);
65 public boolean equals(Object obj) {
69 if (!(obj instanceof Version)) {
72 Version that = (Version) obj;
73 return Objects.equals(this.version, that.version);
77 public String toString() {
78 return toStringHelper(getClass())
79 .add("version", version)