BsonString.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. using MongoDB.Bson.IO;
  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 readonly 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. {
  36. if (value == null)
  37. {
  38. throw new ArgumentNullException("value");
  39. }
  40. _value = value;
  41. }
  42. // public static properties
  43. /// <summary>
  44. /// Gets an instance of BsonString that represents an empty string.
  45. /// </summary>
  46. public static BsonString Empty
  47. {
  48. get { return __emptyInstance; }
  49. }
  50. // public properties
  51. /// <summary>
  52. /// Gets the BsonType of this BsonValue.
  53. /// </summary>
  54. public override BsonType BsonType
  55. {
  56. get { return BsonType.String; }
  57. }
  58. /// <summary>
  59. /// Gets the BsonString as a string.
  60. /// </summary>
  61. [Obsolete("Use Value instead.")]
  62. public override object RawValue
  63. {
  64. get { return _value; }
  65. }
  66. /// <summary>
  67. /// Gets the value of this BsonString.
  68. /// </summary>
  69. public string Value
  70. {
  71. get { return _value; }
  72. }
  73. // public operators
  74. /// <summary>
  75. /// Converts a string to a BsonString.
  76. /// </summary>
  77. /// <param name="value">A string.</param>
  78. /// <returns>A BsonString.</returns>
  79. public static implicit operator BsonString(string value)
  80. {
  81. if (value != null && value.Length == 0)
  82. {
  83. return __emptyInstance;
  84. }
  85. return new BsonString(value);
  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 not equal according to ==.</returns>
  93. public static bool operator !=(BsonString lhs, BsonString rhs)
  94. {
  95. return !(lhs == rhs);
  96. }
  97. /// <summary>
  98. /// Compares two BsonString values.
  99. /// </summary>
  100. /// <param name="lhs">The first BsonString.</param>
  101. /// <param name="rhs">The other BsonString.</param>
  102. /// <returns>True if the two BsonString values are equal according to ==.</returns>
  103. public static bool operator ==(BsonString lhs, BsonString rhs)
  104. {
  105. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  106. return lhs.Equals(rhs);
  107. }
  108. // public static methods
  109. /// <summary>
  110. /// Creates a new BsonString.
  111. /// </summary>
  112. /// <param name="value">An object to be mapped to a BsonString.</param>
  113. /// <returns>A BsonString or null.</returns>
  114. public new static BsonString Create(object value)
  115. {
  116. if (value == null)
  117. {
  118. throw new ArgumentNullException("value");
  119. }
  120. return (BsonString)BsonTypeMapper.MapToBsonValue(value, BsonType.String);
  121. }
  122. // public methods
  123. /// <summary>
  124. /// Compares this BsonString to another BsonString.
  125. /// </summary>
  126. /// <param name="other">The other BsonString.</param>
  127. /// <returns>A 32-bit signed integer that indicates whether this BsonString is less than, equal to, or greather than the other.</returns>
  128. public int CompareTo(BsonString other)
  129. {
  130. if (other == null) { return 1; }
  131. return _value.CompareTo(other.Value);
  132. }
  133. /// <summary>
  134. /// Compares the BsonString to another BsonValue.
  135. /// </summary>
  136. /// <param name="other">The other BsonValue.</param>
  137. /// <returns>A 32-bit signed integer that indicates whether this BsonString is less than, equal to, or greather than the other BsonValue.</returns>
  138. public override int CompareTo(BsonValue other)
  139. {
  140. if (other == null) { return 1; }
  141. var otherString = other as BsonString;
  142. if (otherString != null)
  143. {
  144. return _value.CompareTo(otherString.Value);
  145. }
  146. var otherSymbol = other as BsonSymbol;
  147. if (otherSymbol != null)
  148. {
  149. return _value.CompareTo(otherSymbol.Name);
  150. }
  151. return CompareTypeTo(other);
  152. }
  153. /// <summary>
  154. /// Compares this BsonString to another BsonString.
  155. /// </summary>
  156. /// <param name="rhs">The other BsonString.</param>
  157. /// <returns>True if the two BsonString values are equal.</returns>
  158. public bool Equals(BsonString rhs)
  159. {
  160. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  161. return _value == rhs._value;
  162. }
  163. /// <summary>
  164. /// Compares this BsonString to another object.
  165. /// </summary>
  166. /// <param name="obj">The other object.</param>
  167. /// <returns>True if the other object is a BsonString and equal to this one.</returns>
  168. public override bool Equals(object obj)
  169. {
  170. return Equals(obj as BsonString); // works even if obj is null or of a different type
  171. }
  172. /// <summary>
  173. /// Gets the hash code.
  174. /// </summary>
  175. /// <returns>The hash code.</returns>
  176. public override int GetHashCode()
  177. {
  178. // see Effective Java by Joshua Bloch
  179. int hash = 17;
  180. hash = 37 * hash + BsonType.GetHashCode();
  181. hash = 37 * hash + _value.GetHashCode();
  182. return hash;
  183. }
  184. /// <summary>
  185. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  186. /// </summary>
  187. /// <returns>A Boolean.</returns>
  188. public override bool ToBoolean()
  189. {
  190. return _value != "";
  191. }
  192. /// <inheritdoc/>
  193. public override decimal ToDecimal()
  194. {
  195. return JsonConvert.ToDecimal(_value);
  196. }
  197. /// <inheritdoc/>
  198. public override Decimal128 ToDecimal128()
  199. {
  200. return JsonConvert.ToDecimal128(_value);
  201. }
  202. /// <summary>
  203. /// Converts this BsonValue to a Double.
  204. /// </summary>
  205. /// <returns>A Double.</returns>
  206. public override double ToDouble()
  207. {
  208. return JsonConvert.ToDouble(_value);
  209. }
  210. /// <summary>
  211. /// Converts this BsonValue to an Int32.
  212. /// </summary>
  213. /// <returns>An Int32.</returns>
  214. public override int ToInt32()
  215. {
  216. return JsonConvert.ToInt32(_value);
  217. }
  218. /// <summary>
  219. /// Converts this BsonValue to an Int64.
  220. /// </summary>
  221. /// <returns>An Int32.</returns>
  222. public override long ToInt64()
  223. {
  224. return JsonConvert.ToInt64(_value);
  225. }
  226. /// <summary>
  227. /// Returns a string representation of the value.
  228. /// </summary>
  229. /// <returns>A string representation of the value.</returns>
  230. public override string ToString()
  231. {
  232. return _value;
  233. }
  234. // protected methods
  235. /// <inheritdoc/>
  236. protected override TypeCode IConvertibleGetTypeCodeImplementation()
  237. {
  238. return TypeCode.String;
  239. }
  240. /// <inheritdoc/>
  241. protected override byte IConvertibleToByteImplementation(IFormatProvider provider)
  242. {
  243. return Convert.ToByte(_value, provider);
  244. }
  245. /// <inheritdoc/>
  246. protected override bool IConvertibleToBooleanImplementation(IFormatProvider provider)
  247. {
  248. return Convert.ToBoolean(_value, provider);
  249. }
  250. /// <inheritdoc/>
  251. protected override char IConvertibleToCharImplementation(IFormatProvider provider)
  252. {
  253. return Convert.ToChar(_value, provider);
  254. }
  255. /// <inheritdoc/>
  256. protected override DateTime IConvertibleToDateTimeImplementation(IFormatProvider provider)
  257. {
  258. return Convert.ToDateTime(_value, provider);
  259. }
  260. /// <inheritdoc/>
  261. protected override decimal IConvertibleToDecimalImplementation(IFormatProvider provider)
  262. {
  263. return Convert.ToDecimal(_value, provider);
  264. }
  265. /// <inheritdoc/>
  266. protected override double IConvertibleToDoubleImplementation(IFormatProvider provider)
  267. {
  268. return Convert.ToDouble(_value, provider);
  269. }
  270. /// <inheritdoc/>
  271. protected override short IConvertibleToInt16Implementation(IFormatProvider provider)
  272. {
  273. return Convert.ToInt16(_value, provider);
  274. }
  275. /// <inheritdoc/>
  276. protected override int IConvertibleToInt32Implementation(IFormatProvider provider)
  277. {
  278. return Convert.ToInt32(_value, provider);
  279. }
  280. /// <inheritdoc/>
  281. protected override long IConvertibleToInt64Implementation(IFormatProvider provider)
  282. {
  283. return Convert.ToInt64(_value, provider);
  284. }
  285. /// <inheritdoc/>
  286. #pragma warning disable 3002
  287. protected override sbyte IConvertibleToSByteImplementation(IFormatProvider provider)
  288. {
  289. return Convert.ToSByte(_value, provider);
  290. }
  291. #pragma warning restore
  292. /// <inheritdoc/>
  293. protected override float IConvertibleToSingleImplementation(IFormatProvider provider)
  294. {
  295. return Convert.ToSingle(_value, provider);
  296. }
  297. /// <inheritdoc/>
  298. protected override string IConvertibleToStringImplementation(IFormatProvider provider)
  299. {
  300. return _value;
  301. }
  302. /// <inheritdoc/>
  303. #pragma warning disable 3002
  304. protected override ushort IConvertibleToUInt16Implementation(IFormatProvider provider)
  305. {
  306. return Convert.ToUInt16(_value, provider);
  307. }
  308. #pragma warning restore
  309. /// <inheritdoc/>
  310. #pragma warning disable 3002
  311. protected override uint IConvertibleToUInt32Implementation(IFormatProvider provider)
  312. {
  313. return Convert.ToUInt32(_value, provider);
  314. }
  315. #pragma warning restore
  316. /// <inheritdoc/>
  317. #pragma warning disable 3002
  318. protected override ulong IConvertibleToUInt64Implementation(IFormatProvider provider)
  319. {
  320. return Convert.ToUInt64(_value, provider);
  321. }
  322. #pragma warning restore
  323. }
  324. }