TimingEvent.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. namespace BestHTTP.Timings
  3. {
  4. public struct TimingEvent : IEquatable<TimingEvent>
  5. {
  6. public static readonly TimingEvent Empty = new TimingEvent(null, TimeSpan.Zero);
  7. /// <summary>
  8. /// Name of the event
  9. /// </summary>
  10. public readonly string Name;
  11. /// <summary>
  12. /// Duration of the event.
  13. /// </summary>
  14. public readonly TimeSpan Duration;
  15. /// <summary>
  16. /// When the event occurred.
  17. /// </summary>
  18. public readonly DateTime When;
  19. public TimingEvent(string name, TimeSpan duration)
  20. {
  21. this.Name = name;
  22. this.Duration = duration;
  23. this.When = DateTime.Now;
  24. }
  25. public TimingEvent(string name, DateTime when, TimeSpan duration)
  26. {
  27. this.Name = name;
  28. this.When = when;
  29. this.Duration = duration;
  30. }
  31. public TimeSpan CalculateDuration(TimingEvent @event)
  32. {
  33. if (this.When < @event.When)
  34. return @event.When - this.When;
  35. return this.When - @event.When;
  36. }
  37. public bool Equals(TimingEvent other)
  38. {
  39. return this.Name == other.Name &&
  40. this.Duration == other.Duration &&
  41. this.When == other.When;
  42. }
  43. public override bool Equals(object obj)
  44. {
  45. if (obj is TimingEvent)
  46. return this.Equals((TimingEvent)obj);
  47. return false;
  48. }
  49. public override int GetHashCode()
  50. {
  51. return (this.Name != null ? this.Name.GetHashCode() : 0) ^
  52. this.Duration.GetHashCode() ^
  53. this.When.GetHashCode();
  54. }
  55. public static bool operator ==(TimingEvent lhs, TimingEvent rhs)
  56. {
  57. return lhs.Equals(rhs);
  58. }
  59. public static bool operator !=(TimingEvent lhs, TimingEvent rhs)
  60. {
  61. return !lhs.Equals(rhs);
  62. }
  63. public override string ToString()
  64. {
  65. return string.Format("['{0}': {1}]", this.Name, this.Duration);
  66. }
  67. }
  68. }