Subscription.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. namespace Microsoft.Reactive.Testing
  8. {
  9. /// <summary>
  10. /// Records information about subscriptions to and unsubscriptions from observable sequences.
  11. /// </summary>
  12. #if !NO_DEBUGGER_ATTRIBUTES
  13. [DebuggerDisplay("({Subscribe}, {Unsubscribe})")]
  14. #endif
  15. #if !NO_SERIALIZABLE
  16. [Serializable]
  17. #endif
  18. public struct Subscription : IEquatable<Subscription>
  19. {
  20. /// <summary>
  21. /// Infinite virtual time value, used to indicate an unsubscription never took place.
  22. /// </summary>
  23. public const long Infinite = long.MaxValue;
  24. private long _subscribe;
  25. private long _unsubscribe;
  26. /// <summary>
  27. /// Gets the subscription virtual time.
  28. /// </summary>
  29. public long Subscribe { get { return _subscribe; } }
  30. /// <summary>
  31. /// Gets the unsubscription virtual time.
  32. /// </summary>
  33. public long Unsubscribe { get { return _unsubscribe; } }
  34. /// <summary>
  35. /// Creates a new subscription object with the given virtual subscription time.
  36. /// </summary>
  37. /// <param name="subscribe">Virtual time at which the subscription occurred.</param>-
  38. public Subscription(long subscribe)
  39. {
  40. _subscribe = subscribe;
  41. _unsubscribe = Infinite;
  42. }
  43. /// <summary>
  44. /// Creates a new subscription object with the given virtual subscription and unsubscription time.
  45. /// </summary>
  46. /// <param name="subscribe">Virtual time at which the subscription occurred.</param>
  47. /// <param name="unsubscribe">Virtual time at which the unsubscription occurred.</param>
  48. public Subscription(long subscribe, long unsubscribe)
  49. {
  50. _subscribe = subscribe;
  51. _unsubscribe = unsubscribe;
  52. }
  53. /// <summary>
  54. /// Checks whether the given subscription is equal to the current instance.
  55. /// </summary>
  56. /// <param name="other">Subscription object to check for equality.</param>
  57. /// <returns>true if both objects are equal; false otherwise.</returns>
  58. public bool Equals(Subscription other)
  59. {
  60. return Subscribe == other.Subscribe && Unsubscribe == other.Unsubscribe;
  61. }
  62. /// <summary>
  63. /// Determines whether the two specified Subscription values have the same Subscribe and Unsubscribe.
  64. /// </summary>
  65. /// <param name="left">The first Subscription value to compare.</param>
  66. /// <param name="right">The second Subscription value to compare.</param>
  67. /// <returns>true if the first Subscription value has the same Subscribe and Unsubscribe as the second Subscription value; otherwise, false.</returns>
  68. public static bool operator==(Subscription left, Subscription right)
  69. {
  70. return left.Equals(right);
  71. }
  72. /// <summary>
  73. /// Determines whether the two specified Subscription values don't have the same Subscribe and Unsubscribe.
  74. /// </summary>
  75. /// <param name="left">The first Subscription value to compare.</param>
  76. /// <param name="right">The second Subscription value to compare.</param>
  77. /// <returns>true if the first Subscription value has a different Subscribe or Unsubscribe as the second Subscription value; otherwise, false.</returns>
  78. public static bool operator !=(Subscription left, Subscription right)
  79. {
  80. return !left.Equals(right);
  81. }
  82. /// <summary>
  83. /// Determines whether the specified System.Object is equal to the current Subscription value.
  84. /// </summary>
  85. /// <param name="obj">The System.Object to compare with the current Subscription value.</param>
  86. /// <returns>true if the specified System.Object is equal to the current Subscription value; otherwise, false.</returns>
  87. public override bool Equals(object obj)
  88. {
  89. if (obj is Subscription)
  90. return Equals((Subscription)obj);
  91. return false;
  92. }
  93. /// <summary>
  94. /// Returns the hash code for the current Subscription value.
  95. /// </summary>
  96. /// <returns>A hash code for the current Subscription value.</returns>
  97. public override int GetHashCode()
  98. {
  99. return Subscribe.GetHashCode() ^ Unsubscribe.GetHashCode();
  100. }
  101. /// <summary>
  102. /// Returns a string representation of the current Subscription value.
  103. /// </summary>
  104. /// <returns>String representation of the current Subscription value.</returns>
  105. public override string ToString()
  106. {
  107. if (Unsubscribe == Infinite)
  108. return string.Format(CultureInfo.CurrentCulture, "({0}, Infinite)", Subscribe);
  109. else
  110. return string.Format(CultureInfo.CurrentCulture, "({0}, {1})", Subscribe, Unsubscribe);
  111. }
  112. }
  113. }