Timestamped.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.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. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Timestamped", Justification = "Reviewed and agreed upon.")]
  17. public 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. return false;
  75. var other = (Timestamped<T>)obj;
  76. return Equals(other);
  77. }
  78. /// <summary>
  79. /// Returns the hash code for the current <see cref="Timestamped{T}" /> value.
  80. /// </summary>
  81. /// <returns>A hash code for the current <see cref="Timestamped{T}" /> value.</returns>
  82. public override int GetHashCode()
  83. {
  84. return Timestamp.GetHashCode() ^ (Value?.GetHashCode() ?? 1979);
  85. }
  86. /// <summary>
  87. /// Returns a string representation of the current <see cref="Timestamped{T}" /> value.
  88. /// </summary>
  89. /// <returns>String representation of the current <see cref="Timestamped{T}" /> value.</returns>
  90. public override string ToString()
  91. {
  92. return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp);
  93. }
  94. }
  95. /// <summary>
  96. /// A helper class with a factory method for creating <see cref="Timestamped{T}" /> instances.
  97. /// </summary>
  98. public static class Timestamped
  99. {
  100. /// <summary>
  101. /// Creates an instance of a <see cref="Timestamped{T}" />. This is syntactic sugar that uses type inference
  102. /// to avoid specifying a type in a constructor call, which is very useful when using anonymous types.
  103. /// </summary>
  104. /// <param name="value">The value to be annotated with a timestamp.</param>
  105. /// <param name="timestamp">Timestamp associated with the value.</param>
  106. /// <returns>Creates a new timestamped value.</returns>
  107. public static Timestamped<T> Create<T>(T value, DateTimeOffset timestamp)
  108. {
  109. return new Timestamped<T>(value, timestamp);
  110. }
  111. }
  112. }