BsonInt32.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 int value.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonInt32 : BsonValue, IComparable<BsonInt32>, IEquatable<BsonInt32>
  24. {
  25. #region static
  26. const int __minPrecreatedValue = -100;
  27. const int __maxPrecreatedValue = 100;
  28. private static readonly BsonInt32[] __precreatedInstances = new BsonInt32[__maxPrecreatedValue - __minPrecreatedValue + 1];
  29. static BsonInt32()
  30. {
  31. for (var i = __minPrecreatedValue; i <= __maxPrecreatedValue; i++)
  32. {
  33. var precreatedInstance = new BsonInt32(i);
  34. var index = i - __minPrecreatedValue;
  35. __precreatedInstances[index] = precreatedInstance;
  36. }
  37. }
  38. #endregion
  39. // private fields
  40. private readonly int _value;
  41. // constructors
  42. /// <summary>
  43. /// Creates a new instance of the BsonInt32 class.
  44. /// </summary>
  45. /// <param name="value">The value.</param>
  46. public BsonInt32(int value)
  47. {
  48. _value = value;
  49. }
  50. // public static properties
  51. /// <summary>
  52. /// Gets an instance of BsonInt32 that represents -1.
  53. /// </summary>
  54. [Obsolete("Use (BsonInt32)(-1) instead.")]
  55. public static BsonInt32 MinusOne
  56. {
  57. get { return (BsonInt32)(-1); }
  58. }
  59. /// <summary>
  60. /// Gets an instance of BsonInt32 that represents -0.
  61. /// </summary>
  62. [Obsolete("Use (BsonInt32)0 instead.")]
  63. public static BsonInt32 Zero
  64. {
  65. get { return (BsonInt32)0; }
  66. }
  67. /// <summary>
  68. /// Gets an instance of BsonInt32 that represents 1.
  69. /// </summary>
  70. [Obsolete("Use (BsonInt32)1 instead.")]
  71. public static BsonInt32 One
  72. {
  73. get { return (BsonInt32)1; }
  74. }
  75. /// <summary>
  76. /// Gets an instance of BsonInt32 that represents 2.
  77. /// </summary>
  78. [Obsolete("Use (BsonInt32)2 instead.")]
  79. public static BsonInt32 Two
  80. {
  81. get { return (BsonInt32)2; }
  82. }
  83. /// <summary>
  84. /// Gets an instance of BsonInt32 that represents 3.
  85. /// </summary>
  86. [Obsolete("Use (BsonInt32)3 instead.")]
  87. public static BsonInt32 Three
  88. {
  89. get { return (BsonInt32)3; }
  90. }
  91. // public properties
  92. /// <summary>
  93. /// Gets the BsonType of this BsonValue.
  94. /// </summary>
  95. public override BsonType BsonType
  96. {
  97. get { return BsonType.Int32; }
  98. }
  99. /// <summary>
  100. /// Gets the BsonInt32 as an int.
  101. /// </summary>
  102. [Obsolete("Use Value instead.")]
  103. public override object RawValue
  104. {
  105. get { return _value; }
  106. }
  107. /// <summary>
  108. /// Gets the value of this BsonInt32.
  109. /// </summary>
  110. public int Value
  111. {
  112. get { return _value; }
  113. }
  114. // public operators
  115. /// <summary>
  116. /// Converts an int to a BsonInt32.
  117. /// </summary>
  118. /// <param name="value">An int.</param>
  119. /// <returns>A BsonInt32.</returns>
  120. public static implicit operator BsonInt32(int value)
  121. {
  122. if (value >= __minPrecreatedValue && value <= __maxPrecreatedValue)
  123. {
  124. var index = value - __minPrecreatedValue;
  125. return __precreatedInstances[index];
  126. }
  127. return new BsonInt32(value);
  128. }
  129. /// <summary>
  130. /// Compares two BsonInt32 values.
  131. /// </summary>
  132. /// <param name="lhs">The first BsonInt32.</param>
  133. /// <param name="rhs">The other BsonInt32.</param>
  134. /// <returns>True if the two BsonInt32 values are not equal according to ==.</returns>
  135. public static bool operator !=(BsonInt32 lhs, BsonInt32 rhs)
  136. {
  137. return !(lhs == rhs);
  138. }
  139. /// <summary>
  140. /// Compares two BsonInt32 values.
  141. /// </summary>
  142. /// <param name="lhs">The first BsonInt32.</param>
  143. /// <param name="rhs">The other BsonInt32.</param>
  144. /// <returns>True if the two BsonInt32 values are equal according to ==.</returns>
  145. public static bool operator ==(BsonInt32 lhs, BsonInt32 rhs)
  146. {
  147. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  148. return lhs.OperatorEqualsImplementation(rhs);
  149. }
  150. // public static methods
  151. /// <summary>
  152. /// Creates a new BsonInt32.
  153. /// </summary>
  154. /// <param name="value">An object to be mapped to a BsonInt32.</param>
  155. /// <returns>A BsonInt32 or null.</returns>
  156. public new static BsonInt32 Create(object value)
  157. {
  158. if (value == null)
  159. {
  160. throw new ArgumentNullException("value");
  161. }
  162. return (BsonInt32)BsonTypeMapper.MapToBsonValue(value, BsonType.Int32);
  163. }
  164. // public methods
  165. /// <summary>
  166. /// Compares this BsonInt32 to another BsonInt32.
  167. /// </summary>
  168. /// <param name="other">The other BsonInt32.</param>
  169. /// <returns>A 32-bit signed integer that indicates whether this BsonInt32 is less than, equal to, or greather than the other.</returns>
  170. public int CompareTo(BsonInt32 other)
  171. {
  172. if (other == null) { return 1; }
  173. return _value.CompareTo(other._value);
  174. }
  175. /// <summary>
  176. /// Compares the BsonInt32 to another BsonValue.
  177. /// </summary>
  178. /// <param name="other">The other BsonValue.</param>
  179. /// <returns>A 32-bit signed integer that indicates whether this BsonInt32 is less than, equal to, or greather than the other BsonValue.</returns>
  180. public override int CompareTo(BsonValue other)
  181. {
  182. if (other == null) { return 1; }
  183. var otherInt32 = other as BsonInt32;
  184. if (otherInt32 != null)
  185. {
  186. return _value.CompareTo(otherInt32._value);
  187. }
  188. var otherInt64 = other as BsonInt64;
  189. if (otherInt64 != null)
  190. {
  191. return ((long)_value).CompareTo(otherInt64.Value);
  192. }
  193. var otherDouble = other as BsonDouble;
  194. if (otherDouble != null)
  195. {
  196. return ((double)_value).CompareTo(otherDouble.Value);
  197. }
  198. var otherDecimal128 = other as BsonDecimal128;
  199. if (otherDecimal128 != null)
  200. {
  201. return ((Decimal128)_value).CompareTo(otherDecimal128.Value);
  202. }
  203. return CompareTypeTo(other);
  204. }
  205. /// <summary>
  206. /// Compares this BsonInt32 to another BsonInt32.
  207. /// </summary>
  208. /// <param name="rhs">The other BsonInt32.</param>
  209. /// <returns>True if the two BsonInt32 values are equal.</returns>
  210. public bool Equals(BsonInt32 rhs)
  211. {
  212. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  213. return _value == rhs._value;
  214. }
  215. /// <summary>
  216. /// Compares this BsonInt32 to another object.
  217. /// </summary>
  218. /// <param name="obj">The other object.</param>
  219. /// <returns>True if the other object is a BsonInt32 and equal to this one.</returns>
  220. public override bool Equals(object obj)
  221. {
  222. return Equals(obj as BsonInt32); // works even if obj is null or of a different type
  223. }
  224. /// <summary>
  225. /// Gets the hash code.
  226. /// </summary>
  227. /// <returns>The hash code.</returns>
  228. public override int GetHashCode()
  229. {
  230. // see Effective Java by Joshua Bloch
  231. int hash = 17;
  232. hash = 37 * hash + BsonType.GetHashCode();
  233. hash = 37 * hash + _value.GetHashCode();
  234. return hash;
  235. }
  236. /// <summary>
  237. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  238. /// </summary>
  239. /// <returns>A Boolean.</returns>
  240. public override bool ToBoolean()
  241. {
  242. return _value != 0;
  243. }
  244. /// <inheritdoc/>
  245. public override decimal ToDecimal()
  246. {
  247. return (decimal)_value;
  248. }
  249. /// <inheritdoc/>
  250. public override Decimal128 ToDecimal128()
  251. {
  252. return (Decimal128)_value;
  253. }
  254. /// <summary>
  255. /// Converts this BsonValue to a Double.
  256. /// </summary>
  257. /// <returns>A Double.</returns>
  258. public override double ToDouble()
  259. {
  260. return (double)_value;
  261. }
  262. /// <summary>
  263. /// Converts this BsonValue to an Int32.
  264. /// </summary>
  265. /// <returns>An Int32.</returns>
  266. public override int ToInt32()
  267. {
  268. return _value;
  269. }
  270. /// <summary>
  271. /// Converts this BsonValue to an Int64.
  272. /// </summary>
  273. /// <returns>An Int32.</returns>
  274. public override long ToInt64()
  275. {
  276. return (long)_value;
  277. }
  278. /// <summary>
  279. /// Returns a string representation of the value.
  280. /// </summary>
  281. /// <returns>A string representation of the value.</returns>
  282. public override string ToString()
  283. {
  284. return JsonConvert.ToString(_value);
  285. }
  286. // protected methods
  287. /// <inheritdoc/>
  288. protected override TypeCode IConvertibleGetTypeCodeImplementation()
  289. {
  290. return TypeCode.Int32;
  291. }
  292. /// <inheritdoc/>
  293. protected override bool IConvertibleToBooleanImplementation(IFormatProvider provider)
  294. {
  295. return Convert.ToBoolean(_value, provider);
  296. }
  297. /// <inheritdoc/>
  298. protected override byte IConvertibleToByteImplementation(IFormatProvider provider)
  299. {
  300. return Convert.ToByte(_value, provider);
  301. }
  302. /// <inheritdoc/>
  303. protected override char IConvertibleToCharImplementation(IFormatProvider provider)
  304. {
  305. return Convert.ToChar(_value, provider);
  306. }
  307. /// <inheritdoc/>
  308. protected override decimal IConvertibleToDecimalImplementation(IFormatProvider provider)
  309. {
  310. return Convert.ToDecimal(_value, provider);
  311. }
  312. /// <inheritdoc/>
  313. protected override double IConvertibleToDoubleImplementation(IFormatProvider provider)
  314. {
  315. return Convert.ToDouble(_value, provider);
  316. }
  317. /// <inheritdoc/>
  318. protected override short IConvertibleToInt16Implementation(IFormatProvider provider)
  319. {
  320. return Convert.ToInt16(_value, provider);
  321. }
  322. /// <inheritdoc/>
  323. protected override int IConvertibleToInt32Implementation(IFormatProvider provider)
  324. {
  325. return _value;
  326. }
  327. /// <inheritdoc/>
  328. protected override long IConvertibleToInt64Implementation(IFormatProvider provider)
  329. {
  330. return Convert.ToInt64(_value, provider);
  331. }
  332. /// <inheritdoc/>
  333. #pragma warning disable 3002
  334. protected override sbyte IConvertibleToSByteImplementation(IFormatProvider provider)
  335. {
  336. return Convert.ToSByte(_value, provider);
  337. }
  338. #pragma warning restore
  339. /// <inheritdoc/>
  340. protected override float IConvertibleToSingleImplementation(IFormatProvider provider)
  341. {
  342. return Convert.ToSingle(_value, provider);
  343. }
  344. /// <inheritdoc/>
  345. protected override string IConvertibleToStringImplementation(IFormatProvider provider)
  346. {
  347. return Convert.ToString(_value, provider);
  348. }
  349. /// <inheritdoc/>
  350. #pragma warning disable 3002
  351. protected override ushort IConvertibleToUInt16Implementation(IFormatProvider provider)
  352. {
  353. return Convert.ToUInt16(_value, provider);
  354. }
  355. #pragma warning restore
  356. /// <inheritdoc/>
  357. #pragma warning disable 3002
  358. protected override uint IConvertibleToUInt32Implementation(IFormatProvider provider)
  359. {
  360. return Convert.ToUInt32(_value, provider);
  361. }
  362. #pragma warning restore
  363. /// <inheritdoc/>
  364. #pragma warning disable 3002
  365. protected override ulong IConvertibleToUInt64Implementation(IFormatProvider provider)
  366. {
  367. return Convert.ToUInt64(_value, provider);
  368. }
  369. #pragma warning restore
  370. /// <summary>
  371. /// Compares this BsonInt32 against another BsonValue.
  372. /// </summary>
  373. /// <param name="rhs">The other BsonValue.</param>
  374. /// <returns>True if this BsonInt32 and the other BsonValue are equal according to ==.</returns>
  375. protected override bool OperatorEqualsImplementation(BsonValue rhs)
  376. {
  377. var rhsInt32 = rhs as BsonInt32;
  378. if (rhsInt32 != null)
  379. {
  380. return _value == rhsInt32._value;
  381. }
  382. var rhsInt64 = rhs as BsonInt64;
  383. if (rhsInt64 != null)
  384. {
  385. return (long)_value == rhsInt64.Value;
  386. }
  387. var rhsDouble = rhs as BsonDouble;
  388. if (rhsDouble != null)
  389. {
  390. return (double)_value == rhsDouble.Value; // use == instead of Equals so NaN is handled correctly
  391. }
  392. var rhsDecimal128 = rhs as BsonDecimal128;
  393. if (rhsDecimal128 != null)
  394. {
  395. return _value == (int)rhsDecimal128.Value; // use == instead of Equals so NaN is handled correctly
  396. }
  397. return this.Equals(rhs);
  398. }
  399. }
  400. }