BsonInt64.cs 13 KB

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