BsonBoolean.cs 10 KB

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