BsonDecimal128.cs 11 KB

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