Subscription.cs 5.0 KB

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