blob: b752ad7ef83821fde763809d24fc77cb18b36f0f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.impl;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070017
Jonathan Hart403ea932015-02-20 16:23:00 -080018import com.google.common.base.MoreObjects;
19import org.onosproject.store.Timestamp;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070020
21import java.util.Objects;
22
Jonathan Hart403ea932015-02-20 16:23:00 -080023import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHId2266a72014-10-06 14:41:54 -070024
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070025/**
26 * Wrapper class to store Timestamped value.
Yuta HIGUCHI8ce08732014-10-11 10:40:45 -070027 *
28 * @param <T> Timestamped value type
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070029 */
30public 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 HIGUCHI9ee60f62014-10-09 10:00:01 -070048 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070049 * @return value
50 */
51 public T value() {
52 return value;
53 }
54
55 /**
56 * Returns the time stamp.
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070057 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070058 * @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 Hart403ea932015-02-20 16:23:00 -080071 return isNewerThan(checkNotNull(other).timestamp());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070072 }
73
74 /**
Yuta HIGUCHI85d74702014-10-16 13:54:43 -070075 * Tests if this timestamp is newer than the specified timestamp.
Jonathan Hart403ea932015-02-20 16:23:00 -080076 *
Yuta HIGUCHI85d74702014-10-16 13:54:43 -070077 * @param other timestamp to compare against
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070078 * @return true if this instance is newer
79 */
Jonathan Hart403ea932015-02-20 16:23:00 -080080 public boolean isNewerThan(Timestamp other) {
81 return timestamp.isNewerThan(other);
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070082 }
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 HIGUCHId2266a72014-10-06 14:41:54 -0700102 @Override
103 public String toString() {
104 return MoreObjects.toStringHelper(getClass())
105 .add("timestamp", timestamp)
106 .add("value", value)
107 .toString();
108 }
109
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700110 /**
Ray Milkeycef96062016-02-17 14:47:40 -0800111 * Constructs an empty object. Required for serialization.
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700112 */
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700113 private Timestamped() {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700114 this.value = null;
115 this.timestamp = null;
116 }
117}