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