BsonDouble.cs 11 KB

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