TimeInterval.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. namespace System.Reactive
  7. {
  8. /// <summary>
  9. /// Represents a value associated with time interval information.
  10. /// 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.
  11. /// </summary>
  12. /// <typeparam name="T">The type of the value being annotated with time interval information.</typeparam>
  13. #if !NO_SERIALIZABLE
  14. [Serializable]
  15. #endif
  16. public struct TimeInterval<T> : IEquatable<TimeInterval<T>>
  17. {
  18. /// <summary>
  19. /// Constructs a time interval value.
  20. /// </summary>
  21. /// <param name="value">The value to be annotated with a time interval.</param>
  22. /// <param name="interval">Time interval associated with the value.</param>
  23. public TimeInterval(T value, TimeSpan interval)
  24. {
  25. Interval = interval;
  26. Value = value;
  27. }
  28. /// <summary>
  29. /// Gets the value.
  30. /// </summary>
  31. public T Value { get; }
  32. /// <summary>
  33. /// Gets the interval.
  34. /// </summary>
  35. public TimeSpan Interval { get; }
  36. /// <summary>
  37. /// Determines whether the current <see cref="TimeInterval{T}"/> value has the same <see cref="Value"/> and <see cref="Interval"/> as a specified <see cref="TimeInterval{T}"/> value.
  38. /// </summary>
  39. /// <param name="other">An object to compare to the current <see cref="TimeInterval{T}"/> value.</param>
  40. /// <returns><c>true</c> if both <see cref="TimeInterval{T}"/> values have the same <see cref="Value"/> and <see cref="Interval"/>; otherwise, <c>false</c>.</returns>
  41. public bool Equals(TimeInterval<T> other)
  42. {
  43. return other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value);
  44. }
  45. /// <summary>
  46. /// Determines whether the two specified <see cref="TimeInterval{T}"/> values have the same <see cref="Value"/> and <see cref="Interval"/>.
  47. /// </summary>
  48. /// <param name="first">The first <see cref="TimeInterval{T}"/> value to compare.</param>
  49. /// <param name="second">The second <see cref="TimeInterval{T}"/> value to compare.</param>
  50. /// <returns><c>true</c> if the first <see cref="TimeInterval{T}"/> value has the same <see cref="Value"/> and <see cref="Interval"/> as the second <see cref="TimeInterval{T}"/> value; otherwise, <c>false</c>.</returns>
  51. public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second)
  52. {
  53. return first.Equals(second);
  54. }
  55. /// <summary>
  56. /// Determines whether the two specified <see cref="TimeInterval{T}"/> values don't have the same <see cref="Value"/> and <see cref="Interval"/>.
  57. /// </summary>
  58. /// <param name="first">The first <see cref="TimeInterval{T}"/> value to compare.</param>
  59. /// <param name="second">The second <see cref="TimeInterval{T}"/> value to compare.</param>
  60. /// <returns><c>true</c> if the first <see cref="TimeInterval{T}"/> value has a different <see cref="Value"/> or <see cref="Interval"/> as the second <see cref="TimeInterval{T}"/> value; otherwise, <c>false</c>.</returns>
  61. public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second)
  62. {
  63. return !first.Equals(second);
  64. }
  65. /// <summary>
  66. /// Determines whether the specified System.Object is equal to the current <see cref="TimeInterval{T}"/>.
  67. /// </summary>
  68. /// <param name="obj">The System.Object to compare with the current <see cref="TimeInterval{T}"/>.</param>
  69. /// <returns><c>true</c> if the specified System.Object is equal to the current <see cref="TimeInterval{T}"/>; otherwise, <c>false</c>.</returns>
  70. public override bool Equals(object obj)
  71. {
  72. if (!(obj is TimeInterval<T>))
  73. return false;
  74. var other = (TimeInterval<T>)obj;
  75. return Equals(other);
  76. }
  77. /// <summary>
  78. /// Returns the hash code for the current <see cref="TimeInterval{T}"/> value.
  79. /// </summary>
  80. /// <returns>A hash code for the current <see cref="TimeInterval{T}"/> value.</returns>
  81. public override int GetHashCode()
  82. {
  83. return Interval.GetHashCode() ^ (Value?.GetHashCode() ?? 1963);
  84. }
  85. /// <summary>
  86. /// Returns a string representation of the current <see cref="TimeInterval{T}"/> value.
  87. /// </summary>
  88. /// <returns>String representation of the current <see cref="TimeInterval{T}"/> value.</returns>
  89. public override string ToString()
  90. {
  91. return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
  92. }
  93. }
  94. }