BsonSymbol.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace MongoDB.Bson
  17. {
  18. // TODO: [Serializable] // must have custom deserialization to do SymbolTable lookup
  19. /// <summary>
  20. /// Represents a BSON symbol value.
  21. /// </summary>
  22. public class BsonSymbol : BsonValue, IComparable<BsonSymbol>, IEquatable<BsonSymbol>
  23. {
  24. // private fields
  25. private readonly string _name;
  26. // constructors
  27. // internal because only BsonSymbolTable should call this constructor
  28. internal BsonSymbol(string name)
  29. {
  30. if (name == null)
  31. {
  32. throw new ArgumentNullException("name");
  33. }
  34. _name = name;
  35. }
  36. // public properties
  37. /// <summary>
  38. /// Gets the BsonType of this BsonValue.
  39. /// </summary>
  40. public override BsonType BsonType
  41. {
  42. get { return BsonType.Symbol; }
  43. }
  44. /// <summary>
  45. /// Gets the name of the symbol.
  46. /// </summary>
  47. public string Name
  48. {
  49. get { return _name; }
  50. }
  51. // public operators
  52. /// <summary>
  53. /// Converts a string to a BsonSymbol.
  54. /// </summary>
  55. /// <param name="name">A string.</param>
  56. /// <returns>A BsonSymbol.</returns>
  57. public static implicit operator BsonSymbol(string name)
  58. {
  59. return BsonSymbolTable.Lookup(name);
  60. }
  61. /// <summary>
  62. /// Compares two BsonSymbol values.
  63. /// </summary>
  64. /// <param name="lhs">The first BsonSymbol.</param>
  65. /// <param name="rhs">The other BsonSymbol.</param>
  66. /// <returns>True if the two BsonSymbol values are not equal according to ==.</returns>
  67. public static bool operator !=(BsonSymbol lhs, BsonSymbol rhs)
  68. {
  69. return !(lhs == rhs);
  70. }
  71. /// <summary>
  72. /// Compares two BsonSymbol values.
  73. /// </summary>
  74. /// <param name="lhs">The first BsonSymbol.</param>
  75. /// <param name="rhs">The other BsonSymbol.</param>
  76. /// <returns>True if the two BsonSymbol values are equal according to ==.</returns>
  77. public static bool operator ==(BsonSymbol lhs, BsonSymbol rhs)
  78. {
  79. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  80. return lhs.Equals(rhs);
  81. }
  82. // public static methods
  83. /// <summary>
  84. /// Creates a new BsonSymbol.
  85. /// </summary>
  86. /// <param name="value">An object to be mapped to a BsonSymbol.</param>
  87. /// <returns>A BsonSymbol or null.</returns>
  88. public new static BsonSymbol Create(object value)
  89. {
  90. if (value == null)
  91. {
  92. throw new ArgumentNullException("value");
  93. }
  94. return (BsonSymbol)BsonTypeMapper.MapToBsonValue(value, BsonType.Symbol);
  95. }
  96. // public methods
  97. // note: a BsonSymbol is guaranteed to be unique because it must be looked up in BsonSymbolTable
  98. // therefore the implementations of Equals and GetHashCode are considerably more efficient
  99. /// <summary>
  100. /// Compares this BsonSymbol to another BsonSymbol.
  101. /// </summary>
  102. /// <param name="other">The other BsonSymbol.</param>
  103. /// <returns>A 32-bit signed integer that indicates whether this BsonSymbol is less than, equal to, or greather than the other.</returns>
  104. public int CompareTo(BsonSymbol other)
  105. {
  106. if (other == null) { return 1; }
  107. return _name.CompareTo(other._name);
  108. }
  109. /// <summary>
  110. /// Compares the BsonSymbol to another BsonValue.
  111. /// </summary>
  112. /// <param name="other">The other BsonValue.</param>
  113. /// <returns>A 32-bit signed integer that indicates whether this BsonSymbol is less than, equal to, or greather than the other BsonValue.</returns>
  114. public override int CompareTo(BsonValue other)
  115. {
  116. if (other == null) { return 1; }
  117. var otherSymbol = other as BsonSymbol;
  118. if (otherSymbol != null)
  119. {
  120. return _name.CompareTo(otherSymbol.Name);
  121. }
  122. var otherString = other as BsonString;
  123. if (otherString != null)
  124. {
  125. return _name.CompareTo(otherString.Value);
  126. }
  127. return CompareTypeTo(other);
  128. }
  129. /// <summary>
  130. /// Compares this BsonSymbol to another BsonSymbol.
  131. /// </summary>
  132. /// <param name="rhs">The other BsonSymbol.</param>
  133. /// <returns>True if the two BsonSymbol values are equal.</returns>
  134. public bool Equals(BsonSymbol rhs)
  135. {
  136. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  137. return object.ReferenceEquals(this, rhs); // symbols are guaranteed to be unique
  138. }
  139. /// <summary>
  140. /// Compares this BsonSymbol to another object.
  141. /// </summary>
  142. /// <param name="obj">The other object.</param>
  143. /// <returns>True if the other object is a BsonSymbol and equal to this one.</returns>
  144. public override bool Equals(object obj)
  145. {
  146. return Equals(obj as BsonSymbol); // works even if obj is null or of a different type
  147. }
  148. /// <summary>
  149. /// Gets the hash code.
  150. /// </summary>
  151. /// <returns>The hash code.</returns>
  152. public override int GetHashCode()
  153. {
  154. return _name.GetHashCode();
  155. }
  156. /// <summary>
  157. /// Returns a string representation of the value.
  158. /// </summary>
  159. /// <returns>A string representation of the value.</returns>
  160. public override string ToString()
  161. {
  162. return _name;
  163. }
  164. }
  165. }