OptionalSourceBreakLocationEqualityComparer.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Jint.Runtime.Debugger;
  5. /// <summary>
  6. /// Equality comparer for BreakLocation matching null Source to any other Source.
  7. /// </summary>
  8. /// <remarks>
  9. /// Equals returns true if all properties are equal - or if Source is null on either BreakLocation.
  10. /// GetHashCode excludes Source.
  11. /// </remarks>
  12. internal sealed class OptionalSourceBreakLocationEqualityComparer : IEqualityComparer<BreakLocation>
  13. {
  14. public bool Equals(BreakLocation? x, BreakLocation? y)
  15. {
  16. if (Object.ReferenceEquals(x, y))
  17. {
  18. return true;
  19. }
  20. if (x is null || y is null)
  21. {
  22. return false;
  23. }
  24. return
  25. x.Line == y.Line &&
  26. x.Column == y.Column &&
  27. (x.Source == null || y.Source == null || x.Source == y.Source);
  28. }
  29. public int GetHashCode(BreakLocation? obj)
  30. {
  31. if (obj == null)
  32. {
  33. return 0;
  34. }
  35. // Keeping this rather than HashCode.Combine, which isn't in net461 or netstandard2.0
  36. unchecked
  37. {
  38. int hash = 17;
  39. hash = hash * 33 + obj.Line.GetHashCode();
  40. hash = hash * 33 + obj.Column.GetHashCode();
  41. // Don't include Source
  42. return hash;
  43. }
  44. }
  45. }