Recorded.cs 4.4 KB

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