// 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 a value associated with time interval information. /// The time interval can represent the time it took to produce the value, the interval relative to a previous value, the value's delivery time relative to a base, etc. /// /// The type of the value being annotated with time interval information. [Serializable] public readonly struct TimeInterval : IEquatable> { /// /// Constructs a time interval value. /// /// The value to be annotated with a time interval. /// Time interval associated with the value. public TimeInterval(T value, TimeSpan interval) { Interval = interval; Value = value; } /// /// Gets the value. /// public T Value { get; } /// /// Gets the interval. /// public TimeSpan Interval { get; } /// /// Deconstructs the time interval value into a value and a time interval. /// /// The value. /// Time interval associated with the value. public void Deconstruct(out T value, out TimeSpan interval) => (value, interval) = (Value, Interval); /// /// 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(TimeInterval other) => other.Interval.Equals(Interval) && 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 ==(TimeInterval first, TimeInterval second) => 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 !=(TimeInterval first, TimeInterval second) => !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) => obj is TimeInterval interval && Equals(interval); /// /// Returns the hash code for the current value. /// /// A hash code for the current value. public override int GetHashCode() { // TODO: Use proper hash code combiner. return Interval.GetHashCode() ^ (Value?.GetHashCode() ?? 1963); } /// /// Returns a string representation of the current value. /// /// String representation of the current value. public override string ToString() => string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval); } }