/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.util; import android.annotation.NonNull; import android.annotation.Nullable; import android.os.Parcel; import android.os.SystemClock; import java.util.Objects; /** * A value with an associated reference time. The reference time will typically be provided by the * elapsed realtime clock. The elapsed realtime clock can be obtained using methods like * {@link SystemClock#elapsedRealtime()} or {@link SystemClock#elapsedRealtimeClock()}. * If a suitable clock is used the reference time can be used to identify the age of a value or * ordering between values. * *
To read and write a timestamped value from / to a Parcel see
* {@link #readFromParcel(Parcel, ClassLoader, Class)} and
* {@link #writeToParcel(Parcel, TimestampedValue)}.
*
* @param The marshalling/unmarshalling of the value relies upon {@link Parcel#writeValue(Object)}
* and {@link Parcel#readValue(ClassLoader)} and so this method can only be used with types
* supported by those methods.
*
* @param in the Parcel to read from
* @param classLoader the ClassLoader to pass to {@link Parcel#readValue(ClassLoader)}
* @param valueClass the expected type of the value, typically the same as {@code The marshalling/unmarshalling of the value relies upon {@link Parcel#writeValue(Object)}
* and {@link Parcel#readValue(ClassLoader)} and so this method can only be used with types
* supported by those methods.
*
* @param dest the Parcel
* @param timestampedValue the value
* @throws RuntimeException if the value could not be written to the Parcel
*/
public static void writeToParcel(
@NonNull Parcel dest, @NonNull TimestampedValue> timestampedValue) {
dest.writeLong(timestampedValue.mReferenceTimeMillis);
dest.writeValue(timestampedValue.mValue);
}
/**
* Returns the difference in milliseconds between two instance's reference times.
*/
public static long referenceTimeDifference(
@NonNull TimestampedValue> one, @NonNull TimestampedValue> two) {
return one.mReferenceTimeMillis - two.mReferenceTimeMillis;
}
}