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