blob: 77b0a875036c36c847e23b08b6cae22b125462f3 [file] [log] [blame]
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -07001package org.onlab.onos.store.common.impl;
2
3import static com.google.common.base.Preconditions.checkNotNull;
4
5import java.util.Objects;
6
7import 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.
33 * @return value
34 */
35 public T value() {
36 return value;
37 }
38
39 /**
40 * Returns the time stamp.
41 * @return time stamp
42 */
43 public Timestamp timestamp() {
44 return timestamp;
45 }
46
47 /**
48 * Tests if this timestamped value is newer than the other.
49 *
50 * @param other timestamped value
51 * @return true if this instance is newer.
52 */
53 public boolean isNewer(Timestamped<T> other) {
54 return this.timestamp.compareTo(checkNotNull(other).timestamp()) > 0;
55 }
56
57 @Override
58 public int hashCode() {
59 return timestamp.hashCode();
60 }
61
62 @Override
63 public boolean equals(Object obj) {
64 if (this == obj) {
65 return true;
66 }
67 if (!(obj instanceof Timestamped)) {
68 return false;
69 }
70 @SuppressWarnings("unchecked")
71 Timestamped<T> that = (Timestamped<T>) obj;
72 return Objects.equals(this.timestamp, that.timestamp);
73 }
74
Yuta HIGUCHId2266a72014-10-06 14:41:54 -070075 @Override
76 public String toString() {
77 return MoreObjects.toStringHelper(getClass())
78 .add("timestamp", timestamp)
79 .add("value", value)
80 .toString();
81 }
82
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070083 // Default constructor for serialization
84 @Deprecated
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -070085 private Timestamped() {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070086 this.value = null;
87 this.timestamp = null;
88 }
89}