TimeInterval.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. namespace System.Reactive
  5. {
  6. /// <summary>
  7. /// Represents a value associated with time interval information.
  8. /// 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.
  9. /// </summary>
  10. /// <typeparam name="T">The type of the value being annotated with time interval information.</typeparam>
  11. #if !NO_SERIALIZABLE
  12. [Serializable]
  13. #endif
  14. public struct TimeInterval<T> : IEquatable<TimeInterval<T>>
  15. {
  16. private readonly TimeSpan _interval;
  17. private readonly T _value;
  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
  32. {
  33. get { return _value; }
  34. }
  35. /// <summary>
  36. /// Gets the interval.
  37. /// </summary>
  38. public TimeSpan Interval
  39. {
  40. get { return _interval; }
  41. }
  42. /// <summary>
  43. /// Determines whether the current TimeInterval&lt;T&gt; value has the same Value and Interval as a specified TimeInterval&lt;T&gt; value.
  44. /// </summary>
  45. /// <param name="other">An object to compare to the current TimeInterval&lt;T&gt; value.</param>
  46. /// <returns>true if both TimeInterval&lt;T&gt; values have the same Value and Interval; otherwise, false.</returns>
  47. public bool Equals(TimeInterval<T> other)
  48. {
  49. return other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value);
  50. }
  51. /// <summary>
  52. /// Determines whether the two specified TimeInterval&lt;T&gt; values have the same Value and Interval.
  53. /// </summary>
  54. /// <param name="first">The first TimeInterval&lt;T&gt; value to compare.</param>
  55. /// <param name="second">The second TimeInterval&lt;T&gt; value to compare.</param>
  56. /// <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>
  57. public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second)
  58. {
  59. return first.Equals(second);
  60. }
  61. /// <summary>
  62. /// Determines whether the two specified TimeInterval&lt;T&gt; values don't have the same Value and Interval.
  63. /// </summary>
  64. /// <param name="first">The first TimeInterval&lt;T&gt; value to compare.</param>
  65. /// <param name="second">The second TimeInterval&lt;T&gt; value to compare.</param>
  66. /// <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>
  67. public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second)
  68. {
  69. return !first.Equals(second);
  70. }
  71. /// <summary>
  72. /// Determines whether the specified System.Object is equal to the current TimeInterval&lt;T&gt;.
  73. /// </summary>
  74. /// <param name="obj">The System.Object to compare with the current TimeInterval&lt;T&gt;.</param>
  75. /// <returns>true if the specified System.Object is equal to the current TimeInterval&lt;T&gt;; otherwise, false.</returns>
  76. public override bool Equals(object obj)
  77. {
  78. if (!(obj is TimeInterval<T>))
  79. return false;
  80. var other = (TimeInterval<T>)obj;
  81. return this.Equals(other);
  82. }
  83. /// <summary>
  84. /// Returns the hash code for the current TimeInterval&lt;T&gt; value.
  85. /// </summary>
  86. /// <returns>A hash code for the current TimeInterval&lt;T&gt; value.</returns>
  87. public override int GetHashCode()
  88. {
  89. var valueHashCode = Value == null ? 1963 : Value.GetHashCode();
  90. return Interval.GetHashCode() ^ valueHashCode;
  91. }
  92. /// <summary>
  93. /// Returns a string representation of the current TimeInterval&lt;T&gt; value.
  94. /// </summary>
  95. /// <returns>String representation of the current TimeInterval&lt;T&gt; value.</returns>
  96. public override string ToString()
  97. {
  98. return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
  99. }
  100. }
  101. }