BsonString.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. using System.Xml;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents a BSON string value.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonString : BsonValue, IComparable<BsonString>, IEquatable<BsonString>
  24. {
  25. // private static fields
  26. private static BsonString __emptyInstance = new BsonString("");
  27. // private fields
  28. private string _value;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonString class.
  32. /// </summary>
  33. /// <param name="value">The value.</param>
  34. public BsonString(string value)
  35. : base(BsonType.String)
  36. {
  37. if (value == null)
  38. {
  39. throw new ArgumentNullException("value");
  40. }
  41. _value = value;
  42. }
  43. // public static properties
  44. /// <summary>
  45. /// Gets an instance of BsonString that represents an empty string.
  46. /// </summary>
  47. public static BsonString Empty
  48. {
  49. get { return __emptyInstance; }
  50. }
  51. // public properties
  52. /// <summary>
  53. /// Gets the BsonString as a string.
  54. /// </summary>
  55. [Obsolete("Use Value instead.")]
  56. public override object RawValue
  57. {
  58. get { return _value; }
  59. }
  60. /// <summary>
  61. /// Gets the value of this BsonString.
  62. /// </summary>
  63. public string Value
  64. {
  65. get { return _value; }
  66. }
  67. // public operators
  68. /// <summary>
  69. /// Converts a string to a BsonString.
  70. /// </summary>
  71. /// <param name="value">A string.</param>
  72. /// <returns>A BsonString.</returns>
  73. public static implicit operator BsonString(string value)
  74. {
  75. return new BsonString(value);
  76. }
  77. /// <summary>
  78. /// Compares two BsonString values.
  79. /// </summary>
  80. /// <param name="lhs">The first BsonString.</param>
  81. /// <param name="rhs">The other BsonString.</param>
  82. /// <returns>True if the two BsonString values are not equal according to ==.</returns>
  83. public static bool operator !=(BsonString lhs, BsonString rhs)
  84. {
  85. return !(lhs == rhs);
  86. }
  87. /// <summary>
  88. /// Compares two BsonString values.
  89. /// </summary>
  90. /// <param name="lhs">The first BsonString.</param>
  91. /// <param name="rhs">The other BsonString.</param>
  92. /// <returns>True if the two BsonString values are equal according to ==.</returns>
  93. public static bool operator ==(BsonString lhs, BsonString rhs)
  94. {
  95. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  96. return lhs.Equals(rhs);
  97. }
  98. // public static methods
  99. /// <summary>
  100. /// Creates a new BsonString.
  101. /// </summary>
  102. /// <param name="value">An object to be mapped to a BsonString.</param>
  103. /// <returns>A BsonString or null.</returns>
  104. public new static BsonString Create(object value)
  105. {
  106. if (value != null)
  107. {
  108. return (BsonString)BsonTypeMapper.MapToBsonValue(value, BsonType.String);
  109. }
  110. else
  111. {
  112. return null;
  113. }
  114. }
  115. /// <summary>
  116. /// Creates a new instance of the BsonString class.
  117. /// </summary>
  118. /// <param name="value">A string.</param>
  119. /// <returns>A BsonString.</returns>
  120. [Obsolete("Use new BsonString(string value) instead.")]
  121. public static BsonString Create(string value)
  122. {
  123. if (value != null)
  124. {
  125. // TODO: are there any other commonly used strings worth checking for?
  126. return value == "" ? __emptyInstance : new BsonString(value);
  127. }
  128. else
  129. {
  130. return null;
  131. }
  132. }
  133. // public methods
  134. /// <summary>
  135. /// Compares this BsonString to another BsonString.
  136. /// </summary>
  137. /// <param name="other">The other BsonString.</param>
  138. /// <returns>A 32-bit signed integer that indicates whether this BsonString is less than, equal to, or greather than the other.</returns>
  139. public int CompareTo(BsonString other)
  140. {
  141. if (other == null) { return 1; }
  142. return _value.CompareTo(other.Value);
  143. }
  144. /// <summary>
  145. /// Compares the BsonString to another BsonValue.
  146. /// </summary>
  147. /// <param name="other">The other BsonValue.</param>
  148. /// <returns>A 32-bit signed integer that indicates whether this BsonString is less than, equal to, or greather than the other BsonValue.</returns>
  149. public override int CompareTo(BsonValue other)
  150. {
  151. if (other == null) { return 1; }
  152. var otherString = other as BsonString;
  153. if (otherString != null)
  154. {
  155. return _value.CompareTo(otherString.Value);
  156. }
  157. var otherSymbol = other as BsonSymbol;
  158. if (otherSymbol != null)
  159. {
  160. return _value.CompareTo(otherSymbol.Name);
  161. }
  162. return CompareTypeTo(other);
  163. }
  164. /// <summary>
  165. /// Compares this BsonString to another BsonString.
  166. /// </summary>
  167. /// <param name="rhs">The other BsonString.</param>
  168. /// <returns>True if the two BsonString values are equal.</returns>
  169. public bool Equals(BsonString rhs)
  170. {
  171. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  172. return _value == rhs._value;
  173. }
  174. /// <summary>
  175. /// Compares this BsonString to another object.
  176. /// </summary>
  177. /// <param name="obj">The other object.</param>
  178. /// <returns>True if the other object is a BsonString and equal to this one.</returns>
  179. public override bool Equals(object obj)
  180. {
  181. return Equals(obj as BsonString); // works even if obj is null or of a different type
  182. }
  183. /// <summary>
  184. /// Gets the hash code.
  185. /// </summary>
  186. /// <returns>The hash code.</returns>
  187. public override int GetHashCode()
  188. {
  189. // see Effective Java by Joshua Bloch
  190. int hash = 17;
  191. hash = 37 * hash + BsonType.GetHashCode();
  192. hash = 37 * hash + _value.GetHashCode();
  193. return hash;
  194. }
  195. /// <summary>
  196. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  197. /// </summary>
  198. /// <returns>A Boolean.</returns>
  199. public override bool ToBoolean()
  200. {
  201. return _value != "";
  202. }
  203. /// <summary>
  204. /// Converts this BsonValue to a Double.
  205. /// </summary>
  206. /// <returns>A Double.</returns>
  207. public override double ToDouble()
  208. {
  209. return XmlConvert.ToDouble(_value);
  210. }
  211. /// <summary>
  212. /// Converts this BsonValue to an Int32.
  213. /// </summary>
  214. /// <returns>An Int32.</returns>
  215. public override int ToInt32()
  216. {
  217. return XmlConvert.ToInt32(_value);
  218. }
  219. /// <summary>
  220. /// Converts this BsonValue to an Int64.
  221. /// </summary>
  222. /// <returns>An Int32.</returns>
  223. public override long ToInt64()
  224. {
  225. return XmlConvert.ToInt64(_value);
  226. }
  227. /// <summary>
  228. /// Returns a string representation of the value.
  229. /// </summary>
  230. /// <returns>A string representation of the value.</returns>
  231. public override string ToString()
  232. {
  233. return _value;
  234. }
  235. }
  236. }