BsonElement.cs 5.8 KB

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