blob: 50ab244559c04a95fc728d0a4fdc3ba256dfdc3f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
Yuta HIGUCHI8ce08732014-10-11 10:40:45 -070016package org.onlab.onos.store.impl;
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070017
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070018import static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.Objects;
21
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070022import org.onlab.onos.store.Timestamp;
23
24import com.google.common.base.MoreObjects;
25import com.google.common.collect.ComparisonChain;
26
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070027/**
Madan Jampanifd26ffb2014-10-13 14:08:55 -070028 * 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 HIGUCHI497c8842014-09-25 14:23:34 -070034 */
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070035public final class MastershipBasedTimestamp implements Timestamp {
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070036
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070037 private final int termNumber;
38 private final int sequenceNumber;
39
40 /**
41 * Default version tuple.
42 *
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070043 * @param termNumber the mastership termNumber
44 * @param sequenceNumber the sequenceNumber number within the termNumber
45 */
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070046 public MastershipBasedTimestamp(int termNumber, int sequenceNumber) {
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070047 this.termNumber = termNumber;
48 this.sequenceNumber = sequenceNumber;
49 }
50
51 @Override
52 public int compareTo(Timestamp o) {
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070053 checkArgument(o instanceof MastershipBasedTimestamp,
54 "Must be MastershipBasedTimestamp", o);
55 MastershipBasedTimestamp that = (MastershipBasedTimestamp) o;
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070056
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 Jampani61056bc2014-09-27 09:07:26 -070065 return Objects.hash(termNumber, sequenceNumber);
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070066 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (this == obj) {
71 return true;
72 }
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070073 if (!(obj instanceof MastershipBasedTimestamp)) {
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070074 return false;
75 }
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -070076 MastershipBasedTimestamp that = (MastershipBasedTimestamp) obj;
Madan Jampani61056bc2014-09-27 09:07:26 -070077 return Objects.equals(this.termNumber, that.termNumber) &&
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070078 Objects.equals(this.sequenceNumber, that.sequenceNumber);
79 }
80
81 @Override
82 public String toString() {
83 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070084 .add("termNumber", termNumber)
85 .add("sequenceNumber", sequenceNumber)
86 .toString();
87 }
88
89 /**
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070090 * 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 HIGUCHI67a527f2014-10-02 22:23:54 -0700106
107 // Default constructor for serialization
108 @Deprecated
Yuta HIGUCHI273dcc82014-10-03 00:32:12 -0700109 protected MastershipBasedTimestamp() {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700110 this.termNumber = -1;
111 this.sequenceNumber = -1;
112 }
Yuta HIGUCHI2e963892014-09-27 13:00:39 -0700113}