Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 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 | */ |
Jonathan Hart | 63939a3 | 2015-05-08 11:57:03 -0700 | [diff] [blame] | 16 | package org.onosproject.store.service; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ComparisonChain; |
| 20 | import org.onosproject.store.Timestamp; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 28 | * A logical timestamp that derives its value from two input values. The first |
| 29 | * value always takes precedence over the second value when comparing timestamps. |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 30 | */ |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 31 | public class MultiValuedTimestamp<T extends Comparable<T>, U extends Comparable<U>> |
| 32 | implements Timestamp { |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 33 | |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 34 | private final T value1; |
| 35 | private final U value2; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Creates a new timestamp based on two values. The first value has higher |
| 39 | * precedence than the second when comparing timestamps. |
| 40 | * |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 41 | * @param value1 first value |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 42 | * @param value2 second value |
| 43 | */ |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 44 | public MultiValuedTimestamp(T value1, U value2) { |
| 45 | this.value1 = checkNotNull(value1); |
| 46 | this.value2 = checkNotNull(value2); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public int compareTo(Timestamp o) { |
| 51 | checkArgument(o instanceof MultiValuedTimestamp, |
| 52 | "Must be MultiValuedTimestamp", o); |
| 53 | MultiValuedTimestamp that = (MultiValuedTimestamp) o; |
| 54 | |
| 55 | return ComparisonChain.start() |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 56 | .compare(this.value1, that.value1) |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 57 | .compare(this.value2, that.value2) |
| 58 | .result(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public int hashCode() { |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 63 | return Objects.hash(value1, value2); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean equals(Object obj) { |
| 68 | if (this == obj) { |
| 69 | return true; |
| 70 | } |
| 71 | if (!(obj instanceof MultiValuedTimestamp)) { |
| 72 | return false; |
| 73 | } |
| 74 | MultiValuedTimestamp that = (MultiValuedTimestamp) obj; |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 75 | return Objects.equals(this.value1, that.value1) && |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 76 | Objects.equals(this.value2, that.value2); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| 81 | return MoreObjects.toStringHelper(getClass()) |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 82 | .add("value1", value1) |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 83 | .add("value2", value2) |
| 84 | .toString(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the first value. |
| 89 | * |
| 90 | * @return first value |
| 91 | */ |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 92 | public T value1() { |
| 93 | return value1; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the second value. |
| 98 | * |
| 99 | * @return second value |
| 100 | */ |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 101 | public U value2() { |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 102 | return value2; |
| 103 | } |
| 104 | |
| 105 | // Default constructor for serialization |
| 106 | @SuppressWarnings("unused") |
| 107 | private MultiValuedTimestamp() { |
Jonathan Hart | 2f66936 | 2015-02-11 16:19:20 -0800 | [diff] [blame] | 108 | this.value1 = null; |
| 109 | this.value2 = null; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 110 | } |
| 111 | } |