2 * Copyright 2015-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils.time;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.ComparisonChain;
21 import java.util.Objects;
23 import static com.google.common.base.MoreObjects.toStringHelper;
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.
29 public class MultiValuedTimestamp<T extends Comparable<T>, U extends Comparable<U>> implements Timestamp {
30 private final T value1;
31 private final U value2;
34 * Creates a new timestamp based on two values. The first value has higher
35 * precedence than the second when comparing timestamps.
37 * @param value1 first value
38 * @param value2 second value
40 public MultiValuedTimestamp(T value1, U value2) {
41 this.value1 = Preconditions.checkNotNull(value1);
42 this.value2 = Preconditions.checkNotNull(value2);
46 public int compareTo(Timestamp o) {
47 Preconditions.checkArgument(o instanceof MultiValuedTimestamp,
48 "Must be MultiValuedTimestamp", o);
49 MultiValuedTimestamp that = (MultiValuedTimestamp) o;
51 return ComparisonChain.start()
52 .compare(this.value1, that.value1)
53 .compare(this.value2, that.value2)
58 public int hashCode() {
59 return Objects.hash(value1, value2);
63 public boolean equals(Object obj) {
67 if (!(obj instanceof MultiValuedTimestamp)) {
70 MultiValuedTimestamp that = (MultiValuedTimestamp) obj;
71 return Objects.equals(this.value1, that.value1)
72 && Objects.equals(this.value2, that.value2);
76 public String toString() {
77 return toStringHelper(getClass())
78 .add("value1", value1)
79 .add("value2", value2)
84 * Returns the first value.
93 * Returns the second value.
95 * @return second value
101 // Default constructor for serialization
102 @SuppressWarnings("unused")
103 private MultiValuedTimestamp() {