BsonObjectId.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 a BSON ObjectId value (see also ObjectId).
  20. /// </summary>
  21. [Serializable]
  22. public class BsonObjectId : BsonValue, IComparable<BsonObjectId>, IEquatable<BsonObjectId>
  23. {
  24. // private static fields
  25. private static BsonObjectId __emptyInstance = new BsonObjectId(ObjectId.Empty);
  26. // private fields
  27. private readonly ObjectId _value;
  28. // constructors
  29. /// <summary>
  30. /// Initializes a new instance of the BsonObjectId class.
  31. /// </summary>
  32. /// <param name="value">The value.</param>
  33. public BsonObjectId(ObjectId value)
  34. {
  35. _value = value;
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the BsonObjectId class.
  39. /// </summary>
  40. /// <param name="bytes">The bytes.</param>
  41. [Obsolete("Use new BsonObjectId(ObjectId value) instead.")]
  42. public BsonObjectId(byte[] bytes)
  43. {
  44. _value = new ObjectId(bytes);
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the BsonObjectId class.
  48. /// </summary>
  49. /// <param name="timestamp">The timestamp (expressed as a DateTime).</param>
  50. /// <param name="machine">The machine hash.</param>
  51. /// <param name="pid">The PID.</param>
  52. /// <param name="increment">The increment.</param>
  53. [Obsolete("Use new BsonObjectId(new ObjectId(DateTime timestamp, int machine, short pid, int increment)) instead.")]
  54. public BsonObjectId(DateTime timestamp, int machine, short pid, int increment)
  55. {
  56. _value = new ObjectId(timestamp, machine, pid, increment);
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the BsonObjectId class.
  60. /// </summary>
  61. /// <param name="timestamp">The timestamp.</param>
  62. /// <param name="machine">The machine hash.</param>
  63. /// <param name="pid">The PID.</param>
  64. /// <param name="increment">The increment.</param>
  65. [Obsolete("Use new BsonObjectId(new ObjectId(int timestamp, int machine, short pid, int increment)) instead.")]
  66. public BsonObjectId(int timestamp, int machine, short pid, int increment)
  67. {
  68. _value = new ObjectId(timestamp, machine, pid, increment);
  69. }
  70. /// <summary>
  71. /// Initializes a new instance of the BsonObjectId class.
  72. /// </summary>
  73. /// <param name="value">The value.</param>
  74. [Obsolete("Use new BsonObjectId(new ObjectId(string value)) instead.")]
  75. public BsonObjectId(string value)
  76. {
  77. _value = new ObjectId(value);
  78. }
  79. // public static properties
  80. /// <summary>
  81. /// Gets an instance of BsonObjectId where the value is empty.
  82. /// </summary>
  83. public static BsonObjectId Empty
  84. {
  85. get { return __emptyInstance; }
  86. }
  87. // public properties
  88. /// <summary>
  89. /// Gets the BsonType of this BsonValue.
  90. /// </summary>
  91. public override BsonType BsonType
  92. {
  93. get { return BsonType.ObjectId; }
  94. }
  95. /// <summary>
  96. /// Gets the timestamp.
  97. /// </summary>
  98. [Obsolete("Use Value.Timestamp instead.")]
  99. public int Timestamp
  100. {
  101. get { return _value.Timestamp; }
  102. }
  103. /// <summary>
  104. /// Gets the machine.
  105. /// </summary>
  106. [Obsolete("This property will be removed in a later release.")]
  107. public int Machine
  108. {
  109. get { return _value.Machine; }
  110. }
  111. /// <summary>
  112. /// Gets the PID.
  113. /// </summary>
  114. [Obsolete("This property will be removed in a later release.")]
  115. public short Pid
  116. {
  117. get { return _value.Pid; }
  118. }
  119. /// <summary>
  120. /// Gets the increment.
  121. /// </summary>
  122. [Obsolete("Use Value.Increment instead.")]
  123. public int Increment
  124. {
  125. get { return _value.Increment; }
  126. }
  127. /// <summary>
  128. /// Gets the creation time (derived from the timestamp).
  129. /// </summary>
  130. [Obsolete("Use Value.CreationTime instead.")]
  131. public DateTime CreationTime
  132. {
  133. get { return _value.CreationTime; }
  134. }
  135. /// <summary>
  136. /// Gets the BsonObjectId as an ObjectId.
  137. /// </summary>
  138. [Obsolete("Use Value instead.")]
  139. public override object RawValue
  140. {
  141. get { return _value; }
  142. }
  143. /// <summary>
  144. /// Gets the value of this BsonObjectId.
  145. /// </summary>
  146. public ObjectId Value
  147. {
  148. get { return _value; }
  149. }
  150. // public operators
  151. /// <summary>
  152. /// Converts an ObjectId to a BsonObjectId.
  153. /// </summary>
  154. /// <param name="value">An ObjectId.</param>
  155. /// <returns>A BsonObjectId.</returns>
  156. public static implicit operator BsonObjectId(ObjectId value)
  157. {
  158. return new BsonObjectId(value);
  159. }
  160. /// <summary>
  161. /// Compares two BsonObjectId values.
  162. /// </summary>
  163. /// <param name="lhs">The first BsonObjectId.</param>
  164. /// <param name="rhs">The other BsonObjectId.</param>
  165. /// <returns>True if the two BsonObjectId values are not equal according to ==.</returns>
  166. public static bool operator !=(BsonObjectId lhs, BsonObjectId rhs)
  167. {
  168. return !(lhs == rhs);
  169. }
  170. /// <summary>
  171. /// Compares two BsonObjectId values.
  172. /// </summary>
  173. /// <param name="lhs">The first BsonObjectId.</param>
  174. /// <param name="rhs">The other BsonObjectId.</param>
  175. /// <returns>True if the two BsonObjectId values are equal according to ==.</returns>
  176. public static bool operator ==(BsonObjectId lhs, BsonObjectId rhs)
  177. {
  178. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  179. return lhs.Equals(rhs);
  180. }
  181. // public static methods
  182. /// <summary>
  183. /// Creates a new BsonObjectId.
  184. /// </summary>
  185. /// <param name="value">An object to be mapped to a BsonObjectId.</param>
  186. /// <returns>A BsonObjectId or null.</returns>
  187. public new static BsonObjectId Create(object value)
  188. {
  189. if (value == null)
  190. {
  191. throw new ArgumentNullException("value");
  192. }
  193. return (BsonObjectId)BsonTypeMapper.MapToBsonValue(value, BsonType.ObjectId);
  194. }
  195. /// <summary>
  196. /// Generates a new BsonObjectId with a unique value.
  197. /// </summary>
  198. /// <returns>A BsonObjectId.</returns>
  199. [Obsolete("Use new BsonObjectId(ObjectId.GenerateNewId()) instead.")]
  200. public static BsonObjectId GenerateNewId()
  201. {
  202. return new BsonObjectId(ObjectId.GenerateNewId());
  203. }
  204. /// <summary>
  205. /// Generates a new BsonObjectId with a unique value (with the timestamp component based on a given DateTime).
  206. /// </summary>
  207. /// <param name="timestamp">The timestamp component (expressed as a DateTime).</param>
  208. /// <returns>A BsonObjectId.</returns>
  209. [Obsolete("Use new BsonObjectId(ObjectId.GenerateNewId(DateTime timestamp)) instead.")]
  210. public static BsonObjectId GenerateNewId(DateTime timestamp)
  211. {
  212. return new BsonObjectId(ObjectId.GenerateNewId(timestamp));
  213. }
  214. /// <summary>
  215. /// Generates a new BsonObjectId with a unique value (with the given timestamp).
  216. /// </summary>
  217. /// <param name="timestamp">The timestamp component.</param>
  218. /// <returns>A BsonObjectId.</returns>
  219. [Obsolete("Use new BsonObjectId(ObjectId.GenerateNewId(int timestamp)) instead.")]
  220. public static BsonObjectId GenerateNewId(int timestamp)
  221. {
  222. return new BsonObjectId(ObjectId.GenerateNewId(timestamp));
  223. }
  224. /// <summary>
  225. /// Parses a string and creates a new BsonObjectId.
  226. /// </summary>
  227. /// <param name="s">The string value.</param>
  228. /// <returns>A BsonObjectId.</returns>
  229. [Obsolete("Use new BsonObjectId(ObjectId.Parse(string s)) instead.")]
  230. public static BsonObjectId Parse(string s)
  231. {
  232. return new BsonObjectId(ObjectId.Parse(s));
  233. }
  234. /// <summary>
  235. /// Tries to parse a string and create a new BsonObjectId.
  236. /// </summary>
  237. /// <param name="s">The string value.</param>
  238. /// <param name="value">The new BsonObjectId.</param>
  239. /// <returns>True if the string was parsed successfully.</returns>
  240. [Obsolete("Use ObjectId.TryParse instead.")]
  241. public static bool TryParse(string s, out BsonObjectId value)
  242. {
  243. // don't throw ArgumentNullException if s is null
  244. ObjectId objectId;
  245. if (ObjectId.TryParse(s, out objectId))
  246. {
  247. value = new BsonObjectId(objectId);
  248. return true;
  249. }
  250. else
  251. {
  252. value = null;
  253. return false;
  254. }
  255. }
  256. // public methods
  257. /// <summary>
  258. /// Compares this BsonObjectId to another BsonObjectId.
  259. /// </summary>
  260. /// <param name="other">The other BsonObjectId.</param>
  261. /// <returns>A 32-bit signed integer that indicates whether this BsonObjectId is less than, equal to, or greather than the other.</returns>
  262. public int CompareTo(BsonObjectId other)
  263. {
  264. if (other == null) { return 1; }
  265. return _value.CompareTo(other.Value);
  266. }
  267. /// <summary>
  268. /// Compares the BsonObjectId to another BsonValue.
  269. /// </summary>
  270. /// <param name="other">The other BsonValue.</param>
  271. /// <returns>A 32-bit signed integer that indicates whether this BsonObjectId is less than, equal to, or greather than the other BsonValue.</returns>
  272. public override int CompareTo(BsonValue other)
  273. {
  274. if (other == null) { return 1; }
  275. var otherObjectId = other as BsonObjectId;
  276. if (otherObjectId != null)
  277. {
  278. return _value.CompareTo(otherObjectId.Value);
  279. }
  280. return CompareTypeTo(other);
  281. }
  282. /// <summary>
  283. /// Compares this BsonObjectId to another BsonObjectId.
  284. /// </summary>
  285. /// <param name="rhs">The other BsonObjectId.</param>
  286. /// <returns>True if the two BsonObjectId values are equal.</returns>
  287. public bool Equals(BsonObjectId rhs)
  288. {
  289. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  290. return this.Value == rhs.Value;
  291. }
  292. /// <summary>
  293. /// Compares this BsonObjectId to another object.
  294. /// </summary>
  295. /// <param name="obj">The other object.</param>
  296. /// <returns>True if the other object is a BsonObjectId and equal to this one.</returns>
  297. public override bool Equals(object obj)
  298. {
  299. return Equals(obj as BsonObjectId); // works even if obj is null or of a different type
  300. }
  301. /// <summary>
  302. /// Gets the hash code.
  303. /// </summary>
  304. /// <returns>The hash code.</returns>
  305. public override int GetHashCode()
  306. {
  307. int hash = 17;
  308. hash = 37 * hash + BsonType.GetHashCode();
  309. hash = 37 * hash + _value.GetHashCode();
  310. return hash;
  311. }
  312. /// <summary>
  313. /// Converts the BsonObjectId to a byte array.
  314. /// </summary>
  315. /// <returns>A byte array.</returns>
  316. [Obsolete("Use Value.ToByteArray() instead.")]
  317. public byte[] ToByteArray()
  318. {
  319. return _value.ToByteArray();
  320. }
  321. /// <summary>
  322. /// Returns a string representation of the value.
  323. /// </summary>
  324. /// <returns>A string representation of the value.</returns>
  325. public override string ToString()
  326. {
  327. return _value.ToString();
  328. }
  329. // protected methods
  330. /// <inheritdoc/>
  331. protected override string IConvertibleToStringImplementation(IFormatProvider provider)
  332. {
  333. return _value.ToString();
  334. }
  335. }
  336. }