Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onosproject.store.Timestamp; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 20 | |
| 21 | import java.util.Objects; |
| 22 | |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 24 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Wrapper class to store Timestamped value. |
Yuta HIGUCHI | 8ce0873 | 2014-10-11 10:40:45 -0700 | [diff] [blame] | 27 | * |
| 28 | * @param <T> Timestamped value type |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 29 | */ |
| 30 | public final class Timestamped<T> { |
| 31 | |
| 32 | private final Timestamp timestamp; |
| 33 | private final T value; |
| 34 | |
| 35 | /** |
| 36 | * Creates a time stamped value. |
| 37 | * |
| 38 | * @param value to be timestamp |
| 39 | * @param timestamp the timestamp |
| 40 | */ |
| 41 | public Timestamped(T value, Timestamp timestamp) { |
| 42 | this.value = checkNotNull(value); |
| 43 | this.timestamp = checkNotNull(timestamp); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the value. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 48 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 49 | * @return value |
| 50 | */ |
| 51 | public T value() { |
| 52 | return value; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the time stamp. |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 57 | * |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 58 | * @return time stamp |
| 59 | */ |
| 60 | public Timestamp timestamp() { |
| 61 | return timestamp; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Tests if this timestamped value is newer than the other. |
| 66 | * |
| 67 | * @param other timestamped value |
| 68 | * @return true if this instance is newer. |
| 69 | */ |
| 70 | public boolean isNewer(Timestamped<T> other) { |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 71 | return isNewerThan(checkNotNull(other).timestamp()); |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Yuta HIGUCHI | 85d7470 | 2014-10-16 13:54:43 -0700 | [diff] [blame] | 75 | * Tests if this timestamp is newer than the specified timestamp. |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 76 | * |
Yuta HIGUCHI | 85d7470 | 2014-10-16 13:54:43 -0700 | [diff] [blame] | 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 | */ |
Jonathan Hart | 403ea93 | 2015-02-20 16:23:00 -0800 | [diff] [blame] | 80 | public boolean isNewerThan(Timestamp other) { |
| 81 | return timestamp.isNewerThan(other); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return timestamp.hashCode(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (this == obj) { |
| 92 | return true; |
| 93 | } |
| 94 | if (!(obj instanceof Timestamped)) { |
| 95 | return false; |
| 96 | } |
| 97 | @SuppressWarnings("unchecked") |
| 98 | Timestamped<T> that = (Timestamped<T>) obj; |
| 99 | return Objects.equals(this.timestamp, that.timestamp); |
| 100 | } |
| 101 | |
Yuta HIGUCHI | d2266a7 | 2014-10-06 14:41:54 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public String toString() { |
| 104 | return MoreObjects.toStringHelper(getClass()) |
| 105 | .add("timestamp", timestamp) |
| 106 | .add("value", value) |
| 107 | .toString(); |
| 108 | } |
| 109 | |
Sho SHIMIZU | be63b23 | 2015-06-30 10:57:58 -0700 | [diff] [blame] | 110 | /** |
Ray Milkey | cef9606 | 2016-02-17 14:47:40 -0800 | [diff] [blame] | 111 | * Constructs an empty object. Required for serialization. |
Sho SHIMIZU | be63b23 | 2015-06-30 10:57:58 -0700 | [diff] [blame] | 112 | */ |
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 | } |