BsonInt64.cs 13 KB

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