2
0

TimeInterval.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 a value associated with time interval information.
  11. /// The time interval can represent the time it took to produce the value, the interval relative to a previous value, the value's delivery time relative to a base, etc.
  12. /// </summary>
  13. /// <typeparam name="T">The type of the value being annotated with time interval information.</typeparam>
  14. [Serializable]
  15. public struct TimeInterval<T> : IEquatable<TimeInterval<T>>
  16. {
  17. private readonly TimeSpan _interval;
  18. private readonly T _value;
  19. /// <summary>
  20. /// Constructs a time interval value.
  21. /// </summary>
  22. /// <param name="value">The value to be annotated with a time interval.</param>
  23. /// <param name="interval">Time interval associated with the value.</param>
  24. public TimeInterval(T value, TimeSpan interval)
  25. {
  26. _interval = interval;
  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 interval.
  38. /// </summary>
  39. public TimeSpan Interval
  40. {
  41. get { return _interval; }
  42. }
  43. /// <summary>
  44. /// Determines whether the current TimeInterval&lt;T&gt; value has the same Value and Interval as a specified TimeInterval&lt;T&gt; value.
  45. /// </summary>
  46. /// <param name="other">An object to compare to the current TimeInterval&lt;T&gt; value.</param>
  47. /// <returns>true if both TimeInterval&lt;T&gt; values have the same Value and Interval; otherwise, false.</returns>
  48. public bool Equals(TimeInterval<T> other)
  49. {
  50. return other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value);
  51. }
  52. /// <summary>
  53. /// Determines whether the two specified TimeInterval&lt;T&gt; values have the same Value and Interval.
  54. /// </summary>
  55. /// <param name="first">The first TimeInterval&lt;T&gt; value to compare.</param>
  56. /// <param name="second">The second TimeInterval&lt;T&gt; value to compare.</param>
  57. /// <returns>true if the first TimeInterval&lt;T&gt; value has the same Value and Interval as the second TimeInterval&lt;T&gt; value; otherwise, false.</returns>
  58. public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second)
  59. {
  60. return first.Equals(second);
  61. }
  62. /// <summary>
  63. /// Determines whether the two specified TimeInterval&lt;T&gt; values don't have the same Value and Interval.
  64. /// </summary>
  65. /// <param name="first">The first TimeInterval&lt;T&gt; value to compare.</param>
  66. /// <param name="second">The second TimeInterval&lt;T&gt; value to compare.</param>
  67. /// <returns>true if the first TimeInterval&lt;T&gt; value has a different Value or Interval as the second TimeInterval&lt;T&gt; value; otherwise, false.</returns>
  68. public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second)
  69. {
  70. return !first.Equals(second);
  71. }
  72. /// <summary>
  73. /// Determines whether the specified System.Object is equal to the current TimeInterval&lt;T&gt;.
  74. /// </summary>
  75. /// <param name="obj">The System.Object to compare with the current TimeInterval&lt;T&gt;.</param>
  76. /// <returns>true if the specified System.Object is equal to the current TimeInterval&lt;T&gt;; otherwise, false.</returns>
  77. public override bool Equals(object obj)
  78. {
  79. if (!(obj is TimeInterval<T>))
  80. return false;
  81. var other = (TimeInterval<T>)obj;
  82. return this.Equals(other);
  83. }
  84. /// <summary>
  85. /// Returns the hash code for the current TimeInterval&lt;T&gt; value.
  86. /// </summary>
  87. /// <returns>A hash code for the current TimeInterval&lt;T&gt; value.</returns>
  88. public override int GetHashCode()
  89. {
  90. var valueHashCode = Value == null ? 1963 : Value.GetHashCode();
  91. return Interval.GetHashCode() ^ valueHashCode;
  92. }
  93. /// <summary>
  94. /// Returns a string representation of the current TimeInterval&lt;T&gt; value.
  95. /// </summary>
  96. /// <returns>String representation of the current TimeInterval&lt;T&gt; value.</returns>
  97. public override string ToString()
  98. {
  99. return String.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
  100. }
  101. }
  102. }