Import atomix/{storage,utils}
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / time / VectorTimestamp.java
1 /*
2  * Copyright 2017-present Open Networking Foundation
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  */
16 package io.atomix.utils.time;
17
18 import com.google.common.collect.ComparisonChain;
19 import io.atomix.utils.Identifier;
20
21 import java.util.Objects;
22
23 import static com.google.common.base.MoreObjects.toStringHelper;
24 import static com.google.common.base.Preconditions.checkArgument;
25
26 /**
27  * Vector clock timestamp.
28  */
29 public class VectorTimestamp<T extends Identifier> extends LogicalTimestamp {
30   private final T identifier;
31
32   public VectorTimestamp(T identifier, long value) {
33     super(value);
34     this.identifier = identifier;
35   }
36
37   /**
38    * Returns the timestamp identifier.
39    *
40    * @return the timestamp identifier
41    */
42   public T identifier() {
43     return identifier;
44   }
45
46   @Override
47   public int compareTo(Timestamp o) {
48     checkArgument(o instanceof VectorTimestamp, "Must be VectorTimestamp", o);
49     VectorTimestamp that = (VectorTimestamp) o;
50
51     return ComparisonChain.start()
52         .compare(this.identifier.id(), that.identifier.id())
53         .compare(this.value(), that.value())
54         .result();
55   }
56
57   @Override
58   public int hashCode() {
59     return Objects.hash(identifier(), value());
60   }
61
62   @Override
63   public boolean equals(Object obj) {
64     if (this == obj) {
65       return true;
66     }
67     if (!(obj instanceof VectorTimestamp)) {
68       return false;
69     }
70     VectorTimestamp that = (VectorTimestamp) obj;
71     return Objects.equals(this.identifier, that.identifier)
72         && Objects.equals(this.value(), that.value());
73   }
74
75   @Override
76   public String toString() {
77     return toStringHelper(this)
78         .add("identifier", identifier())
79         .add("value", value())
80         .toString();
81   }
82 }