1
0

FrameInterval.cs 4.6 KB

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