BsonInt32.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright 2010-2014 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.Xml;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents a BSON int value.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonInt32 : BsonValue, IComparable<BsonInt32>, IEquatable<BsonInt32>
  24. {
  25. // private fields
  26. private int _value;
  27. // constructors
  28. /// <summary>
  29. /// Creates a new instance of the BsonInt32 class.
  30. /// </summary>
  31. /// <param name="value">The value.</param>
  32. public BsonInt32(int value)
  33. : base(BsonType.Int32)
  34. {
  35. _value = value;
  36. }
  37. // public static properties
  38. /// <summary>
  39. /// Gets an instance of BsonInt32 that represents -1.
  40. /// </summary>
  41. [Obsolete("Use new BsonInt32(-1) instead.")]
  42. public static BsonInt32 MinusOne
  43. {
  44. get { return new BsonInt32(-1); }
  45. }
  46. /// <summary>
  47. /// Gets an instance of BsonInt32 that represents -0.
  48. /// </summary>
  49. [Obsolete("Use new BsonInt32(0) instead.")]
  50. public static BsonInt32 Zero
  51. {
  52. get { return new BsonInt32(0); }
  53. }
  54. /// <summary>
  55. /// Gets an instance of BsonInt32 that represents 1.
  56. /// </summary>
  57. [Obsolete("Use new BsonInt32(1) instead.")]
  58. public static BsonInt32 One
  59. {
  60. get { return new BsonInt32(1); }
  61. }
  62. /// <summary>
  63. /// Gets an instance of BsonInt32 that represents 2.
  64. /// </summary>
  65. [Obsolete("Use new BsonInt32(2) instead.")]
  66. public static BsonInt32 Two
  67. {
  68. get { return new BsonInt32(2); }
  69. }
  70. /// <summary>
  71. /// Gets an instance of BsonInt32 that represents 3.
  72. /// </summary>
  73. [Obsolete("Use new BsonInt32(3) instead.")]
  74. public static BsonInt32 Three
  75. {
  76. get { return new BsonInt32(3); }
  77. }
  78. // public properties
  79. /// <summary>
  80. /// Gets the BsonInt32 as an int.
  81. /// </summary>
  82. [Obsolete("Use Value instead.")]
  83. public override object RawValue
  84. {
  85. get { return _value; }
  86. }
  87. /// <summary>
  88. /// Gets the value of this BsonInt32.
  89. /// </summary>
  90. public int Value
  91. {
  92. get { return _value; }
  93. }
  94. // public operators
  95. /// <summary>
  96. /// Converts an int to a BsonInt32.
  97. /// </summary>
  98. /// <param name="value">An int.</param>
  99. /// <returns>A BsonInt32.</returns>
  100. public static implicit operator BsonInt32(int value)
  101. {
  102. return new BsonInt32(value);
  103. }
  104. /// <summary>
  105. /// Compares two BsonInt32 values.
  106. /// </summary>
  107. /// <param name="lhs">The first BsonInt32.</param>
  108. /// <param name="rhs">The other BsonInt32.</param>
  109. /// <returns>True if the two BsonInt32 values are not equal according to ==.</returns>
  110. public static bool operator !=(BsonInt32 lhs, BsonInt32 rhs)
  111. {
  112. return !(lhs == rhs);
  113. }
  114. /// <summary>
  115. /// Compares two BsonInt32 values.
  116. /// </summary>
  117. /// <param name="lhs">The first BsonInt32.</param>
  118. /// <param name="rhs">The other BsonInt32.</param>
  119. /// <returns>True if the two BsonInt32 values are equal according to ==.</returns>
  120. public static bool operator ==(BsonInt32 lhs, BsonInt32 rhs)
  121. {
  122. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  123. return lhs.OperatorEqualsImplementation(rhs);
  124. }
  125. // public static methods
  126. /// <summary>
  127. /// Creates a new instance of the BsonInt32 class.
  128. /// </summary>
  129. /// <param name="value">An int.</param>
  130. /// <returns>A BsonInt32.</returns>
  131. [Obsolete("Use new BsonInt32(int value) instead.")]
  132. public static BsonInt32 Create(int value)
  133. {
  134. return new BsonInt32(value);
  135. }
  136. /// <summary>
  137. /// Creates a new BsonInt32.
  138. /// </summary>
  139. /// <param name="value">An object to be mapped to a BsonInt32.</param>
  140. /// <returns>A BsonInt32 or null.</returns>
  141. public new static BsonInt32 Create(object value)
  142. {
  143. if (value != null)
  144. {
  145. return (BsonInt32)BsonTypeMapper.MapToBsonValue(value, BsonType.Int32);
  146. }
  147. else
  148. {
  149. return null;
  150. }
  151. }
  152. // public methods
  153. /// <summary>
  154. /// Compares this BsonInt32 to another BsonInt32.
  155. /// </summary>
  156. /// <param name="other">The other BsonInt32.</param>
  157. /// <returns>A 32-bit signed integer that indicates whether this BsonInt32 is less than, equal to, or greather than the other.</returns>
  158. public int CompareTo(BsonInt32 other)
  159. {
  160. if (other == null) { return 1; }
  161. return _value.CompareTo(other._value);
  162. }
  163. /// <summary>
  164. /// Compares the BsonInt32 to another BsonValue.
  165. /// </summary>
  166. /// <param name="other">The other BsonValue.</param>
  167. /// <returns>A 32-bit signed integer that indicates whether this BsonInt32 is less than, equal to, or greather than the other BsonValue.</returns>
  168. public override int CompareTo(BsonValue other)
  169. {
  170. if (other == null) { return 1; }
  171. var otherInt32 = other as BsonInt32;
  172. if (otherInt32 != null)
  173. {
  174. return _value.CompareTo(otherInt32._value);
  175. }
  176. var otherInt64 = other as BsonInt64;
  177. if (otherInt64 != null)
  178. {
  179. return ((long)_value).CompareTo(otherInt64.Value);
  180. }
  181. var otherDouble = other as BsonDouble;
  182. if (otherDouble != null)
  183. {
  184. return ((double)_value).CompareTo(otherDouble.Value);
  185. }
  186. return CompareTypeTo(other);
  187. }
  188. /// <summary>
  189. /// Compares this BsonInt32 to another BsonInt32.
  190. /// </summary>
  191. /// <param name="rhs">The other BsonInt32.</param>
  192. /// <returns>True if the two BsonInt32 values are equal.</returns>
  193. public bool Equals(BsonInt32 rhs)
  194. {
  195. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  196. return _value == rhs._value;
  197. }
  198. /// <summary>
  199. /// Compares this BsonInt32 to another object.
  200. /// </summary>
  201. /// <param name="obj">The other object.</param>
  202. /// <returns>True if the other object is a BsonInt32 and equal to this one.</returns>
  203. public override bool Equals(object obj)
  204. {
  205. return Equals(obj as BsonInt32); // works even if obj is null or of a different type
  206. }
  207. /// <summary>
  208. /// Gets the hash code.
  209. /// </summary>
  210. /// <returns>The hash code.</returns>
  211. public override int GetHashCode()
  212. {
  213. // see Effective Java by Joshua Bloch
  214. int hash = 17;
  215. hash = 37 * hash + BsonType.GetHashCode();
  216. hash = 37 * hash + _value.GetHashCode();
  217. return hash;
  218. }
  219. /// <summary>
  220. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  221. /// </summary>
  222. /// <returns>A Boolean.</returns>
  223. public override bool ToBoolean()
  224. {
  225. return _value != 0;
  226. }
  227. /// <summary>
  228. /// Converts this BsonValue to a Double.
  229. /// </summary>
  230. /// <returns>A Double.</returns>
  231. public override double ToDouble()
  232. {
  233. return (double)_value;
  234. }
  235. /// <summary>
  236. /// Converts this BsonValue to an Int32.
  237. /// </summary>
  238. /// <returns>An Int32.</returns>
  239. public override int ToInt32()
  240. {
  241. return _value;
  242. }
  243. /// <summary>
  244. /// Converts this BsonValue to an Int64.
  245. /// </summary>
  246. /// <returns>An Int32.</returns>
  247. public override long ToInt64()
  248. {
  249. return (long)_value;
  250. }
  251. /// <summary>
  252. /// Returns a string representation of the value.
  253. /// </summary>
  254. /// <returns>A string representation of the value.</returns>
  255. public override string ToString()
  256. {
  257. return XmlConvert.ToString(_value);
  258. }
  259. // protected methods
  260. /// <summary>
  261. /// Compares this BsonInt32 against another BsonValue.
  262. /// </summary>
  263. /// <param name="rhs">The other BsonValue.</param>
  264. /// <returns>True if this BsonInt32 and the other BsonValue are equal according to ==.</returns>
  265. protected override bool OperatorEqualsImplementation(BsonValue rhs)
  266. {
  267. var rhsInt32 = rhs as BsonInt32;
  268. if (rhsInt32 != null)
  269. {
  270. return _value == rhsInt32._value;
  271. }
  272. var rhsInt64 = rhs as BsonInt64;
  273. if (rhsInt64 != null)
  274. {
  275. return (long)_value == rhsInt64.Value;
  276. }
  277. var rhsDouble = rhs as BsonDouble;
  278. if (rhsDouble != null)
  279. {
  280. return (double)_value == rhsDouble.Value; // use == instead of Equals so NaN is handled correctly
  281. }
  282. return this.Equals(rhs);
  283. }
  284. }
  285. }