Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
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 | |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 37 | private final int termNumber; |
| 38 | private final int sequenceNumber; |
| 39 | |
| 40 | /** |
| 41 | * Default version tuple. |
| 42 | * |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 43 | * @param termNumber the mastership termNumber |
| 44 | * @param sequenceNumber the sequenceNumber number within the termNumber |
| 45 | */ |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 46 | public MastershipBasedTimestamp(int termNumber, int sequenceNumber) { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 47 | this.termNumber = termNumber; |
| 48 | this.sequenceNumber = sequenceNumber; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public int compareTo(Timestamp o) { |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 53 | checkArgument(o instanceof MastershipBasedTimestamp, |
| 54 | "Must be MastershipBasedTimestamp", o); |
| 55 | MastershipBasedTimestamp that = (MastershipBasedTimestamp) o; |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 56 | |
| 57 | return ComparisonChain.start() |
| 58 | .compare(this.termNumber, that.termNumber) |
| 59 | .compare(this.sequenceNumber, that.sequenceNumber) |
| 60 | .result(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 65 | return Objects.hash(termNumber, sequenceNumber); |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object obj) { |
| 70 | if (this == obj) { |
| 71 | return true; |
| 72 | } |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 73 | if (!(obj instanceof MastershipBasedTimestamp)) { |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 76 | MastershipBasedTimestamp that = (MastershipBasedTimestamp) obj; |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 77 | return Objects.equals(this.termNumber, that.termNumber) && |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 78 | Objects.equals(this.sequenceNumber, that.sequenceNumber); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String toString() { |
| 83 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 84 | .add("termNumber", termNumber) |
| 85 | .add("sequenceNumber", sequenceNumber) |
| 86 | .toString(); |
| 87 | } |
| 88 | |
| 89 | /** |
Yuta HIGUCHI | 497c884 | 2014-09-25 14:23:34 -0700 | [diff] [blame] | 90 | * Returns the termNumber. |
| 91 | * |
| 92 | * @return termNumber |
| 93 | */ |
| 94 | public int termNumber() { |
| 95 | return termNumber; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the sequenceNumber number. |
| 100 | * |
| 101 | * @return sequenceNumber |
| 102 | */ |
| 103 | public int sequenceNumber() { |
| 104 | return sequenceNumber; |
| 105 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 106 | |
| 107 | // Default constructor for serialization |
| 108 | @Deprecated |
Yuta HIGUCHI | 273dcc8 | 2014-10-03 00:32:12 -0700 | [diff] [blame] | 109 | protected MastershipBasedTimestamp() { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 110 | this.termNumber = -1; |
| 111 | this.sequenceNumber = -1; |
| 112 | } |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 113 | } |