BsonTimestamp.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. using System.Xml;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents a BSON timestamp value.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonTimestamp : BsonValue, IComparable<BsonTimestamp>, IEquatable<BsonTimestamp>
  24. {
  25. // private fields
  26. private long _value;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the BsonTimestamp class.
  30. /// </summary>
  31. /// <param name="value">The combined timestamp/increment value.</param>
  32. public BsonTimestamp(long value)
  33. : base(BsonType.Timestamp)
  34. {
  35. _value = value;
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the BsonTimestamp class.
  39. /// </summary>
  40. /// <param name="timestamp">The timestamp.</param>
  41. /// <param name="increment">The increment.</param>
  42. public BsonTimestamp(int timestamp, int increment)
  43. : base(BsonType.Timestamp)
  44. {
  45. _value = ((long)timestamp << 32) + increment;
  46. }
  47. // public operators
  48. /// <summary>
  49. /// Compares two BsonTimestamp values.
  50. /// </summary>
  51. /// <param name="lhs">The first BsonTimestamp.</param>
  52. /// <param name="rhs">The other BsonTimestamp.</param>
  53. /// <returns>True if the two BsonTimestamp values are not equal according to ==.</returns>
  54. public static bool operator !=(BsonTimestamp lhs, BsonTimestamp rhs)
  55. {
  56. return !(lhs == rhs);
  57. }
  58. /// <summary>
  59. /// Compares two BsonTimestamp values.
  60. /// </summary>
  61. /// <param name="lhs">The first BsonTimestamp.</param>
  62. /// <param name="rhs">The other BsonTimestamp.</param>
  63. /// <returns>True if the two BsonTimestamp values are equal according to ==.</returns>
  64. public static bool operator ==(BsonTimestamp lhs, BsonTimestamp rhs)
  65. {
  66. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  67. return lhs.Equals(rhs);
  68. }
  69. // public properties
  70. /// <summary>
  71. /// Gets the value of this BsonTimestamp.
  72. /// </summary>
  73. public long Value
  74. {
  75. get { return _value; }
  76. }
  77. /// <summary>
  78. /// Gets the increment.
  79. /// </summary>
  80. public int Increment
  81. {
  82. get { return (int)_value; }
  83. }
  84. /// <summary>
  85. /// Gets the timestamp.
  86. /// </summary>
  87. public int Timestamp
  88. {
  89. get { return (int)(_value >> 32); }
  90. }
  91. // public static methods
  92. /// <summary>
  93. /// Creates a new instance of the BsonTimestamp class.
  94. /// </summary>
  95. /// <param name="value">The combined timestamp/increment value.</param>
  96. /// <returns>A BsonTimestamp.</returns>
  97. [Obsolete("Use new BsonTimestamp(long value) instead.")]
  98. public static BsonTimestamp Create(long value)
  99. {
  100. return new BsonTimestamp(value);
  101. }
  102. /// <summary>
  103. /// Creates a new instance of the BsonTimestamp class.
  104. /// </summary>
  105. /// <param name="timestamp">The timestamp.</param>
  106. /// <param name="increment">The increment.</param>
  107. /// <returns>A BsonTimestamp.</returns>
  108. [Obsolete("Use new BsonTimestamp(int timestamp, int increment) instead.")]
  109. public static BsonTimestamp Create(int timestamp, int increment)
  110. {
  111. return new BsonTimestamp(timestamp, increment);
  112. }
  113. /// <summary>
  114. /// Creates a new BsonTimestamp.
  115. /// </summary>
  116. /// <param name="value">An object to be mapped to a BsonTimestamp.</param>
  117. /// <returns>A BsonTimestamp or null.</returns>
  118. public new static BsonTimestamp Create(object value)
  119. {
  120. if (value != null)
  121. {
  122. return (BsonTimestamp)BsonTypeMapper.MapToBsonValue(value, BsonType.Timestamp);
  123. }
  124. else
  125. {
  126. return null;
  127. }
  128. }
  129. // public methods
  130. /// <summary>
  131. /// Compares this BsonTimestamp to another BsonTimestamp.
  132. /// </summary>
  133. /// <param name="other">The other BsonTimestamp.</param>
  134. /// <returns>A 32-bit signed integer that indicates whether this BsonTimestamp is less than, equal to, or greather than the other.</returns>
  135. public int CompareTo(BsonTimestamp other)
  136. {
  137. if (other == null) { return 1; }
  138. return _value.CompareTo(other._value);
  139. }
  140. /// <summary>
  141. /// Compares the BsonTimestamp to another BsonValue.
  142. /// </summary>
  143. /// <param name="other">The other BsonValue.</param>
  144. /// <returns>A 32-bit signed integer that indicates whether this BsonTimestamp is less than, equal to, or greather than the other BsonValue.</returns>
  145. public override int CompareTo(BsonValue other)
  146. {
  147. if (other == null) { return 1; }
  148. var otherTimestamp = other as BsonTimestamp;
  149. if (otherTimestamp != null)
  150. {
  151. return _value.CompareTo(otherTimestamp._value);
  152. }
  153. var otherDateTime = other as BsonDateTime;
  154. if (otherDateTime != null)
  155. {
  156. var seconds = (int)(otherDateTime.MillisecondsSinceEpoch / 1000);
  157. var otherTimestampValue = ((long)seconds) << 32;
  158. return _value.CompareTo(otherTimestampValue);
  159. }
  160. return CompareTypeTo(other);
  161. }
  162. /// <summary>
  163. /// Compares this BsonTimestamp to another BsonTimestamp.
  164. /// </summary>
  165. /// <param name="rhs">The other BsonTimestamp.</param>
  166. /// <returns>True if the two BsonTimestamp values are equal.</returns>
  167. public bool Equals(BsonTimestamp rhs)
  168. {
  169. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  170. return _value == rhs._value;
  171. }
  172. /// <summary>
  173. /// Compares this BsonTimestamp to another object.
  174. /// </summary>
  175. /// <param name="obj">The other object.</param>
  176. /// <returns>True if the other object is a BsonTimestamp and equal to this one.</returns>
  177. public override bool Equals(object obj)
  178. {
  179. return Equals(obj as BsonTimestamp); // works even if obj is null or of a different type
  180. }
  181. /// <summary>
  182. /// Gets the hash code.
  183. /// </summary>
  184. /// <returns>The hash code.</returns>
  185. public override int GetHashCode()
  186. {
  187. // see Effective Java by Joshua Bloch
  188. int hash = 17;
  189. hash = 37 * hash + BsonType.GetHashCode();
  190. hash = 37 * hash + _value.GetHashCode();
  191. return hash;
  192. }
  193. /// <summary>
  194. /// Returns a string representation of the value.
  195. /// </summary>
  196. /// <returns>A string representation of the value.</returns>
  197. public override string ToString()
  198. {
  199. return XmlConvert.ToString(_value);
  200. }
  201. }
  202. }