Timestamped.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. namespace System.Reactive
  7. {
  8. /// <summary>
  9. /// Represents value with a timestamp on it.
  10. /// The timestamp typically represents the time the value was received, using an IScheduler's clock to obtain the current time.
  11. /// </summary>
  12. /// <typeparam name="T">The type of the value being timestamped.</typeparam>
  13. #if !NO_SERIALIZABLE
  14. [Serializable]
  15. #endif
  16. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Timestamped", Justification = "Reviewed and agreed upon.")]
  17. public readonly struct Timestamped<T> : IEquatable<Timestamped<T>>
  18. {
  19. /// <summary>
  20. /// Constructs a timestamped value.
  21. /// </summary>
  22. /// <param name="value">The value to be annotated with a timestamp.</param>
  23. /// <param name="timestamp">Timestamp associated with the value.</param>
  24. public Timestamped(T value, DateTimeOffset timestamp)
  25. {
  26. Timestamp = timestamp;
  27. Value = value;
  28. }
  29. /// <summary>
  30. /// Gets the value.
  31. /// </summary>
  32. public T Value { get; }
  33. /// <summary>
  34. /// Gets the timestamp.
  35. /// </summary>
  36. public DateTimeOffset Timestamp { get; }
  37. /// <summary>
  38. /// Determines whether the current <see cref="Timestamped{T}" /> value has the same <see cref="Value"/> and <see cref="Timestamp"/> as a specified <see cref="Timestamped{T}" /> value.
  39. /// </summary>
  40. /// <param name="other">An object to compare to the current <see cref="Timestamped{T}" /> value.</param>
  41. /// <returns><c>true</c> if both <see cref="Timestamped{T}" /> values have the same <see cref="Value"/> and <see cref="Timestamp"/>; otherwise, <c>false</c>.</returns>
  42. public bool Equals(Timestamped<T> other)
  43. {
  44. return other.Timestamp.Equals(Timestamp) && EqualityComparer<T>.Default.Equals(Value, other.Value);
  45. }
  46. /// <summary>
  47. /// Determines whether the two specified <see cref="Timestamped{T}" /> values have the same <see cref="Value"/> and <see cref="Timestamp"/>.
  48. /// </summary>
  49. /// <param name="first">The first <see cref="Timestamped{T}" /> value to compare.</param>
  50. /// <param name="second">The second <see cref="Timestamped{T}" /> value to compare.</param>
  51. /// <returns><c>true</c> if the first <see cref="Timestamped{T}" /> value has the same <see cref="Value"/> and <see cref="Timestamp"/> as the second <see cref="Timestamped{T}" /> value; otherwise, <c>false</c>.</returns>
  52. public static bool operator ==(Timestamped<T> first, Timestamped<T> second)
  53. {
  54. return first.Equals(second);
  55. }
  56. /// <summary>
  57. /// Determines whether the two specified <see cref="Timestamped{T}" /> values don't have the same <see cref="Value"/> and <see cref="Timestamp"/>.
  58. /// </summary>
  59. /// <param name="first">The first <see cref="Timestamped{T}" /> value to compare.</param>
  60. /// <param name="second">The second <see cref="Timestamped{T}" /> value to compare.</param>
  61. /// <returns><c>true</c> if the first <see cref="Timestamped{T}" /> value has a different <see cref="Value"/> or <see cref="Timestamp"/> as the second <see cref="Timestamped{T}" /> value; otherwise, <c>false</c>.</returns>
  62. public static bool operator !=(Timestamped<T> first, Timestamped<T> second)
  63. {
  64. return !first.Equals(second);
  65. }
  66. /// <summary>
  67. /// Determines whether the specified System.Object is equal to the current <see cref="Timestamped{T}" />.
  68. /// </summary>
  69. /// <param name="obj">The System.Object to compare with the current <see cref="Timestamped{T}" />.</param>
  70. /// <returns><c>true</c> if the specified System.Object is equal to the current <see cref="Timestamped{T}" />; otherwise, <c>false</c>.</returns>
  71. public override bool Equals(object obj)
  72. {
  73. if (!(obj is Timestamped<T>))
  74. {
  75. return false;
  76. }
  77. var other = (Timestamped<T>)obj;
  78. return Equals(other);
  79. }
  80. /// <summary>
  81. /// Returns the hash code for the current <see cref="Timestamped{T}" /> value.
  82. /// </summary>
  83. /// <returns>A hash code for the current <see cref="Timestamped{T}" /> value.</returns>
  84. public override int GetHashCode()
  85. {
  86. return Timestamp.GetHashCode() ^ (Value?.GetHashCode() ?? 1979);
  87. }
  88. /// <summary>
  89. /// Returns a string representation of the current <see cref="Timestamped{T}" /> value.
  90. /// </summary>
  91. /// <returns>String representation of the current <see cref="Timestamped{T}" /> value.</returns>
  92. public override string ToString()
  93. {
  94. return string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp);
  95. }
  96. }
  97. /// <summary>
  98. /// A helper class with a factory method for creating <see cref="Timestamped{T}" /> instances.
  99. /// </summary>
  100. public static class Timestamped
  101. {
  102. /// <summary>
  103. /// Creates an instance of a <see cref="Timestamped{T}" />. This is syntactic sugar that uses type inference
  104. /// to avoid specifying a type in a constructor call, which is very useful when using anonymous types.
  105. /// </summary>
  106. /// <param name="value">The value to be annotated with a timestamp.</param>
  107. /// <param name="timestamp">Timestamp associated with the value.</param>
  108. /// <returns>Creates a new timestamped value.</returns>
  109. public static Timestamped<T> Create<T>(T value, DateTimeOffset timestamp)
  110. {
  111. return new Timestamped<T>(value, timestamp);
  112. }
  113. }
  114. }