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