BsonJavaScript.cs 6.2 KB

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