blob: 45056730b08bf857dfef423e1c02c113bf3b5393 [file] [log] [blame]
Neil Fuller4773b9d2018-06-08 18:44:49 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
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 */
16
17package android.util;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.os.Parcel;
Neil Fullerdef77b52019-10-10 09:56:15 +010022import android.os.Parcelable;
Neil Fuller4773b9d2018-06-08 18:44:49 +010023import android.os.SystemClock;
24
25import java.util.Objects;
26
27/**
28 * A value with an associated reference time. The reference time will typically be provided by the
29 * elapsed realtime clock. The elapsed realtime clock can be obtained using methods like
30 * {@link SystemClock#elapsedRealtime()} or {@link SystemClock#elapsedRealtimeClock()}.
31 * If a suitable clock is used the reference time can be used to identify the age of a value or
32 * ordering between values.
33 *
Neil Fullerdef77b52019-10-10 09:56:15 +010034 * <p>This class implements {@link Parcelable} for convenience but instances will only actually be
35 * parcelable if the value type held is {@code null}, {@link Parcelable}, or one of the other types
36 * supported by {@link Parcel#writeValue(Object)} / {@link Parcel#readValue(ClassLoader)}.
Neil Fuller4773b9d2018-06-08 18:44:49 +010037 *
38 * @param <T> the type of the value with an associated timestamp
39 * @hide
40 */
Neil Fullerdef77b52019-10-10 09:56:15 +010041public final class TimestampedValue<T> implements Parcelable {
Neil Fuller4773b9d2018-06-08 18:44:49 +010042 private final long mReferenceTimeMillis;
43 private final T mValue;
44
45 public TimestampedValue(long referenceTimeMillis, T value) {
46 mReferenceTimeMillis = referenceTimeMillis;
47 mValue = value;
48 }
49
50 public long getReferenceTimeMillis() {
51 return mReferenceTimeMillis;
52 }
53
54 public T getValue() {
55 return mValue;
56 }
57
58 @Override
59 public boolean equals(Object o) {
60 if (this == o) {
61 return true;
62 }
63 if (o == null || getClass() != o.getClass()) {
64 return false;
65 }
66 TimestampedValue<?> that = (TimestampedValue<?>) o;
67 return mReferenceTimeMillis == that.mReferenceTimeMillis
68 && Objects.equals(mValue, that.mValue);
69 }
70
71 @Override
72 public int hashCode() {
73 return Objects.hash(mReferenceTimeMillis, mValue);
74 }
75
Neil Fullerca97b342018-06-13 15:23:32 +010076 @Override
77 public String toString() {
78 return "TimestampedValue{"
79 + "mReferenceTimeMillis=" + mReferenceTimeMillis
80 + ", mValue=" + mValue
81 + '}';
82 }
83
Neil Fuller4773b9d2018-06-08 18:44:49 +010084 /**
Neil Fuller4980bbc2018-06-12 21:06:20 +010085 * Returns the difference in milliseconds between two instance's reference times.
86 */
87 public static long referenceTimeDifference(
88 @NonNull TimestampedValue<?> one, @NonNull TimestampedValue<?> two) {
89 return one.mReferenceTimeMillis - two.mReferenceTimeMillis;
90 }
Neil Fullerdef77b52019-10-10 09:56:15 +010091
92 public static final @NonNull Parcelable.Creator<TimestampedValue<?>> CREATOR =
93 new Parcelable.ClassLoaderCreator<TimestampedValue<?>>() {
94
95 @Override
96 public TimestampedValue<?> createFromParcel(@NonNull Parcel source) {
97 return createFromParcel(source, null);
98 }
99
100 @Override
101 public TimestampedValue<?> createFromParcel(
102 @NonNull Parcel source, @Nullable ClassLoader classLoader) {
103 long referenceTimeMillis = source.readLong();
104 Object value = source.readValue(classLoader);
105 return new TimestampedValue<>(referenceTimeMillis, value);
106 }
107
108 @Override
109 public TimestampedValue[] newArray(int size) {
110 return new TimestampedValue[size];
111 }
112 };
113
114 @Override
115 public int describeContents() {
116 return 0;
117 }
118
119 @Override
120 public void writeToParcel(@NonNull Parcel dest, int flags) {
121 dest.writeLong(mReferenceTimeMillis);
122 dest.writeValue(mValue);
123 }
Neil Fuller4773b9d2018-06-08 18:44:49 +0100124}