Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.impl; |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.store.Timestamp; |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | import com.google.common.collect.ComparisonChain; |
| 26 | |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 27 | /** |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 28 | * A logical timestamp that derives its value from two things: |
| 29 | * <ul> |
| 30 | * <li> The current mastership term of the device.</li> |
| 31 | * <li> The value of the counter used for tracking topology events observed from |
| 32 | * the device during that current time of a device. </li> |
| 33 | * </ul> |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 34 | */ |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 35 | public final class MastershipBasedTimestamp implements Timestamp { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 36 | |
Madan Jampani | 15d773c | 2015-02-25 15:31:55 -0800 | [diff] [blame] | 37 | private final long termNumber; |
| 38 | private final long sequenceNumber; |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Ray Milkey | 3071c16 | 2016-02-22 10:11:30 -0800 | [diff] [blame] | 41 | * Default constructor for serialization. |
| 42 | */ |
| 43 | protected MastershipBasedTimestamp() { |
| 44 | this.termNumber = -1; |
| 45 | this.sequenceNumber = -1; |
| 46 | } |
| 47 | |
| 48 | /** |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 49 | * Default version tuple. |
| 50 | * |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 51 | * @param termNumber the mastership termNumber |
| 52 | * @param sequenceNumber the sequenceNumber number within the termNumber |
| 53 | */ |
Madan Jampani | 15d773c | 2015-02-25 15:31:55 -0800 | [diff] [blame] | 54 | public MastershipBasedTimestamp(long termNumber, long sequenceNumber) { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 55 | this.termNumber = termNumber; |
| 56 | this.sequenceNumber = sequenceNumber; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int compareTo(Timestamp o) { |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 61 | checkArgument(o instanceof MastershipBasedTimestamp, |
| 62 | "Must be MastershipBasedTimestamp", o); |
| 63 | MastershipBasedTimestamp that = (MastershipBasedTimestamp) o; |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 64 | |
| 65 | return ComparisonChain.start() |
| 66 | .compare(this.termNumber, that.termNumber) |
| 67 | .compare(this.sequenceNumber, that.sequenceNumber) |
| 68 | .result(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int hashCode() { |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 73 | return Objects.hash(termNumber, sequenceNumber); |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean equals(Object obj) { |
| 78 | if (this == obj) { |
| 79 | return true; |
| 80 | } |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 81 | if (!(obj instanceof MastershipBasedTimestamp)) { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 84 | MastershipBasedTimestamp that = (MastershipBasedTimestamp) obj; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 85 | return Objects.equals(this.termNumber, that.termNumber) && |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 86 | Objects.equals(this.sequenceNumber, that.sequenceNumber); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String toString() { |
| 91 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 92 | .add("termNumber", termNumber) |
| 93 | .add("sequenceNumber", sequenceNumber) |
| 94 | .toString(); |
| 95 | } |
| 96 | |
| 97 | /** |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 98 | * Returns the termNumber. |
| 99 | * |
| 100 | * @return termNumber |
| 101 | */ |
Madan Jampani | 15d773c | 2015-02-25 15:31:55 -0800 | [diff] [blame] | 102 | public long termNumber() { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 103 | return termNumber; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the sequenceNumber number. |
| 108 | * |
| 109 | * @return sequenceNumber |
| 110 | */ |
Madan Jampani | 15d773c | 2015-02-25 15:31:55 -0800 | [diff] [blame] | 111 | public long sequenceNumber() { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 112 | return sequenceNumber; |
| 113 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 115 | } |