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. |
Yuta HIGUCHI | 8ce0873 | 2014-10-11 10:40:45 -0700 | [diff] [blame^] | 13 | * |
| 14 | * @param <T> Timestamped value type |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 15 | */ |
| 16 | public 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 HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 34 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 35 | * @return value |
| 36 | */ |
| 37 | public T value() { |
| 38 | return value; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the time stamp. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 43 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 44 | * @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 HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 57 | 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 HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 67 | } |
| 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 HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public String toString() { |
| 89 | return MoreObjects.toStringHelper(getClass()) |
| 90 | .add("timestamp", timestamp) |
| 91 | .add("value", value) |
| 92 | .toString(); |
| 93 | } |
| 94 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 95 | // Default constructor for serialization |
| 96 | @Deprecated |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 97 | private Timestamped() { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 98 | this.value = null; |
| 99 | this.timestamp = null; |
| 100 | } |
| 101 | } |