Timestamped.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // original code from rx.codeplex.com
  2. /* ------------------ */
  3. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. namespace UniRx
  8. {
  9. /// <summary>
  10. /// Represents value with a timestamp on it.
  11. /// The timestamp typically represents the time the value was received, using an IScheduler's clock to obtain the current time.
  12. /// </summary>
  13. /// <typeparam name="T">The type of the value being timestamped.</typeparam>
  14. [Serializable]
  15. public struct Timestamped<T> : IEquatable<Timestamped<T>>
  16. {
  17. private readonly DateTimeOffset _timestamp;
  18. private readonly T _value;
  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
  33. {
  34. get { return _value; }
  35. }
  36. /// <summary>
  37. /// Gets the timestamp.
  38. /// </summary>
  39. public DateTimeOffset Timestamp
  40. {
  41. get { return _timestamp; }
  42. }
  43. /// <summary>
  44. /// Determines whether the current Timestamped&lt;T&gt; value has the same Value and Timestamp as a specified Timestamped&lt;T&gt; value.
  45. /// </summary>
  46. /// <param name="other">An object to compare to the current Timestamped&lt;T&gt; value.</param>
  47. /// <returns>true if both Timestamped&lt;T&gt; values have the same Value and Timestamp; otherwise, false.</returns>
  48. public bool Equals(Timestamped<T> other)
  49. {
  50. return other.Timestamp.Equals(Timestamp) && EqualityComparer<T>.Default.Equals(Value, other.Value);
  51. }
  52. /// <summary>
  53. /// Determines whether the two specified Timestamped&lt;T&gt; values have the same Value and Timestamp.
  54. /// </summary>
  55. /// <param name="first">The first Timestamped&lt;T&gt; value to compare.</param>
  56. /// <param name="second">The second Timestamped&lt;T&gt; value to compare.</param>
  57. /// <returns>true if the first Timestamped&lt;T&gt; value has the same Value and Timestamp as the second Timestamped&lt;T&gt; value; otherwise, false.</returns>
  58. public static bool operator ==(Timestamped<T> first, Timestamped<T> second)
  59. {
  60. return first.Equals(second);
  61. }
  62. /// <summary>
  63. /// Determines whether the two specified Timestamped&lt;T&gt; values don't have the same Value and Timestamp.
  64. /// </summary>
  65. /// <param name="first">The first Timestamped&lt;T&gt; value to compare.</param>
  66. /// <param name="second">The second Timestamped&lt;T&gt; value to compare.</param>
  67. /// <returns>true if the first Timestamped&lt;T&gt; value has a different Value or Timestamp as the second Timestamped&lt;T&gt; value; otherwise, false.</returns>
  68. public static bool operator !=(Timestamped<T> first, Timestamped<T> second)
  69. {
  70. return !first.Equals(second);
  71. }
  72. /// <summary>
  73. /// Determines whether the specified System.Object is equal to the current Timestamped&lt;T&gt;.
  74. /// </summary>
  75. /// <param name="obj">The System.Object to compare with the current Timestamped&lt;T&gt;.</param>
  76. /// <returns>true if the specified System.Object is equal to the current Timestamped&lt;T&gt;; otherwise, false.</returns>
  77. public override bool Equals(object obj)
  78. {
  79. if (!(obj is Timestamped<T>))
  80. return false;
  81. var other = (Timestamped<T>)obj;
  82. return this.Equals(other);
  83. }
  84. /// <summary>
  85. /// Returns the hash code for the current Timestamped&lt;T&gt; value.
  86. /// </summary>
  87. /// <returns>A hash code for the current Timestamped&lt;T&gt; value.</returns>
  88. public override int GetHashCode()
  89. {
  90. var valueHashCode = Value == null ? 1979 : Value.GetHashCode();
  91. return _timestamp.GetHashCode() ^ valueHashCode;
  92. }
  93. /// <summary>
  94. /// Returns a string representation of the current Timestamped&lt;T&gt; value.
  95. /// </summary>
  96. /// <returns>String representation of the current Timestamped&lt;T&gt; value.</returns>
  97. public override string ToString()
  98. {
  99. return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp);
  100. }
  101. }
  102. /// <summary>
  103. /// A helper class with a factory method for creating Timestamped&lt;T&gt; instances.
  104. /// </summary>
  105. public static class Timestamped
  106. {
  107. /// <summary>
  108. /// Creates an instance of a Timestamped&lt;T&gt;. This is syntactic sugar that uses type inference
  109. /// to avoid specifying a type in a constructor call, which is very useful when using anonymous types.
  110. /// </summary>
  111. /// <param name="value">The value to be annotated with a timestamp.</param>
  112. /// <param name="timestamp">Timestamp associated with the value.</param>
  113. /// <returns>Creates a new timestamped value.</returns>
  114. public static Timestamped<T> Create<T>(T value, DateTimeOffset timestamp)
  115. {
  116. return new Timestamped<T>(value, timestamp);
  117. }
  118. }
  119. }