Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store; |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 22 | import com.google.common.base.MoreObjects; |
| 23 | import 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 | */ |
| 30 | public 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 Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 51 | return Long.hashCode(value); |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 52 | } |
| 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 | } |