BsonObjectId.cs 13 KB

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