OptionalSourceBreakLocationEqualityComparer.cs 1.3 KB

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