BsonBoolean.cs 10 KB

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