Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.impl; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.store.Timestamp; |
Yuta HIGUCHI | fa891c9 | 2014-10-09 15:21:40 -0700 | [diff] [blame] | 23 | |
Yuta HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 24 | import com.google.common.base.MoreObjects; |
| 25 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Wrapper class to store Timestamped value. |
Yuta HIGUCHI | 8ce0873 | 2014-10-11 10:40:45 -0700 | [diff] [blame] | 28 | * |
| 29 | * @param <T> Timestamped value type |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 30 | */ |
| 31 | public final class Timestamped<T> { |
| 32 | |
| 33 | private final Timestamp timestamp; |
| 34 | private final T value; |
| 35 | |
| 36 | /** |
| 37 | * Creates a time stamped value. |
| 38 | * |
| 39 | * @param value to be timestamp |
| 40 | * @param timestamp the timestamp |
| 41 | */ |
| 42 | public Timestamped(T value, Timestamp timestamp) { |
| 43 | this.value = checkNotNull(value); |
| 44 | this.timestamp = checkNotNull(timestamp); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the value. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 49 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 50 | * @return value |
| 51 | */ |
| 52 | public T value() { |
| 53 | return value; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the time stamp. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 58 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 59 | * @return time stamp |
| 60 | */ |
| 61 | public Timestamp timestamp() { |
| 62 | return timestamp; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Tests if this timestamped value is newer than the other. |
| 67 | * |
| 68 | * @param other timestamped value |
| 69 | * @return true if this instance is newer. |
| 70 | */ |
| 71 | public boolean isNewer(Timestamped<T> other) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 72 | return isNewer(checkNotNull(other).timestamp()); |
| 73 | } |
| 74 | |
| 75 | /** |
Yuta HIGUCHI | 85d7470 | 2014-10-16 13:54:43 -0700 | [diff] [blame] | 76 | * Tests if this timestamp is newer than the specified timestamp. |
| 77 | * @param other timestamp to compare against |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 78 | * @return true if this instance is newer |
| 79 | */ |
Brian O'Connor | 2952e3b | 2015-02-19 21:47:57 -0800 | [diff] [blame] | 80 | //TODO put this in Timestamp |
Yuta HIGUCHI | 85d7470 | 2014-10-16 13:54:43 -0700 | [diff] [blame] | 81 | public boolean isNewer(Timestamp other) { |
| 82 | return this.timestamp.compareTo(checkNotNull(other)) > 0; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public int hashCode() { |
| 87 | return timestamp.hashCode(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean equals(Object obj) { |
| 92 | if (this == obj) { |
| 93 | return true; |
| 94 | } |
| 95 | if (!(obj instanceof Timestamped)) { |
| 96 | return false; |
| 97 | } |
| 98 | @SuppressWarnings("unchecked") |
| 99 | Timestamped<T> that = (Timestamped<T>) obj; |
| 100 | return Objects.equals(this.timestamp, that.timestamp); |
| 101 | } |
| 102 | |
Yuta HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public String toString() { |
| 105 | return MoreObjects.toStringHelper(getClass()) |
| 106 | .add("timestamp", timestamp) |
| 107 | .add("value", value) |
| 108 | .toString(); |
| 109 | } |
| 110 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 111 | // Default constructor for serialization |
| 112 | @Deprecated |
Yuta HIGUCHI | f1f1d32 | 2014-10-07 21:09:56 -0700 | [diff] [blame] | 113 | private Timestamped() { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 114 | this.value = null; |
| 115 | this.timestamp = null; |
| 116 | } |
| 117 | } |