BsonString.cs 11 KB

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