Recorded.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. namespace Microsoft.Reactive.Testing
  7. {
  8. /// <summary>
  9. /// Record of a value including the virtual time it was produced on.
  10. /// </summary>
  11. /// <typeparam name="T">Type of the value.</typeparam>
  12. #if !NO_DEBUGGER_ATTRIBUTES
  13. [DebuggerDisplay("{Value}@{Time}")]
  14. #endif
  15. #if !NO_SERIALIZABLE
  16. [Serializable]
  17. #endif
  18. public struct Recorded<T> : IEquatable<Recorded<T>>
  19. {
  20. private readonly long _time;
  21. private readonly T _value;
  22. /// <summary>
  23. /// Gets the virtual time the value was produced on.
  24. /// </summary>
  25. public long Time { get { return _time; } }
  26. /// <summary>
  27. /// Gets the recorded value.
  28. /// </summary>
  29. public T Value { get { return _value; } }
  30. /// <summary>
  31. /// Creates a new object recording the production of the specified value at the given virtual time.
  32. /// </summary>
  33. /// <param name="time">Virtual time the value was produced on.</param>
  34. /// <param name="value">Value that was produced.</param>
  35. public Recorded(long time, T value)
  36. {
  37. _time = time;
  38. _value = value;
  39. }
  40. /// <summary>
  41. /// Checks whether the given recorded object is equal to the current instance.
  42. /// </summary>
  43. /// <param name="other">Recorded object to check for equality.</param>
  44. /// <returns>true if both objects are equal; false otherwise.</returns>
  45. public bool Equals(Recorded<T> other)
  46. {
  47. return Time == other.Time && EqualityComparer<T>.Default.Equals(Value, other.Value);
  48. }
  49. /// <summary>
  50. /// Determines whether the two specified Recorded&lt;T&gt; values have the same Time and Value.
  51. /// </summary>
  52. /// <param name="left">The first Recorded&lt;T&gt; value to compare.</param>
  53. /// <param name="right">The second Recorded&lt;T&gt; value to compare.</param>
  54. /// <returns>true if the first Recorded&lt;T&gt; value has the same Time and Value as the second Recorded&lt;T&gt; value; otherwise, false.</returns>
  55. public static bool operator ==(Recorded<T> left, Recorded<T> right)
  56. {
  57. return left.Equals(right);
  58. }
  59. /// <summary>
  60. /// Determines whether the two specified Recorded&lt;T&gt; values don't have the same Time and Value.
  61. /// </summary>
  62. /// <param name="left">The first Recorded&lt;T&gt; value to compare.</param>
  63. /// <param name="right">The second Recorded&lt;T&gt; value to compare.</param>
  64. /// <returns>true if the first Recorded&lt;T&gt; value has a different Time or Value as the second Recorded&lt;T&gt; value; otherwise, false.</returns>
  65. public static bool operator !=(Recorded<T> left, Recorded<T> right)
  66. {
  67. return !left.Equals(right);
  68. }
  69. /// <summary>
  70. /// Determines whether the specified System.Object is equal to the current Recorded&lt;T&gt; value.
  71. /// </summary>
  72. /// <param name="obj">The System.Object to compare with the current Recorded&lt;T&gt; value.</param>
  73. /// <returns>true if the specified System.Object is equal to the current Recorded&lt;T&gt; value; otherwise, false.</returns>
  74. public override bool Equals(object obj)
  75. {
  76. if (obj is Recorded<T>)
  77. return Equals((Recorded<T>)obj);
  78. return false;
  79. }
  80. /// <summary>
  81. /// Returns the hash code for the current Recorded&lt;T&gt; value.
  82. /// </summary>
  83. /// <returns>A hash code for the current Recorded&lt;T&gt; value.</returns>
  84. public override int GetHashCode()
  85. {
  86. return Time.GetHashCode() + EqualityComparer<T>.Default.GetHashCode(Value);
  87. }
  88. /// <summary>
  89. /// Returns a string representation of the current Recorded&lt;T&gt; value.
  90. /// </summary>
  91. /// <returns>String representation of the current Recorded&lt;T&gt; value.</returns>
  92. public override string ToString()
  93. {
  94. return Value.ToString() + "@" + Time.ToString(CultureInfo.CurrentCulture);
  95. }
  96. }
  97. }