blob: baf8facb2955f84ef89bf2b898119cf2cd0b4d23 [file] [log] [blame]
Jonathan Hart5ec32ba2015-02-05 13:33:58 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart5ec32ba2015-02-05 13:33:58 -08003 *
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 Hart63939a32015-05-08 11:57:03 -070016package org.onosproject.store.service;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080017
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ComparisonChain;
20import org.onosproject.store.Timestamp;
21
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkArgument;
Jonathan Hart2f669362015-02-11 16:19:20 -080025import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080026
27/**
Jonathan Hart2f669362015-02-11 16:19:20 -080028 * 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 Hart5ec32ba2015-02-05 13:33:58 -080030 */
Jonathan Hart2f669362015-02-11 16:19:20 -080031public class MultiValuedTimestamp<T extends Comparable<T>, U extends Comparable<U>>
32 implements Timestamp {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080033
Jonathan Hart2f669362015-02-11 16:19:20 -080034 private final T value1;
35 private final U value2;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080036
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 Hart2f669362015-02-11 16:19:20 -080041 * @param value1 first value
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080042 * @param value2 second value
43 */
Jonathan Hart2f669362015-02-11 16:19:20 -080044 public MultiValuedTimestamp(T value1, U value2) {
45 this.value1 = checkNotNull(value1);
46 this.value2 = checkNotNull(value2);
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080047 }
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 Hart2f669362015-02-11 16:19:20 -080056 .compare(this.value1, that.value1)
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080057 .compare(this.value2, that.value2)
58 .result();
59 }
60
61 @Override
62 public int hashCode() {
Jonathan Hart2f669362015-02-11 16:19:20 -080063 return Objects.hash(value1, value2);
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080064 }
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 Hart2f669362015-02-11 16:19:20 -080075 return Objects.equals(this.value1, that.value1) &&
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080076 Objects.equals(this.value2, that.value2);
77 }
78
79 @Override
80 public String toString() {
81 return MoreObjects.toStringHelper(getClass())
Jonathan Hart2f669362015-02-11 16:19:20 -080082 .add("value1", value1)
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080083 .add("value2", value2)
84 .toString();
85 }
86
87 /**
88 * Returns the first value.
89 *
90 * @return first value
91 */
Jonathan Hart2f669362015-02-11 16:19:20 -080092 public T value1() {
93 return value1;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080094 }
95
96 /**
97 * Returns the second value.
98 *
99 * @return second value
100 */
Jonathan Hart2f669362015-02-11 16:19:20 -0800101 public U value2() {
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800102 return value2;
103 }
104
105 // Default constructor for serialization
106 @SuppressWarnings("unused")
107 private MultiValuedTimestamp() {
Jonathan Hart2f669362015-02-11 16:19:20 -0800108 this.value1 = null;
109 this.value2 = null;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800110 }
111}