blob: 8d2aee1b3b27deb618ac244d85c7a5cdde031bc7 [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.
Yuta HIGUCHI8ce08732014-10-11 10:40:45 -070013 *
14 * @param <T> Timestamped value type
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070015 */
16public final class Timestamped<T> {
17
18 private final Timestamp timestamp;
19 private final T value;
20
21 /**
22 * Creates a time stamped value.
23 *
24 * @param value to be timestamp
25 * @param timestamp the timestamp
26 */
27 public Timestamped(T value, Timestamp timestamp) {
28 this.value = checkNotNull(value);
29 this.timestamp = checkNotNull(timestamp);
30 }
31
32 /**
33 * Returns the value.
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070034 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070035 * @return value
36 */
37 public T value() {
38 return value;
39 }
40
41 /**
42 * Returns the time stamp.
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070043 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070044 * @return time stamp
45 */
46 public Timestamp timestamp() {
47 return timestamp;
48 }
49
50 /**
51 * Tests if this timestamped value is newer than the other.
52 *
53 * @param other timestamped value
54 * @return true if this instance is newer.
55 */
56 public boolean isNewer(Timestamped<T> other) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070057 return isNewer(checkNotNull(other).timestamp());
58 }
59
60 /**
61 * Tests if this timestamp is newer thatn the specified timestamp.
62 * @param timestamp to compare agains
63 * @return true if this instance is newer
64 */
65 public boolean isNewer(Timestamp timestamp) {
66 return this.timestamp.compareTo(checkNotNull(timestamp)) > 0;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070067 }
68
69 @Override
70 public int hashCode() {
71 return timestamp.hashCode();
72 }
73
74 @Override
75 public boolean equals(Object obj) {
76 if (this == obj) {
77 return true;
78 }
79 if (!(obj instanceof Timestamped)) {
80 return false;
81 }
82 @SuppressWarnings("unchecked")
83 Timestamped<T> that = (Timestamped<T>) obj;
84 return Objects.equals(this.timestamp, that.timestamp);
85 }
86
Yuta HIGUCHId2266a72014-10-06 14:41:54 -070087 @Override
88 public String toString() {
89 return MoreObjects.toStringHelper(getClass())
90 .add("timestamp", timestamp)
91 .add("value", value)
92 .toString();
93 }
94
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070095 // Default constructor for serialization
96 @Deprecated
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -070097 private Timestamped() {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070098 this.value = null;
99 this.timestamp = null;
100 }
101}