MongoIdentity.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Runtime.InteropServices;
  19. using System.Security;
  20. using System.Security.Principal;
  21. using System.Text;
  22. namespace MongoDB.Driver
  23. {
  24. /// <summary>
  25. /// Represents an identity in MongoDB.
  26. /// </summary>
  27. public abstract class MongoIdentity : IEquatable<MongoIdentity>
  28. {
  29. // private fields
  30. private readonly string _source;
  31. private readonly string _username;
  32. // constructors
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="MongoIdentity" /> class.
  35. /// </summary>
  36. /// <param name="source">The source.</param>
  37. /// <param name="username">The username.</param>
  38. /// <param name="allowNullUsername">Whether to allow null usernames.</param>
  39. internal MongoIdentity(string source, string username, bool allowNullUsername = false)
  40. {
  41. if (source == null)
  42. {
  43. throw new ArgumentNullException("source");
  44. }
  45. if (username == null && !allowNullUsername)
  46. {
  47. throw new ArgumentNullException("username");
  48. }
  49. _source = source;
  50. _username = username;
  51. }
  52. // public properties
  53. /// <summary>
  54. /// Gets the source.
  55. /// </summary>
  56. public string Source
  57. {
  58. get { return _source; }
  59. }
  60. /// <summary>
  61. /// Gets the username.
  62. /// </summary>
  63. public string Username
  64. {
  65. get { return _username; }
  66. }
  67. // public operators
  68. /// <summary>
  69. /// Compares two MongoIdentity values.
  70. /// </summary>
  71. /// <param name="lhs">The first MongoIdentity.</param>
  72. /// <param name="rhs">The other MongoIdentity.</param>
  73. /// <returns>True if the two MongoIdentity values are equal (or both null).</returns>
  74. public static bool operator ==(MongoIdentity lhs, MongoIdentity rhs)
  75. {
  76. return object.Equals(lhs, rhs);
  77. }
  78. /// <summary>
  79. /// Compares two MongoIdentity values.
  80. /// </summary>
  81. /// <param name="lhs">The first MongoIdentity.</param>
  82. /// <param name="rhs">The other MongoIdentity.</param>
  83. /// <returns>True if the two MongoIdentity values are not equal (or one is null and the other is not).</returns>
  84. public static bool operator !=(MongoIdentity lhs, MongoIdentity rhs)
  85. {
  86. return !(lhs == rhs);
  87. }
  88. // public methods
  89. /// <summary>
  90. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  91. /// </summary>
  92. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  93. /// <returns>
  94. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  95. /// </returns>
  96. public override bool Equals(object obj)
  97. {
  98. return Equals(obj as MongoIdentity);
  99. }
  100. /// <summary>
  101. /// Determines whether the specified instance is equal to this instance.
  102. /// </summary>
  103. /// <param name="rhs">The right-hand side.</param>
  104. /// <returns>
  105. /// <c>true</c> if the specified instance is equal to this instance; otherwise, <c>false</c>.
  106. /// </returns>
  107. public bool Equals(MongoIdentity rhs)
  108. {
  109. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  110. return _username == rhs._username && _source == rhs._source;
  111. }
  112. /// <summary>
  113. /// Returns a hash code for this instance.
  114. /// </summary>
  115. /// <returns>
  116. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  117. /// </returns>
  118. public override int GetHashCode()
  119. {
  120. var hash = 17;
  121. hash += 37 * (_username == null ? 0 :_username.GetHashCode());
  122. hash += 37 * _source.GetHashCode();
  123. return hash;
  124. }
  125. }
  126. }