BsonTimestamp.cs 6.9 KB

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