OptionalSourceBreakLocationEqualityComparer.cs 1.3 KB

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