BsonJavaScript.cs 7.2 KB

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