// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 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. #if !NO_SERIALIZABLE [Serializable] #endif public 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; } /// /// 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) { return 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) { 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 !=(TimeInterval first, TimeInterval 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 TimeInterval)) return false; var other = (TimeInterval)obj; return Equals(other); } /// /// Returns the hash code for the current value. /// /// A hash code for the current value. public override int GetHashCode() { return Interval.GetHashCode() ^ (Value?.GetHashCode() ?? 1963); } /// /// 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, Interval); } } }