Import atomix/{storage,utils}
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / time / MultiValuedTimestamp.java
1 /*
2  * Copyright 2015-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.base.Preconditions;
19 import com.google.common.collect.ComparisonChain;
20
21 import java.util.Objects;
22
23 import static com.google.common.base.MoreObjects.toStringHelper;
24
25 /**
26  * A logical timestamp that derives its value from two input values. The first
27  * value always takes precedence over the second value when comparing timestamps.
28  */
29 public class MultiValuedTimestamp<T extends Comparable<T>, U extends Comparable<U>> implements Timestamp {
30   private final T value1;
31   private final U value2;
32
33   /**
34    * Creates a new timestamp based on two values. The first value has higher
35    * precedence than the second when comparing timestamps.
36    *
37    * @param value1 first value
38    * @param value2 second value
39    */
40   public MultiValuedTimestamp(T value1, U value2) {
41     this.value1 = Preconditions.checkNotNull(value1);
42     this.value2 = Preconditions.checkNotNull(value2);
43   }
44
45   @Override
46   public int compareTo(Timestamp o) {
47     Preconditions.checkArgument(o instanceof MultiValuedTimestamp,
48         "Must be MultiValuedTimestamp", o);
49     MultiValuedTimestamp that = (MultiValuedTimestamp) o;
50
51     return ComparisonChain.start()
52         .compare(this.value1, that.value1)
53         .compare(this.value2, that.value2)
54         .result();
55   }
56
57   @Override
58   public int hashCode() {
59     return Objects.hash(value1, value2);
60   }
61
62   @Override
63   public boolean equals(Object obj) {
64     if (this == obj) {
65       return true;
66     }
67     if (!(obj instanceof MultiValuedTimestamp)) {
68       return false;
69     }
70     MultiValuedTimestamp that = (MultiValuedTimestamp) obj;
71     return Objects.equals(this.value1, that.value1)
72         && Objects.equals(this.value2, that.value2);
73   }
74
75   @Override
76   public String toString() {
77     return toStringHelper(getClass())
78         .add("value1", value1)
79         .add("value2", value2)
80         .toString();
81   }
82
83   /**
84    * Returns the first value.
85    *
86    * @return first value
87    */
88   public T value1() {
89     return value1;
90   }
91
92   /**
93    * Returns the second value.
94    *
95    * @return second value
96    */
97   public U value2() {
98     return value2;
99   }
100
101   // Default constructor for serialization
102   @SuppressWarnings("unused")
103   private MultiValuedTimestamp() {
104     this.value1 = null;
105     this.value2 = null;
106   }
107 }