BsonMaxKey.cs 4.7 KB

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