BsonJavaScriptWithScope.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 JavaScript value with a scope.
  20. /// </summary>
  21. [Serializable]
  22. public class BsonJavaScriptWithScope : BsonJavaScript, IComparable<BsonJavaScriptWithScope>, IEquatable<BsonJavaScriptWithScope>
  23. {
  24. // private fields
  25. private BsonDocument _scope;
  26. // constructors
  27. /// <summary>
  28. /// Initializes a new instance of the BsonJavaScriptWithScope class.
  29. /// </summary>
  30. /// <param name="code">The JavaScript code.</param>
  31. /// <param name="scope">A scope (a set of variables with values).</param>
  32. public BsonJavaScriptWithScope(string code, BsonDocument scope)
  33. : base(code, BsonType.JavaScriptWithScope)
  34. {
  35. if (scope == null)
  36. {
  37. throw new ArgumentNullException("scope");
  38. }
  39. _scope = scope;
  40. }
  41. // public operators
  42. /// <summary>
  43. /// Compares two BsonJavaScriptWithScope values.
  44. /// </summary>
  45. /// <param name="lhs">The first BsonJavaScriptWithScope.</param>
  46. /// <param name="rhs">The other BsonJavaScriptWithScope.</param>
  47. /// <returns>True if the two BsonJavaScriptWithScope values are not equal according to ==.</returns>
  48. public static bool operator !=(BsonJavaScriptWithScope lhs, BsonJavaScriptWithScope rhs)
  49. {
  50. return !(lhs == rhs);
  51. }
  52. /// <summary>
  53. /// Compares two BsonJavaScriptWithScope values.
  54. /// </summary>
  55. /// <param name="lhs">The first BsonJavaScriptWithScope.</param>
  56. /// <param name="rhs">The other BsonJavaScriptWithScope.</param>
  57. /// <returns>True if the two BsonJavaScriptWithScope values are equal according to ==.</returns>
  58. public static bool operator ==(BsonJavaScriptWithScope lhs, BsonJavaScriptWithScope rhs)
  59. {
  60. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  61. return lhs.Equals(rhs);
  62. }
  63. // public properties
  64. /// <summary>
  65. /// Gets the scope (a set of variables with values).
  66. /// </summary>
  67. public BsonDocument Scope
  68. {
  69. get { return _scope; }
  70. }
  71. // public static methods
  72. /// <summary>
  73. /// Creates a new BsonJavaScriptWithScope.
  74. /// </summary>
  75. /// <param name="value">An object to be mapped to a BsonJavaScriptWithScope.</param>
  76. /// <returns>A BsonJavaScriptWithScope or null.</returns>
  77. public new static BsonJavaScriptWithScope Create(object value)
  78. {
  79. if (value != null)
  80. {
  81. return (BsonJavaScriptWithScope)BsonTypeMapper.MapToBsonValue(value, BsonType.JavaScriptWithScope);
  82. }
  83. else
  84. {
  85. return null;
  86. }
  87. }
  88. /// <summary>
  89. /// Creates a new instance of the BsonJavaScript class.
  90. /// </summary>
  91. /// <param name="code">A string containing JavaScript code.</param>
  92. /// <param name="scope">A scope (a set of variable with values).</param>
  93. /// <returns>A BsonJavaScript.</returns>
  94. [Obsolete("Use new BsonJavaScriptWithScope(string code, BsonDocument scope) instead.")]
  95. public static BsonJavaScriptWithScope Create(string code, BsonDocument scope)
  96. {
  97. if (code != null)
  98. {
  99. return new BsonJavaScriptWithScope(code, scope);
  100. }
  101. else
  102. {
  103. return null;
  104. }
  105. }
  106. // public methods
  107. /// <summary>
  108. /// Creates a shallow clone of the BsonJavaScriptWithScope (see also DeepClone).
  109. /// </summary>
  110. /// <returns>A shallow clone of the BsonJavaScriptWithScope.</returns>
  111. public override BsonValue Clone()
  112. {
  113. return new BsonJavaScriptWithScope(Code, (BsonDocument)_scope.Clone());
  114. }
  115. /// <summary>
  116. /// Creates a deep clone of the BsonJavaScriptWithScope (see also Clone).
  117. /// </summary>
  118. /// <returns>A deep clone of the BsonJavaScriptWithScope.</returns>
  119. public override BsonValue DeepClone()
  120. {
  121. BsonJavaScriptWithScope clone = new BsonJavaScriptWithScope(Code, new BsonDocument());
  122. foreach (BsonElement element in _scope)
  123. {
  124. clone._scope.Add(element.DeepClone());
  125. }
  126. return clone;
  127. }
  128. /// <summary>
  129. /// Compares this BsonJavaScriptWithScope to another BsonJavaScriptWithScope.
  130. /// </summary>
  131. /// <param name="other">The other BsonJavaScriptWithScope.</param>
  132. /// <returns>A 32-bit signed integer that indicates whether this BsonJavaScriptWithScope is less than, equal to, or greather than the other.</returns>
  133. public int CompareTo(BsonJavaScriptWithScope other)
  134. {
  135. if (other == null) { return 1; }
  136. int r = Code.CompareTo(other.Code);
  137. if (r != 0) { return r; }
  138. return _scope.CompareTo(other._scope);
  139. }
  140. /// <summary>
  141. /// Compares the BsonJavaScriptWithScope to another BsonValue.
  142. /// </summary>
  143. /// <param name="other">The other BsonValue.</param>
  144. /// <returns>A 32-bit signed integer that indicates whether this BsonJavaScriptWithScope is less than, equal to, or greather than the other BsonValue.</returns>
  145. public override int CompareTo(BsonValue other)
  146. {
  147. if (other == null) { return 1; }
  148. var otherJavaScriptWithScope = other as BsonJavaScriptWithScope;
  149. if (otherJavaScriptWithScope != null)
  150. {
  151. return CompareTo(otherJavaScriptWithScope);
  152. }
  153. return CompareTypeTo(other);
  154. }
  155. /// <summary>
  156. /// Compares this BsonJavaScriptWithScope to another BsonJavaScriptWithScope.
  157. /// </summary>
  158. /// <param name="rhs">The other BsonJavaScriptWithScope.</param>
  159. /// <returns>True if the two BsonJavaScriptWithScope values are equal.</returns>
  160. public bool Equals(BsonJavaScriptWithScope rhs)
  161. {
  162. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  163. return Code == rhs.Code && _scope == rhs._scope;
  164. }
  165. /// <summary>
  166. /// Compares this BsonJavaScriptWithScope to another object.
  167. /// </summary>
  168. /// <param name="obj">The other object.</param>
  169. /// <returns>True if the other object is a BsonJavaScriptWithScope and equal to this one.</returns>
  170. public override bool Equals(object obj)
  171. {
  172. return Equals(obj as BsonJavaScriptWithScope); // works even if obj is null or of a different type
  173. }
  174. /// <summary>
  175. /// Gets the hash code.
  176. /// </summary>
  177. /// <returns>The hash code.</returns>
  178. public override int GetHashCode()
  179. {
  180. // see Effective Java by Joshua Bloch
  181. int hash = 17;
  182. hash = 37 * hash + base.GetHashCode();
  183. hash = 37 * hash + _scope.GetHashCode();
  184. return hash;
  185. }
  186. /// <summary>
  187. /// Returns a string representation of the value.
  188. /// </summary>
  189. /// <returns>A string representation of the value.</returns>
  190. public override string ToString()
  191. {
  192. return string.Format("new BsonJavaScript(\"{0}\", {1})", Code, _scope.ToJson());
  193. }
  194. }
  195. }