Yuta HIGUCHI | fa891c9 | 2014-10-09 15:21:40 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.common.impl; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 2 | |
| 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
| 5 | import java.util.Objects; |
| 6 | |
Yuta HIGUCHI | fa891c9 | 2014-10-09 15:21:40 -0700 | [diff] [blame] | 7 | import org.onlab.onos.store.Timestamp; |
| 8 | |
Yuta HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 9 | import com.google.common.base.MoreObjects; |
| 10 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Wrapper class to store Timestamped value. |
| 13 | * @param <T> |
| 14 | */ |
| 15 | public 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 HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 33 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 34 | * @return value |
| 35 | */ |
| 36 | public T value() { |
| 37 | return value; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns the time stamp. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 42 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 43 | * @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 HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 56 | 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 HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 66 | } |
| 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 HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public String toString() { |
| 88 | return MoreObjects.toStringHelper(getClass()) |
| 89 | .add("timestamp", timestamp) |
| 90 | .add("value", value) |
| 91 | .toString(); |
| 92 | } |
| 93 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 94 | // Default constructor for serialization |
| 95 | @Deprecated |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 96 | private Timestamped() { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 97 | this.value = null; |
| 98 | this.timestamp = null; |
| 99 | } |
| 100 | } |