BsonTimestamp.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright 2010-2016 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. #if NET45
  23. [Serializable]
  24. #endif
  25. public class BsonTimestamp : BsonValue, IComparable<BsonTimestamp>, IEquatable<BsonTimestamp>
  26. {
  27. // private fields
  28. private readonly long _value;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonTimestamp class.
  32. /// </summary>
  33. /// <param name="value">The combined timestamp/increment value.</param>
  34. public BsonTimestamp(long value)
  35. {
  36. _value = value;
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the BsonTimestamp class.
  40. /// </summary>
  41. /// <param name="timestamp">The timestamp.</param>
  42. /// <param name="increment">The increment.</param>
  43. public BsonTimestamp(int timestamp, int increment)
  44. {
  45. _value = (long)(((ulong)(uint)timestamp << 32) | (ulong)(uint)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 BsonType of this BsonValue.
  72. /// </summary>
  73. public override BsonType BsonType
  74. {
  75. get { return BsonType.Timestamp; }
  76. }
  77. /// <summary>
  78. /// Gets the value of this BsonTimestamp.
  79. /// </summary>
  80. public long Value
  81. {
  82. get { return _value; }
  83. }
  84. /// <summary>
  85. /// Gets the increment.
  86. /// </summary>
  87. public int Increment
  88. {
  89. get { return (int)_value; }
  90. }
  91. /// <summary>
  92. /// Gets the timestamp.
  93. /// </summary>
  94. public int Timestamp
  95. {
  96. get { return (int)(_value >> 32); }
  97. }
  98. // public static methods
  99. /// <summary>
  100. /// Creates a new BsonTimestamp.
  101. /// </summary>
  102. /// <param name="value">An object to be mapped to a BsonTimestamp.</param>
  103. /// <returns>A BsonTimestamp or null.</returns>
  104. public new static BsonTimestamp Create(object value)
  105. {
  106. if (value == null)
  107. {
  108. throw new ArgumentNullException("value");
  109. }
  110. return (BsonTimestamp)BsonTypeMapper.MapToBsonValue(value, BsonType.Timestamp);
  111. }
  112. // public methods
  113. /// <summary>
  114. /// Compares this BsonTimestamp to another BsonTimestamp.
  115. /// </summary>
  116. /// <param name="other">The other BsonTimestamp.</param>
  117. /// <returns>A 32-bit signed integer that indicates whether this BsonTimestamp is less than, equal to, or greather than the other.</returns>
  118. public int CompareTo(BsonTimestamp other)
  119. {
  120. if (other == null) { return 1; }
  121. return _value.CompareTo(other._value);
  122. }
  123. /// <summary>
  124. /// Compares the BsonTimestamp to another BsonValue.
  125. /// </summary>
  126. /// <param name="other">The other BsonValue.</param>
  127. /// <returns>A 32-bit signed integer that indicates whether this BsonTimestamp is less than, equal to, or greather than the other BsonValue.</returns>
  128. public override int CompareTo(BsonValue other)
  129. {
  130. if (other == null) { return 1; }
  131. var otherTimestamp = other as BsonTimestamp;
  132. if (otherTimestamp != null)
  133. {
  134. return _value.CompareTo(otherTimestamp._value);
  135. }
  136. var otherDateTime = other as BsonDateTime;
  137. if (otherDateTime != null)
  138. {
  139. var seconds = (int)(otherDateTime.MillisecondsSinceEpoch / 1000);
  140. var otherTimestampValue = ((long)seconds) << 32;
  141. return _value.CompareTo(otherTimestampValue);
  142. }
  143. return CompareTypeTo(other);
  144. }
  145. /// <summary>
  146. /// Compares this BsonTimestamp to another BsonTimestamp.
  147. /// </summary>
  148. /// <param name="rhs">The other BsonTimestamp.</param>
  149. /// <returns>True if the two BsonTimestamp values are equal.</returns>
  150. public bool Equals(BsonTimestamp rhs)
  151. {
  152. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  153. return _value == rhs._value;
  154. }
  155. /// <summary>
  156. /// Compares this BsonTimestamp to another object.
  157. /// </summary>
  158. /// <param name="obj">The other object.</param>
  159. /// <returns>True if the other object is a BsonTimestamp and equal to this one.</returns>
  160. public override bool Equals(object obj)
  161. {
  162. return Equals(obj as BsonTimestamp); // works even if obj is null or of a different type
  163. }
  164. /// <summary>
  165. /// Gets the hash code.
  166. /// </summary>
  167. /// <returns>The hash code.</returns>
  168. public override int GetHashCode()
  169. {
  170. // see Effective Java by Joshua Bloch
  171. int hash = 17;
  172. hash = 37 * hash + BsonType.GetHashCode();
  173. hash = 37 * hash + _value.GetHashCode();
  174. return hash;
  175. }
  176. /// <summary>
  177. /// Returns a string representation of the value.
  178. /// </summary>
  179. /// <returns>A string representation of the value.</returns>
  180. public override string ToString()
  181. {
  182. return JsonConvert.ToString(_value);
  183. }
  184. }
  185. }