blob: e6e16aad9db42ab4590c0454b08b408cdbed41f0 [file] [log] [blame]
Jordan Halterman948d6592017-04-20 17:18:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman948d6592017-04-20 17:18:24 -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 */
16package org.onosproject.store.service;
17
18import java.util.Objects;
19
20import com.google.common.base.MoreObjects;
21import com.google.common.collect.ComparisonChain;
22import org.onosproject.store.Timestamp;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26/**
27 * Logical timestamp for versions.
28 * <p>
29 * The version is a logical timestamp that represents a point in logical time at which an event occurs.
30 * This is used in both pessimistic and optimistic locking protocols to ensure that the state of a shared resource
31 * has not changed at the end of a transaction.
32 */
33public class Version implements Timestamp {
34 private final long version;
35
36 public Version(long version) {
37 this.version = version;
38 }
39
40 @Override
41 public int compareTo(Timestamp o) {
42 checkArgument(o instanceof Version,
43 "Must be LockVersion", o);
44 Version that = (Version) o;
45
46 return ComparisonChain.start()
47 .compare(this.version, that.version)
48 .result();
49 }
50
51 @Override
52 public int hashCode() {
53 return Long.hashCode(version);
54 }
55
56 @Override
57 public boolean equals(Object obj) {
58 if (this == obj) {
59 return true;
60 }
61 if (!(obj instanceof Version)) {
62 return false;
63 }
64 Version that = (Version) obj;
65 return Objects.equals(this.version, that.version);
66 }
67
68 @Override
69 public String toString() {
70 return MoreObjects.toStringHelper(getClass())
71 .add("version", version)
72 .toString();
73 }
74
75 /**
76 * Returns the lock version.
77 *
78 * @return the lock version
79 */
80 public long value() {
81 return this.version;
82 }
83}