BsonBoolean.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 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 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. : base(BsonType.Boolean)
  37. {
  38. _value = value;
  39. }
  40. // public static properties
  41. /// <summary>
  42. /// Gets the instance of BsonBoolean that represents false.
  43. /// </summary>
  44. public static BsonBoolean False
  45. {
  46. get { return __falseInstance; }
  47. }
  48. /// <summary>
  49. /// Gets the instance of BsonBoolean that represents true.
  50. /// </summary>
  51. public static BsonBoolean True
  52. {
  53. get { return __trueInstance; }
  54. }
  55. // public properties
  56. /// <summary>
  57. /// Gets the BsonBoolean as a bool.
  58. /// </summary>
  59. [Obsolete("Use Value instead.")]
  60. public override object RawValue
  61. {
  62. get { return _value; }
  63. }
  64. /// <summary>
  65. /// Gets the value of this BsonBoolean.
  66. /// </summary>
  67. public bool Value
  68. {
  69. get { return _value; }
  70. }
  71. // public operators
  72. /// <summary>
  73. /// Converts a bool to a BsonBoolean.
  74. /// </summary>
  75. /// <param name="value">A bool.</param>
  76. /// <returns>A BsonBoolean.</returns>
  77. public static implicit operator BsonBoolean(bool value)
  78. {
  79. return value ? __trueInstance : __falseInstance;
  80. }
  81. /// <summary>
  82. /// Compares two BsonBoolean values.
  83. /// </summary>
  84. /// <param name="lhs">The first BsonBoolean.</param>
  85. /// <param name="rhs">The other BsonBoolean.</param>
  86. /// <returns>True if the two BsonBoolean values are not equal according to ==.</returns>
  87. public static bool operator !=(BsonBoolean lhs, BsonBoolean rhs)
  88. {
  89. return !(lhs == rhs);
  90. }
  91. /// <summary>
  92. /// Compares two BsonBoolean values.
  93. /// </summary>
  94. /// <param name="lhs">The first BsonBoolean.</param>
  95. /// <param name="rhs">The other BsonBoolean.</param>
  96. /// <returns>True if the two BsonBoolean values are equal according to ==.</returns>
  97. public static bool operator ==(BsonBoolean lhs, BsonBoolean rhs)
  98. {
  99. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  100. return lhs.Equals(rhs);
  101. }
  102. // public static methods
  103. /// <summary>
  104. /// Returns one of the two possible BsonBoolean values.
  105. /// </summary>
  106. /// <param name="value">The bool value.</param>
  107. /// <returns>The corresponding BsonBoolean value.</returns>
  108. [Obsolete("Use implicit conversion to BsonBoolean or new BsonBoolean(bool value) instead.")]
  109. public static BsonBoolean Create(bool value)
  110. {
  111. return value ? __trueInstance : __falseInstance;
  112. }
  113. /// <summary>
  114. /// Returns one of the two possible BsonBoolean values.
  115. /// </summary>
  116. /// <param name="value">An object to be mapped to a BsonBoolean.</param>
  117. /// <returns>A BsonBoolean or null.</returns>
  118. public new static BsonBoolean Create(object value)
  119. {
  120. if (value != null)
  121. {
  122. return (BsonBoolean)BsonTypeMapper.MapToBsonValue(value, BsonType.Boolean);
  123. }
  124. else
  125. {
  126. return null;
  127. }
  128. }
  129. // public methods
  130. /// <summary>
  131. /// Compares this BsonBoolean to another BsonBoolean.
  132. /// </summary>
  133. /// <param name="other">The other BsonBoolean.</param>
  134. /// <returns>A 32-bit signed integer that indicates whether this BsonBoolean is less than, equal to, or greather than the other.</returns>
  135. public int CompareTo(BsonBoolean other)
  136. {
  137. if (other == null) { return 1; }
  138. return (_value ? 1 : 0).CompareTo(other._value ? 1 : 0);
  139. }
  140. /// <summary>
  141. /// Compares the BsonBoolean to another BsonValue.
  142. /// </summary>
  143. /// <param name="other">The other BsonValue.</param>
  144. /// <returns>A 32-bit signed integer that indicates whether this BsonBoolean is less than, equal to, or greather than the other BsonValue.</returns>
  145. public override int CompareTo(BsonValue other)
  146. {
  147. if (other == null) { return 1; }
  148. var otherBoolean = other as BsonBoolean;
  149. if (otherBoolean != null)
  150. {
  151. return (_value ? 1 : 0).CompareTo(otherBoolean._value ? 1 : 0);
  152. }
  153. return CompareTypeTo(other);
  154. }
  155. /// <summary>
  156. /// Compares this BsonBoolean to another BsonBoolean.
  157. /// </summary>
  158. /// <param name="rhs">The other BsonBoolean.</param>
  159. /// <returns>True if the two BsonBoolean values are equal.</returns>
  160. public bool Equals(BsonBoolean rhs)
  161. {
  162. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  163. return _value == rhs._value;
  164. }
  165. /// <summary>
  166. /// Compares this BsonBoolean to another object.
  167. /// </summary>
  168. /// <param name="obj">The other object.</param>
  169. /// <returns>True if the other object is a BsonBoolean and equal to this one.</returns>
  170. public override bool Equals(object obj)
  171. {
  172. return Equals(obj as BsonBoolean); // works even if obj is null or of a different type
  173. }
  174. /// <summary>
  175. /// Gets the hash code.
  176. /// </summary>
  177. /// <returns>The hash code.</returns>
  178. public override int GetHashCode()
  179. {
  180. // see Effective Java by Joshua Bloch
  181. int hash = 17;
  182. hash = 37 * hash + BsonType.GetHashCode();
  183. hash = 37 * hash + _value.GetHashCode();
  184. return hash;
  185. }
  186. /// <summary>
  187. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  188. /// </summary>
  189. /// <returns>A Boolean.</returns>
  190. public override bool ToBoolean()
  191. {
  192. return _value;
  193. }
  194. /// <summary>
  195. /// Returns a string representation of the value.
  196. /// </summary>
  197. /// <returns>A string representation of the value.</returns>
  198. public override string ToString()
  199. {
  200. return XmlConvert.ToString(_value);
  201. }
  202. }
  203. }