BsonObjectId.cs 15 KB

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