blob: e803e74714be694f34c3e663a745575f04379907 [file] [log] [blame]
Yuta HIGUCHIfa891c92014-10-09 15:21:40 -07001package org.onlab.onos.store.common.impl;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -07002
3import static com.google.common.base.Preconditions.checkNotNull;
4
5import java.util.Objects;
6
Yuta HIGUCHIfa891c92014-10-09 15:21:40 -07007import org.onlab.onos.store.Timestamp;
8
Yuta HIGUCHId2266a72014-10-06 14:41:54 -07009import com.google.common.base.MoreObjects;
10
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070011/**
12 * Wrapper class to store Timestamped value.
13 * @param <T>
14 */
15public final class Timestamped<T> {
16
17 private final Timestamp timestamp;
18 private final T value;
19
20 /**
21 * Creates a time stamped value.
22 *
23 * @param value to be timestamp
24 * @param timestamp the timestamp
25 */
26 public Timestamped(T value, Timestamp timestamp) {
27 this.value = checkNotNull(value);
28 this.timestamp = checkNotNull(timestamp);
29 }
30
31 /**
32 * Returns the value.
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070033 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070034 * @return value
35 */
36 public T value() {
37 return value;
38 }
39
40 /**
41 * Returns the time stamp.
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070042 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070043 * @return time stamp
44 */
45 public Timestamp timestamp() {
46 return timestamp;
47 }
48
49 /**
50 * Tests if this timestamped value is newer than the other.
51 *
52 * @param other timestamped value
53 * @return true if this instance is newer.
54 */
55 public boolean isNewer(Timestamped<T> other) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070056 return isNewer(checkNotNull(other).timestamp());
57 }
58
59 /**
60 * Tests if this timestamp is newer thatn the specified timestamp.
61 * @param timestamp to compare agains
62 * @return true if this instance is newer
63 */
64 public boolean isNewer(Timestamp timestamp) {
65 return this.timestamp.compareTo(checkNotNull(timestamp)) > 0;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070066 }
67
68 @Override
69 public int hashCode() {
70 return timestamp.hashCode();
71 }
72
73 @Override
74 public boolean equals(Object obj) {
75 if (this == obj) {
76 return true;
77 }
78 if (!(obj instanceof Timestamped)) {
79 return false;
80 }
81 @SuppressWarnings("unchecked")
82 Timestamped<T> that = (Timestamped<T>) obj;
83 return Objects.equals(this.timestamp, that.timestamp);
84 }
85
Yuta HIGUCHId2266a72014-10-06 14:41:54 -070086 @Override
87 public String toString() {
88 return MoreObjects.toStringHelper(getClass())
89 .add("timestamp", timestamp)
90 .add("value", value)
91 .toString();
92 }
93
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070094 // Default constructor for serialization
95 @Deprecated
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -070096 private Timestamped() {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070097 this.value = null;
98 this.timestamp = null;
99 }
100}