PageRenderTime 3146ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/FireEngine.Net/Bson/ObjectModel/BsonObjectId.cs

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