| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
using System.Diagnostics; |
| 5 |
using System.Globalization; |
| 6 |
|
| 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 |
|
| 25 |
private long _subscribe; |
| 26 |
private long _unsubscribe; |
| 27 |
|
| 28 |
/// <summary> |
| 29 |
/// Gets the subscription virtual time. |
| 30 |
/// </summary> |
| 31 |
public long Subscribe { get { return _subscribe; } } |
| 32 |
|
| 33 |
/// <summary> |
| 34 |
/// Gets the unsubscription virtual time. |
| 35 |
/// </summary> |
| 36 |
public long Unsubscribe { get { return _unsubscribe; } } |
| 37 |
|
| 38 |
/// <summary> |
| 39 |
/// Creates a new subscription object with the given virtual subscription time. |
| 40 |
/// </summary> |
| 41 |
/// <param name="subscribe">Virtual time at which the subscription occurred.</param>- |
| 42 |
public Subscription(long subscribe) |
| 43 |
{ |
| 44 |
_subscribe = subscribe; |
| 45 |
_unsubscribe = Infinite; |
| 46 |
} |
| 47 |
|
| 48 |
/// <summary> |
| 49 |
/// Creates a new subscription object with the given virtual subscription and unsubscription time. |
| 50 |
/// </summary> |
| 51 |
/// <param name="subscribe">Virtual time at which the subscription occurred.</param> |
| 52 |
/// <param name="unsubscribe">Virtual time at which the unsubscription occurred.</param> |
| 53 |
public Subscription(long subscribe, long unsubscribe) |
| 54 |
{ |
| 55 |
_subscribe = subscribe; |
| 56 |
_unsubscribe = unsubscribe; |
| 57 |
} |
| 58 |
|
| 59 |
/// <summary> |
| 60 |
/// Checks whether the given subscription is equal to the current instance. |
| 61 |
/// </summary> |
| 62 |
/// <param name="other">Subscription object to check for equality.</param> |
| 63 |
/// <returns>true if both objects are equal; false otherwise.</returns> |
| 64 |
public bool Equals(Subscription other) |
| 65 |
{ |
| 66 |
return Subscribe == other.Subscribe && Unsubscribe == other.Unsubscribe; |
| 67 |
} |
| 68 |
|
| 69 |
/// <summary> |
| 70 |
/// Determines whether the two specified Subscription values have the same Subscribe and Unsubscribe. |
| 71 |
/// </summary> |
| 72 |
/// <param name="left">The first Subscription value to compare.</param> |
| 73 |
/// <param name="right">The second Subscription value to compare.</param> |
| 74 |
/// <returns>true if the first Subscription value has the same Subscribe and Unsubscribe as the second Subscription value; otherwise, false.</returns> |
| 75 |
public static bool operator==(Subscription left, Subscription right) |
| 76 |
{ |
| 77 |
return left.Equals(right); |
| 78 |
} |
| 79 |
|
| 80 |
/// <summary> |
| 81 |
/// Determines whether the two specified Subscription values don't have the same Subscribe and Unsubscribe. |
| 82 |
/// </summary> |
| 83 |
/// <param name="left">The first Subscription value to compare.</param> |
| 84 |
/// <param name="right">The second Subscription value to compare.</param> |
| 85 |
/// <returns>true if the first Subscription value has a different Subscribe or Unsubscribe as the second Subscription value; otherwise, false.</returns> |
| 86 |
public static bool operator !=(Subscription left, Subscription right) |
| 87 |
{ |
| 88 |
return !left.Equals(right); |
| 89 |
} |
| 90 |
|
| 91 |
/// <summary> |
| 92 |
/// Determines whether the specified System.Object is equal to the current Subscription value. |
| 93 |
/// </summary> |
| 94 |
/// <param name="obj">The System.Object to compare with the current Subscription value.</param> |
| 95 |
/// <returns>true if the specified System.Object is equal to the current Subscription value; otherwise, false.</returns> |
| 96 |
public override bool Equals(object obj) |
| 97 |
{ |
| 98 |
if (obj is Subscription) |
| 99 |
return Equals((Subscription)obj); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
/// <summary> |
| 104 |
/// Returns the hash code for the current Subscription value. |
| 105 |
/// </summary> |
| 106 |
/// <returns>A hash code for the current Subscription value.</returns> |
| 107 |
public override int GetHashCode() |
| 108 |
{ |
| 109 |
return Subscribe.GetHashCode() ^ Unsubscribe.GetHashCode(); |
| 110 |
} |
| 111 |
|
| 112 |
/// <summary> |
| 113 |
/// Returns a string representation of the current Subscription value. |
| 114 |
/// </summary> |
| 115 |
/// <returns>String representation of the current Subscription value.</returns> |
| 116 |
public override string ToString() |
| 117 |
{ |
| 118 |
if (Unsubscribe == Infinite) |
| 119 |
return string.Format(CultureInfo.CurrentCulture, "({0}, Infinite)", Subscribe); |
| 120 |
else |
| 121 |
return string.Format(CultureInfo.CurrentCulture, "({0}, {1})", Subscribe, Unsubscribe); |
| 122 |
} |
| 123 |
} |
| 124 |
} |