BsonInt32.cs 14 KB

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