BsonInt64.cs 8.6 KB

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