CanonicalEquatableClass.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Shared
  17. {
  18. internal class CanonicalEquatableClass : IEquatable<CanonicalEquatableClass>
  19. {
  20. // private fields
  21. private int _x;
  22. private int _y;
  23. // constructors
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="CanonicalEquatableClass"/> class.
  26. /// </summary>
  27. /// <param name="x">The x.</param>
  28. /// <param name="y">The y.</param>
  29. public CanonicalEquatableClass(int x, int y)
  30. {
  31. _x = y;
  32. _y = y;
  33. }
  34. // public operators
  35. /// <summary>
  36. /// Determines whether two <see cref="CanonicalEquatableClass"/> instances are equal.
  37. /// </summary>
  38. /// <param name="lhs">The LHS.</param>
  39. /// <param name="rhs">The RHS.</param>
  40. /// <returns>
  41. /// <c>true</c> if the left hand side is equal to the right hand side; otherwise, <c>false</c>.
  42. /// </returns>
  43. public static bool operator ==(CanonicalEquatableClass lhs, CanonicalEquatableClass rhs)
  44. {
  45. return object.Equals(lhs, rhs); // handles lhs == null correctly
  46. }
  47. /// <summary>
  48. /// Determines whether two <see cref="CanonicalEquatableClass"/> instances are not equal.
  49. /// </summary>
  50. /// <param name="lhs">The LHS.</param>
  51. /// <param name="rhs">The RHS.</param>
  52. /// <returns>
  53. /// <c>true</c> if the left hand side is not equal to the right hand side; otherwise, <c>false</c>.
  54. /// </returns>
  55. public static bool operator !=(CanonicalEquatableClass lhs, CanonicalEquatableClass rhs)
  56. {
  57. return !(lhs == rhs);
  58. }
  59. // public methods
  60. /// <summary>
  61. /// Determines whether the specified <see cref="CanonicalEquatableClass" /> is equal to this instance.
  62. /// </summary>
  63. /// <param name="obj">The <see cref="CanonicalEquatableClass" /> to compare with this instance.</param>
  64. /// <returns>
  65. /// <c>true</c> if the specified <see cref="CanonicalEquatableClass" /> is equal to this instance; otherwise, <c>false</c>.
  66. /// </returns>
  67. public bool Equals(CanonicalEquatableClass obj)
  68. {
  69. return Equals((object)obj); // handles obj == null correctly
  70. }
  71. /// <summary>
  72. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  73. /// </summary>
  74. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  75. /// <returns>
  76. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  77. /// </returns>
  78. public override bool Equals(object obj)
  79. {
  80. // actual work done here (in virtual Equals) to handle inheritance
  81. // use ReferenceEquals consistently because sometimes using == can lead to recursion loops
  82. // make sure to use GetType instead of typeof in case derived classes are involved
  83. if (object.ReferenceEquals(obj, null) || GetType() != obj.GetType()) { return false; }
  84. var rhs = (CanonicalEquatableClass)obj;
  85. return // be sure x and y implement ==, otherwise use Equals
  86. _x == rhs._x &&
  87. _y == rhs._y;
  88. }
  89. /// <summary>
  90. /// Returns a hash code for this instance.
  91. /// </summary>
  92. /// <returns>
  93. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  94. /// </returns>
  95. public override int GetHashCode()
  96. {
  97. return new Hasher()
  98. .Hash(_x)
  99. .Hash(_y)
  100. .GetHashCode();
  101. }
  102. }
  103. }