BsonElement.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.Bson
  17. {
  18. /// <summary>
  19. /// Represents a BSON element.
  20. /// </summary>
  21. [Serializable]
  22. public struct BsonElement : IComparable<BsonElement>, IEquatable<BsonElement>
  23. {
  24. // private fields
  25. private readonly string _name;
  26. private readonly BsonValue _value;
  27. // constructors
  28. // NOTE: for every public BsonElement constructor there is a matching constructor, Add and Set method in BsonDocument
  29. /// <summary>
  30. /// Initializes a new instance of the BsonElement class.
  31. /// </summary>
  32. /// <param name="name">The name of the element.</param>
  33. /// <param name="value">The value of the element.</param>
  34. public BsonElement(string name, BsonValue value)
  35. {
  36. if (name == null)
  37. {
  38. throw new ArgumentNullException("name");
  39. }
  40. if (value == null)
  41. {
  42. throw new ArgumentNullException("value");
  43. }
  44. ValidateElementName(name);
  45. _name = name;
  46. _value = value;
  47. }
  48. // public properties
  49. /// <summary>
  50. /// Gets the name of the element.
  51. /// </summary>
  52. public string Name
  53. {
  54. get { return _name; }
  55. }
  56. /// <summary>
  57. /// Gets or sets the value of the element.
  58. /// </summary>
  59. public BsonValue Value
  60. {
  61. get { return _value; }
  62. }
  63. // public operators
  64. /// <summary>
  65. /// Compares two BsonElements.
  66. /// </summary>
  67. /// <param name="lhs">The first BsonElement.</param>
  68. /// <param name="rhs">The other BsonElement.</param>
  69. /// <returns>True if the two BsonElements are equal (or both null).</returns>
  70. public static bool operator ==(BsonElement lhs, BsonElement rhs)
  71. {
  72. return Equals(lhs, rhs);
  73. }
  74. /// <summary>
  75. /// Compares two BsonElements.
  76. /// </summary>
  77. /// <param name="lhs">The first BsonElement.</param>
  78. /// <param name="rhs">The other BsonElement.</param>
  79. /// <returns>True if the two BsonElements are not equal (or one is null and the other is not).</returns>
  80. public static bool operator !=(BsonElement lhs, BsonElement rhs)
  81. {
  82. return !(lhs == rhs);
  83. }
  84. // private static methods
  85. private static void ValidateElementName(string name)
  86. {
  87. if (name.IndexOf('\0') >= 0)
  88. {
  89. throw new ArgumentException("Element name cannot contain null (0x00) characters");
  90. }
  91. }
  92. // public methods
  93. /// <summary>
  94. /// Creates a shallow clone of the element (see also DeepClone).
  95. /// </summary>
  96. /// <returns>A shallow clone of the element.</returns>
  97. public BsonElement Clone()
  98. {
  99. return new BsonElement(_name, _value);
  100. }
  101. /// <summary>
  102. /// Creates a deep clone of the element (see also Clone).
  103. /// </summary>
  104. /// <returns>A deep clone of the element.</returns>
  105. public BsonElement DeepClone()
  106. {
  107. return new BsonElement(_name, _value.DeepClone());
  108. }
  109. /// <summary>
  110. /// Compares this BsonElement to another BsonElement.
  111. /// </summary>
  112. /// <param name="other">The other BsonElement.</param>
  113. /// <returns>A 32-bit signed integer that indicates whether this BsonElement is less than, equal to, or greather than the other.</returns>
  114. public int CompareTo(BsonElement other)
  115. {
  116. int r = _name.CompareTo(other._name);
  117. if (r != 0) { return r; }
  118. return _value.CompareTo(other._value);
  119. }
  120. /// <summary>
  121. /// Compares this BsonElement to another BsonElement.
  122. /// </summary>
  123. /// <param name="rhs">The other BsonElement.</param>
  124. /// <returns>True if the two BsonElement values are equal.</returns>
  125. public bool Equals(BsonElement rhs)
  126. {
  127. return _name == rhs._name && _value == rhs._value;
  128. }
  129. /// <summary>
  130. /// Compares this BsonElement to another object.
  131. /// </summary>
  132. /// <param name="obj">The other object.</param>
  133. /// <returns>True if the other object is a BsonElement and equal to this one.</returns>
  134. public override bool Equals(object obj)
  135. {
  136. if (obj == null || obj.GetType() != typeof(BsonElement)) { return false; }
  137. return Equals((BsonElement)obj);
  138. }
  139. /// <summary>
  140. /// Gets the hash code.
  141. /// </summary>
  142. /// <returns>The hash code.</returns>
  143. public override int GetHashCode()
  144. {
  145. // see Effective Java by Joshua Bloch
  146. int hash = 17;
  147. hash = 37 * hash + _name.GetHashCode();
  148. hash = 37 * hash + _value.GetHashCode();
  149. return hash;
  150. }
  151. /// <summary>
  152. /// Returns a string representation of the value.
  153. /// </summary>
  154. /// <returns>A string representation of the value.</returns>
  155. public override string ToString()
  156. {
  157. return string.Format("{0}={1}", _name, _value);
  158. }
  159. }
  160. }