BsonJavaScript.cs 6.2 KB

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