blob: 2b3f3e5067308670689d6148018d73ea18d4623d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070016package org.onlab.onos.store.impl;
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070017
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
Yuta HIGUCHIfa891c92014-10-09 15:21:40 -070022import org.onlab.onos.store.Timestamp;
23
Yuta HIGUCHId2266a72014-10-06 14:41:54 -070024import com.google.common.base.MoreObjects;
25
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070026/**
27 * Wrapper class to store Timestamped value.
Yuta HIGUCHI8ce08732014-10-11 10:40:45 -070028 *
29 * @param <T> Timestamped value type
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070030 */
31public 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 HIGUCHI9ee60f62014-10-09 10:00:01 -070049 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070050 * @return value
51 */
52 public T value() {
53 return value;
54 }
55
56 /**
57 * Returns the time stamp.
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070058 *
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -070059 * @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 HIGUCHI9ee60f62014-10-09 10:00:01 -070072 return isNewer(checkNotNull(other).timestamp());
73 }
74
75 /**
Yuta HIGUCHI85d74702014-10-16 13:54:43 -070076 * Tests if this timestamp is newer than the specified timestamp.
77 * @param other timestamp to compare against
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070078 * @return true if this instance is newer
79 */
Yuta HIGUCHI85d74702014-10-16 13:54:43 -070080 public boolean isNewer(Timestamp other) {
81 return this.timestamp.compareTo(checkNotNull(other)) > 0;
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
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700110 // Default constructor for serialization
111 @Deprecated
Yuta HIGUCHIf1f1d322014-10-07 21:09:56 -0700112 private Timestamped() {
Yuta HIGUCHI67a527f2014-10-02 22:23:54 -0700113 this.value = null;
114 this.timestamp = null;
115 }
116}