BsonElement.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Copyright 2010-2014 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 class BsonElement : IComparable<BsonElement>, IEquatable<BsonElement>
  23. {
  24. // private fields
  25. private string _name;
  26. private BsonValue _value;
  27. // constructors
  28. // NOTE: for every public BsonElement constructor there is a matching constructor, Add and Set method in BsonDocument
  29. // used when cloning an existing element, caller will set name and value
  30. private BsonElement()
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the BsonElement class.
  35. /// </summary>
  36. /// <param name="name">The name of the element.</param>
  37. /// <param name="value">The value of the element.</param>
  38. public BsonElement(string name, BsonValue value)
  39. {
  40. if (name == null)
  41. {
  42. throw new ArgumentNullException("name");
  43. }
  44. if (value == null)
  45. {
  46. throw new ArgumentNullException("value");
  47. }
  48. ValidateElementName(name);
  49. _name = name;
  50. _value = value;
  51. }
  52. // public properties
  53. /// <summary>
  54. /// Gets the name of the element.
  55. /// </summary>
  56. public string Name
  57. {
  58. get { return _name; }
  59. }
  60. /// <summary>
  61. /// Gets or sets the value of the element.
  62. /// </summary>
  63. public BsonValue Value
  64. {
  65. get { return _value; }
  66. set
  67. {
  68. if (value == null)
  69. {
  70. throw new ArgumentNullException("value");
  71. }
  72. _value = value;
  73. }
  74. }
  75. // public operators
  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 equal (or both null).</returns>
  82. public static bool operator ==(BsonElement lhs, BsonElement rhs)
  83. {
  84. return object.Equals(lhs, rhs);
  85. }
  86. /// <summary>
  87. /// Compares two BsonElements.
  88. /// </summary>
  89. /// <param name="lhs">The first BsonElement.</param>
  90. /// <param name="rhs">The other BsonElement.</param>
  91. /// <returns>True if the two BsonElements are not equal (or one is null and the other is not).</returns>
  92. public static bool operator !=(BsonElement lhs, BsonElement rhs)
  93. {
  94. return !(lhs == rhs);
  95. }
  96. // public static methods
  97. /// <summary>
  98. /// Creates a new instance of the BsonElement class.
  99. /// </summary>
  100. /// <param name="condition">Whether to create the BsonElement or return null.</param>
  101. /// <param name="name">The name of the element.</param>
  102. /// <param name="value">The value of the element.</param>
  103. /// <returns>A BsonElement or null.</returns>
  104. [Obsolete("Use new BsonElement(string name, BsonValue value) instead.")]
  105. public static BsonElement Create(bool condition, string name, BsonValue value)
  106. {
  107. if (name == null)
  108. {
  109. throw new ArgumentNullException("name");
  110. }
  111. if (condition && value != null)
  112. {
  113. return new BsonElement(name, value);
  114. }
  115. else
  116. {
  117. return null;
  118. }
  119. }
  120. /// <summary>
  121. /// Creates a new instance of the BsonElement class.
  122. /// </summary>
  123. /// <param name="name">The name of the element.</param>
  124. /// <param name="value">The value of the element.</param>
  125. /// <returns>A BsonElement or null.</returns>
  126. [Obsolete("Use new BsonElement(string name, BsonValue value) instead.")]
  127. public static BsonElement Create(string name, BsonValue value)
  128. {
  129. if (name == null)
  130. {
  131. throw new ArgumentNullException("name");
  132. }
  133. if (value != null)
  134. {
  135. return new BsonElement(name, value);
  136. }
  137. else
  138. {
  139. return null;
  140. }
  141. }
  142. // private static methods
  143. private static void ValidateElementName(string name)
  144. {
  145. if (name.IndexOf('\0') >= 0)
  146. {
  147. throw new ArgumentException("Element name cannot contain null (0x00) characters");
  148. }
  149. }
  150. // public methods
  151. /// <summary>
  152. /// Creates a shallow clone of the element (see also DeepClone).
  153. /// </summary>
  154. /// <returns>A shallow clone of the element.</returns>
  155. public BsonElement Clone()
  156. {
  157. var clone = new BsonElement();
  158. clone._name = _name;
  159. clone._value = _value;
  160. return clone;
  161. }
  162. /// <summary>
  163. /// Creates a deep clone of the element (see also Clone).
  164. /// </summary>
  165. /// <returns>A deep clone of the element.</returns>
  166. public BsonElement DeepClone()
  167. {
  168. var clone = new BsonElement();
  169. clone._name = _name;
  170. clone._value = _value.DeepClone();
  171. return clone;
  172. }
  173. /// <summary>
  174. /// Compares this BsonElement to another BsonElement.
  175. /// </summary>
  176. /// <param name="other">The other BsonElement.</param>
  177. /// <returns>A 32-bit signed integer that indicates whether this BsonElement is less than, equal to, or greather than the other.</returns>
  178. public int CompareTo(BsonElement other)
  179. {
  180. if (other == null) { return 1; }
  181. int r = _name.CompareTo(other._name);
  182. if (r != 0) { return r; }
  183. return _value.CompareTo(other._value);
  184. }
  185. /// <summary>
  186. /// Compares this BsonElement to another BsonElement.
  187. /// </summary>
  188. /// <param name="rhs">The other BsonElement.</param>
  189. /// <returns>True if the two BsonElement values are equal.</returns>
  190. public bool Equals(BsonElement rhs)
  191. {
  192. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  193. return _name == rhs._name && _value == rhs._value;
  194. }
  195. /// <summary>
  196. /// Compares this BsonElement to another object.
  197. /// </summary>
  198. /// <param name="obj">The other object.</param>
  199. /// <returns>True if the other object is a BsonElement and equal to this one.</returns>
  200. public override bool Equals(object obj)
  201. {
  202. return Equals(obj as BsonElement); // works even if obj is null or of a different type
  203. }
  204. /// <summary>
  205. /// Gets the hash code.
  206. /// </summary>
  207. /// <returns>The hash code.</returns>
  208. public override int GetHashCode()
  209. {
  210. // see Effective Java by Joshua Bloch
  211. int hash = 17;
  212. hash = 37 * hash + _name.GetHashCode();
  213. hash = 37 * hash + _value.GetHashCode();
  214. return hash;
  215. }
  216. /// <summary>
  217. /// Returns a string representation of the value.
  218. /// </summary>
  219. /// <returns>A string representation of the value.</returns>
  220. public override string ToString()
  221. {
  222. return string.Format("{0}={1}", _name, _value);
  223. }
  224. }
  225. }