BsonUndefined.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace MongoDB.Bson
  17. {
  18. /// <summary>
  19. /// Represents the BSON undefined value.
  20. /// </summary>
  21. #if NET452
  22. [Serializable]
  23. #endif
  24. public class BsonUndefined : BsonValue, IComparable<BsonUndefined>, IEquatable<BsonUndefined>
  25. {
  26. // private static fields
  27. private static BsonUndefined __value = new BsonUndefined();
  28. // constructors
  29. // private so only the singleton instance can be created
  30. private BsonUndefined()
  31. {
  32. }
  33. // public operators
  34. /// <summary>
  35. /// Compares two BsonUndefined values.
  36. /// </summary>
  37. /// <param name="lhs">The first BsonUndefined.</param>
  38. /// <param name="rhs">The other BsonUndefined.</param>
  39. /// <returns>True if the two BsonUndefined values are not equal according to ==.</returns>
  40. public static bool operator !=(BsonUndefined lhs, BsonUndefined rhs)
  41. {
  42. return !(lhs == rhs);
  43. }
  44. /// <summary>
  45. /// Compares two BsonUndefined values.
  46. /// </summary>
  47. /// <param name="lhs">The first BsonUndefined.</param>
  48. /// <param name="rhs">The other BsonUndefined.</param>
  49. /// <returns>True if the two BsonUndefined values are equal according to ==.</returns>
  50. public static bool operator ==(BsonUndefined lhs, BsonUndefined rhs)
  51. {
  52. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  53. return lhs.Equals(rhs);
  54. }
  55. // public static properties
  56. /// <summary>
  57. /// Gets the singleton instance of BsonUndefined.
  58. /// </summary>
  59. public static BsonUndefined Value { get { return __value; } }
  60. // public properties
  61. /// <summary>
  62. /// Gets the BsonType of this BsonValue.
  63. /// </summary>
  64. public override BsonType BsonType
  65. {
  66. get { return BsonType.Undefined; }
  67. }
  68. // public methods
  69. /// <summary>
  70. /// Compares this BsonUndefined to another BsonUndefined.
  71. /// </summary>
  72. /// <param name="other">The other BsonUndefined.</param>
  73. /// <returns>A 32-bit signed integer that indicates whether this BsonUndefined is less than, equal to, or greather than the other.</returns>
  74. public int CompareTo(BsonUndefined other)
  75. {
  76. if (other == null) { return 1; }
  77. return 0; // it's a singleton
  78. }
  79. /// <summary>
  80. /// Compares the BsonUndefined to another BsonValue.
  81. /// </summary>
  82. /// <param name="other">The other BsonValue.</param>
  83. /// <returns>A 32-bit signed integer that indicates whether this BsonUndefined is less than, equal to, or greather than the other BsonValue.</returns>
  84. public override int CompareTo(BsonValue other)
  85. {
  86. if (other == null) { return 1; }
  87. if (other is BsonMinKey) { return 1; }
  88. if (other is BsonUndefined) { return 0; }
  89. return -1;
  90. }
  91. /// <summary>
  92. /// Compares this BsonUndefined to another BsonUndefined.
  93. /// </summary>
  94. /// <param name="rhs">The other BsonUndefined.</param>
  95. /// <returns>True if the two BsonUndefined values are equal.</returns>
  96. public bool Equals(BsonUndefined rhs)
  97. {
  98. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  99. return true; // it's a singleton
  100. }
  101. /// <summary>
  102. /// Compares this BsonUndefined to another object.
  103. /// </summary>
  104. /// <param name="obj">The other object.</param>
  105. /// <returns>True if the other object is a BsonUndefined and equal to this one.</returns>
  106. public override bool Equals(object obj)
  107. {
  108. return Equals(obj as BsonUndefined); // works even if obj is null or of a different type
  109. }
  110. /// <summary>
  111. /// Gets the hash code.
  112. /// </summary>
  113. /// <returns>The hash code.</returns>
  114. public override int GetHashCode()
  115. {
  116. return BsonType.GetHashCode();
  117. }
  118. /// <summary>
  119. /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
  120. /// </summary>
  121. /// <returns>A Boolean.</returns>
  122. public override bool ToBoolean()
  123. {
  124. return false;
  125. }
  126. /// <summary>
  127. /// Returns a string representation of the value.
  128. /// </summary>
  129. /// <returns>A string representation of the value.</returns>
  130. public override string ToString()
  131. {
  132. return "BsonUndefined";
  133. }
  134. }
  135. }