// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Globalization; namespace System.Reactive { /// /// Represents value with a timestamp on it. /// The timestamp typically represents the time the value was received, using an IScheduler's clock to obtain the current time. /// /// The type of the value being timestamped. #if !NO_SERIALIZABLE [Serializable] #endif [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Timestamped", Justification = "Reviewed and agreed upon.")] public readonly struct Timestamped : IEquatable> { /// /// Constructs a timestamped value. /// /// The value to be annotated with a timestamp. /// Timestamp associated with the value. public Timestamped(T value, DateTimeOffset timestamp) { Timestamp = timestamp; Value = value; } /// /// Gets the value. /// public T Value { get; } /// /// Gets the timestamp. /// public DateTimeOffset Timestamp { get; } /// /// Determines whether the current value has the same and as a specified value. /// /// An object to compare to the current value. /// true if both values have the same and ; otherwise, false. public bool Equals(Timestamped other) { return other.Timestamp.Equals(Timestamp) && EqualityComparer.Default.Equals(Value, other.Value); } /// /// Determines whether the two specified values have the same and . /// /// The first value to compare. /// The second value to compare. /// true if the first value has the same and as the second value; otherwise, false. public static bool operator ==(Timestamped first, Timestamped second) { return first.Equals(second); } /// /// Determines whether the two specified values don't have the same and . /// /// The first value to compare. /// The second value to compare. /// true if the first value has a different or as the second value; otherwise, false. public static bool operator !=(Timestamped first, Timestamped second) { return !first.Equals(second); } /// /// Determines whether the specified System.Object is equal to the current . /// /// The System.Object to compare with the current . /// true if the specified System.Object is equal to the current ; otherwise, false. public override bool Equals(object obj) { if (!(obj is Timestamped)) { return false; } var other = (Timestamped)obj; return Equals(other); } /// /// Returns the hash code for the current value. /// /// A hash code for the current value. public override int GetHashCode() { return Timestamp.GetHashCode() ^ (Value?.GetHashCode() ?? 1979); } /// /// Returns a string representation of the current value. /// /// String representation of the current value. public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp); } } /// /// A helper class with a factory method for creating instances. /// public static class Timestamped { /// /// Creates an instance of a . This is syntactic sugar that uses type inference /// to avoid specifying a type in a constructor call, which is very useful when using anonymous types. /// /// The value to be annotated with a timestamp. /// Timestamp associated with the value. /// Creates a new timestamped value. public static Timestamped Create(T value, DateTimeOffset timestamp) { return new Timestamped(value, timestamp); } } }