1
0

TimeInterval.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. private readonly TimeSpan _interval;
  19. private readonly T _value;
  20. /// <summary>
  21. /// Constructs a time interval value.
  22. /// </summary>
  23. /// <param name="value">The value to be annotated with a time interval.</param>
  24. /// <param name="interval">Time interval associated with the value.</param>
  25. public TimeInterval(T value, TimeSpan interval)
  26. {
  27. _interval = interval;
  28. _value = value;
  29. }
  30. /// <summary>
  31. /// Gets the value.
  32. /// </summary>
  33. public T Value
  34. {
  35. get { return _value; }
  36. }
  37. /// <summary>
  38. /// Gets the interval.
  39. /// </summary>
  40. public TimeSpan Interval
  41. {
  42. get { return _interval; }
  43. }
  44. /// <summary>
  45. /// Determines whether the current TimeInterval&lt;T&gt; value has the same Value and Interval as a specified TimeInterval&lt;T&gt; value.
  46. /// </summary>
  47. /// <param name="other">An object to compare to the current TimeInterval&lt;T&gt; value.</param>
  48. /// <returns>true if both TimeInterval&lt;T&gt; values have the same Value and Interval; otherwise, false.</returns>
  49. public bool Equals(TimeInterval<T> other)
  50. {
  51. return other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value);
  52. }
  53. /// <summary>
  54. /// Determines whether the two specified TimeInterval&lt;T&gt; values have the same Value and Interval.
  55. /// </summary>
  56. /// <param name="first">The first TimeInterval&lt;T&gt; value to compare.</param>
  57. /// <param name="second">The second TimeInterval&lt;T&gt; value to compare.</param>
  58. /// <returns>true if the first TimeInterval&lt;T&gt; value has the same Value and Interval as the second TimeInterval&lt;T&gt; value; otherwise, false.</returns>
  59. public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second)
  60. {
  61. return first.Equals(second);
  62. }
  63. /// <summary>
  64. /// Determines whether the two specified TimeInterval&lt;T&gt; values don't have the same Value and Interval.
  65. /// </summary>
  66. /// <param name="first">The first TimeInterval&lt;T&gt; value to compare.</param>
  67. /// <param name="second">The second TimeInterval&lt;T&gt; value to compare.</param>
  68. /// <returns>true if the first TimeInterval&lt;T&gt; value has a different Value or Interval as the second TimeInterval&lt;T&gt; value; otherwise, false.</returns>
  69. public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second)
  70. {
  71. return !first.Equals(second);
  72. }
  73. /// <summary>
  74. /// Determines whether the specified System.Object is equal to the current TimeInterval&lt;T&gt;.
  75. /// </summary>
  76. /// <param name="obj">The System.Object to compare with the current TimeInterval&lt;T&gt;.</param>
  77. /// <returns>true if the specified System.Object is equal to the current TimeInterval&lt;T&gt;; otherwise, false.</returns>
  78. public override bool Equals(object obj)
  79. {
  80. if (!(obj is TimeInterval<T>))
  81. return false;
  82. var other = (TimeInterval<T>)obj;
  83. return this.Equals(other);
  84. }
  85. /// <summary>
  86. /// Returns the hash code for the current TimeInterval&lt;T&gt; value.
  87. /// </summary>
  88. /// <returns>A hash code for the current TimeInterval&lt;T&gt; value.</returns>
  89. public override int GetHashCode()
  90. {
  91. var valueHashCode = Value == null ? 1963 : Value.GetHashCode();
  92. return Interval.GetHashCode() ^ valueHashCode;
  93. }
  94. /// <summary>
  95. /// Returns a string representation of the current TimeInterval&lt;T&gt; value.
  96. /// </summary>
  97. /// <returns>String representation of the current TimeInterval&lt;T&gt; value.</returns>
  98. public override string ToString()
  99. {
  100. return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
  101. }
  102. }
  103. }