BsonJavaScriptWithScope.cs 7.5 KB

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