BsonJavaScriptWithScope.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 JavaScript value with a scope.
  20. /// </summary>
  21. [Serializable]
  22. public class BsonJavaScriptWithScope : BsonJavaScript, IComparable<BsonJavaScriptWithScope>, IEquatable<BsonJavaScriptWithScope>
  23. {
  24. // private fields
  25. private readonly 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)
  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 BsonType of this BsonValue.
  66. /// </summary>
  67. public override BsonType BsonType
  68. {
  69. get { return BsonType.JavaScriptWithScope; }
  70. }
  71. /// <summary>
  72. /// Gets the scope (a set of variables with values).
  73. /// </summary>
  74. public BsonDocument Scope
  75. {
  76. get { return _scope; }
  77. }
  78. // public static methods
  79. /// <summary>
  80. /// Creates a new BsonJavaScriptWithScope.
  81. /// </summary>
  82. /// <param name="value">An object to be mapped to a BsonJavaScriptWithScope.</param>
  83. /// <returns>A BsonJavaScriptWithScope or null.</returns>
  84. public new static BsonJavaScriptWithScope Create(object value)
  85. {
  86. if (value == null)
  87. {
  88. throw new ArgumentNullException("value");
  89. }
  90. return (BsonJavaScriptWithScope)BsonTypeMapper.MapToBsonValue(value, BsonType.JavaScriptWithScope);
  91. }
  92. // public methods
  93. /// <summary>
  94. /// Creates a shallow clone of the BsonJavaScriptWithScope (see also DeepClone).
  95. /// </summary>
  96. /// <returns>A shallow clone of the BsonJavaScriptWithScope.</returns>
  97. public override BsonValue Clone()
  98. {
  99. return new BsonJavaScriptWithScope(Code, (BsonDocument)_scope.Clone());
  100. }
  101. /// <summary>
  102. /// Creates a deep clone of the BsonJavaScriptWithScope (see also Clone).
  103. /// </summary>
  104. /// <returns>A deep clone of the BsonJavaScriptWithScope.</returns>
  105. public override BsonValue DeepClone()
  106. {
  107. BsonJavaScriptWithScope clone = new BsonJavaScriptWithScope(Code, new BsonDocument());
  108. foreach (BsonElement element in _scope)
  109. {
  110. clone._scope.Add(element.DeepClone());
  111. }
  112. return clone;
  113. }
  114. /// <summary>
  115. /// Compares this BsonJavaScriptWithScope to another BsonJavaScriptWithScope.
  116. /// </summary>
  117. /// <param name="other">The other BsonJavaScriptWithScope.</param>
  118. /// <returns>A 32-bit signed integer that indicates whether this BsonJavaScriptWithScope is less than, equal to, or greather than the other.</returns>
  119. public int CompareTo(BsonJavaScriptWithScope other)
  120. {
  121. if (other == null) { return 1; }
  122. int r = Code.CompareTo(other.Code);
  123. if (r != 0) { return r; }
  124. return _scope.CompareTo(other._scope);
  125. }
  126. /// <summary>
  127. /// Compares the BsonJavaScriptWithScope to another BsonValue.
  128. /// </summary>
  129. /// <param name="other">The other BsonValue.</param>
  130. /// <returns>A 32-bit signed integer that indicates whether this BsonJavaScriptWithScope is less than, equal to, or greather than the other BsonValue.</returns>
  131. public override int CompareTo(BsonValue other)
  132. {
  133. if (other == null) { return 1; }
  134. var otherJavaScriptWithScope = other as BsonJavaScriptWithScope;
  135. if (otherJavaScriptWithScope != null)
  136. {
  137. return CompareTo(otherJavaScriptWithScope);
  138. }
  139. return CompareTypeTo(other);
  140. }
  141. /// <summary>
  142. /// Compares this BsonJavaScriptWithScope to another BsonJavaScriptWithScope.
  143. /// </summary>
  144. /// <param name="rhs">The other BsonJavaScriptWithScope.</param>
  145. /// <returns>True if the two BsonJavaScriptWithScope values are equal.</returns>
  146. public bool Equals(BsonJavaScriptWithScope rhs)
  147. {
  148. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  149. return Code == rhs.Code && _scope == rhs._scope;
  150. }
  151. /// <summary>
  152. /// Compares this BsonJavaScriptWithScope to another object.
  153. /// </summary>
  154. /// <param name="obj">The other object.</param>
  155. /// <returns>True if the other object is a BsonJavaScriptWithScope and equal to this one.</returns>
  156. public override bool Equals(object obj)
  157. {
  158. return Equals(obj as BsonJavaScriptWithScope); // works even if obj is null or of a different type
  159. }
  160. /// <summary>
  161. /// Gets the hash code.
  162. /// </summary>
  163. /// <returns>The hash code.</returns>
  164. public override int GetHashCode()
  165. {
  166. // see Effective Java by Joshua Bloch
  167. int hash = 17;
  168. hash = 37 * hash + base.GetHashCode();
  169. hash = 37 * hash + _scope.GetHashCode();
  170. return hash;
  171. }
  172. /// <summary>
  173. /// Returns a string representation of the value.
  174. /// </summary>
  175. /// <returns>A string representation of the value.</returns>
  176. public override string ToString()
  177. {
  178. return string.Format("new BsonJavaScript(\"{0}\", {1})", Code, _scope.ToJson());
  179. }
  180. }
  181. }