/* Copyright 2010-2016 MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using MongoDB.Bson.IO; namespace MongoDB.Bson { /// /// Represents a BSON timestamp value. /// #if NET45 [Serializable] #endif public class BsonTimestamp : BsonValue, IComparable, IEquatable { // private fields private readonly long _value; // constructors /// /// Initializes a new instance of the BsonTimestamp class. /// /// The combined timestamp/increment value. public BsonTimestamp(long value) { _value = value; } /// /// Initializes a new instance of the BsonTimestamp class. /// /// The timestamp. /// The increment. public BsonTimestamp(int timestamp, int increment) { _value = (long)(((ulong)(uint)timestamp << 32) | (ulong)(uint)increment); } // public operators /// /// Compares two BsonTimestamp values. /// /// The first BsonTimestamp. /// The other BsonTimestamp. /// True if the two BsonTimestamp values are not equal according to ==. public static bool operator !=(BsonTimestamp lhs, BsonTimestamp rhs) { return !(lhs == rhs); } /// /// Compares two BsonTimestamp values. /// /// The first BsonTimestamp. /// The other BsonTimestamp. /// True if the two BsonTimestamp values are equal according to ==. public static bool operator ==(BsonTimestamp lhs, BsonTimestamp rhs) { if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); } return lhs.Equals(rhs); } // public properties /// /// Gets the BsonType of this BsonValue. /// public override BsonType BsonType { get { return BsonType.Timestamp; } } /// /// Gets the value of this BsonTimestamp. /// public long Value { get { return _value; } } /// /// Gets the increment. /// public int Increment { get { return (int)_value; } } /// /// Gets the timestamp. /// public int Timestamp { get { return (int)(_value >> 32); } } // public static methods /// /// Creates a new BsonTimestamp. /// /// An object to be mapped to a BsonTimestamp. /// A BsonTimestamp or null. public new static BsonTimestamp Create(object value) { if (value == null) { throw new ArgumentNullException("value"); } return (BsonTimestamp)BsonTypeMapper.MapToBsonValue(value, BsonType.Timestamp); } // public methods /// /// Compares this BsonTimestamp to another BsonTimestamp. /// /// The other BsonTimestamp. /// A 32-bit signed integer that indicates whether this BsonTimestamp is less than, equal to, or greather than the other. public int CompareTo(BsonTimestamp other) { if (other == null) { return 1; } return _value.CompareTo(other._value); } /// /// Compares the BsonTimestamp to another BsonValue. /// /// The other BsonValue. /// A 32-bit signed integer that indicates whether this BsonTimestamp is less than, equal to, or greather than the other BsonValue. public override int CompareTo(BsonValue other) { if (other == null) { return 1; } var otherTimestamp = other as BsonTimestamp; if (otherTimestamp != null) { return _value.CompareTo(otherTimestamp._value); } var otherDateTime = other as BsonDateTime; if (otherDateTime != null) { var seconds = (int)(otherDateTime.MillisecondsSinceEpoch / 1000); var otherTimestampValue = ((long)seconds) << 32; return _value.CompareTo(otherTimestampValue); } return CompareTypeTo(other); } /// /// Compares this BsonTimestamp to another BsonTimestamp. /// /// The other BsonTimestamp. /// True if the two BsonTimestamp values are equal. public bool Equals(BsonTimestamp rhs) { if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; } return _value == rhs._value; } /// /// Compares this BsonTimestamp to another object. /// /// The other object. /// True if the other object is a BsonTimestamp and equal to this one. public override bool Equals(object obj) { return Equals(obj as BsonTimestamp); // works even if obj is null or of a different type } /// /// Gets the hash code. /// /// The hash code. public override int GetHashCode() { // see Effective Java by Joshua Bloch int hash = 17; hash = 37 * hash + BsonType.GetHashCode(); hash = 37 * hash + _value.GetHashCode(); return hash; } /// /// Returns a string representation of the value. /// /// A string representation of the value. public override string ToString() { return JsonConvert.ToString(_value); } } }