BsonSymbol.cs 6.7 KB

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