/* Copyright 2010-2014 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; namespace MongoDB.Bson { /// /// Represents a BSON ObjectId value (see also ObjectId). /// [Serializable] public class BsonObjectId : BsonValue, IComparable, IEquatable { // private static fields private static BsonObjectId __emptyInstance = new BsonObjectId(ObjectId.Empty); // private fields private ObjectId _value; // constructors /// /// Initializes a new instance of the BsonObjectId class. /// /// The value. public BsonObjectId(ObjectId value) : base(BsonType.ObjectId) { _value = value; } /// /// Initializes a new instance of the BsonObjectId class. /// /// The bytes. [Obsolete("Use new BsonObjectId(byte[] bytes) instead.")] public BsonObjectId(byte[] bytes) : base(BsonType.ObjectId) { _value = new ObjectId(bytes); } /// /// Initializes a new instance of the BsonObjectId class. /// /// The timestamp (expressed as a DateTime). /// The machine hash. /// The PID. /// The increment. [Obsolete("Use new BsonObjectId(new ObjectId(DateTime timestamp, int machine, short pid, int increment)) instead.")] public BsonObjectId(DateTime timestamp, int machine, short pid, int increment) : base(BsonType.ObjectId) { _value = new ObjectId(timestamp, machine, pid, increment); } /// /// Initializes a new instance of the BsonObjectId class. /// /// The timestamp. /// The machine hash. /// The PID. /// The increment. [Obsolete("Use new BsonObjectId(new ObjectId(int timestamp, int machine, short pid, int increment)) instead.")] public BsonObjectId(int timestamp, int machine, short pid, int increment) : base(BsonType.ObjectId) { _value = new ObjectId(timestamp, machine, pid, increment); } /// /// Initializes a new instance of the BsonObjectId class. /// /// The value. [Obsolete("Use new BsonObjectId(new ObjectId(string value)) instead.")] public BsonObjectId(string value) : base(BsonType.ObjectId) { _value = new ObjectId(value); } // public static properties /// /// Gets an instance of BsonObjectId where the value is empty. /// public static BsonObjectId Empty { get { return __emptyInstance; } } // public properties /// /// Gets the timestamp. /// [Obsolete("Use Value.Timestamp instead.")] public int Timestamp { get { return _value.Timestamp; } } /// /// Gets the machine. /// [Obsolete("Use Value.Machine instead.")] public int Machine { get { return _value.Machine; } } /// /// Gets the PID. /// [Obsolete("Use Value.Pid instead.")] public short Pid { get { return _value.Pid; } } /// /// Gets the increment. /// [Obsolete("Use Value.Increment instead.")] public int Increment { get { return _value.Increment; } } /// /// Gets the creation time (derived from the timestamp). /// [Obsolete("Use Value.CreationTime instead.")] public DateTime CreationTime { get { return _value.CreationTime; } } /// /// Gets the BsonObjectId as an ObjectId. /// [Obsolete("Use Value instead.")] public override object RawValue { get { return _value; } } /// /// Gets the value of this BsonObjectId. /// public ObjectId Value { get { return _value; } } // public operators /// /// Converts an ObjectId to a BsonObjectId. /// /// An ObjectId. /// A BsonObjectId. public static implicit operator BsonObjectId(ObjectId value) { return new BsonObjectId(value); } /// /// Compares two BsonObjectId values. /// /// The first BsonObjectId. /// The other BsonObjectId. /// True if the two BsonObjectId values are not equal according to ==. public static bool operator !=(BsonObjectId lhs, BsonObjectId rhs) { return !(lhs == rhs); } /// /// Compares two BsonObjectId values. /// /// The first BsonObjectId. /// The other BsonObjectId. /// True if the two BsonObjectId values are equal according to ==. public static bool operator ==(BsonObjectId lhs, BsonObjectId rhs) { if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); } return lhs.Equals(rhs); } // public static methods /// /// Creates a new instance of the BsonObjectId class. /// /// An ObjectId. /// A BsonObjectId. [Obsolete("Use new BsonObjectId(ObjectId value) instead.")] public static BsonObjectId Create(ObjectId value) { return new BsonObjectId(value); } /// /// Creates a new instance of the BsonObjectId class. /// /// A byte array. /// A BsonObjectId. [Obsolete("Use new BsonObjectId(byte[] value) instead.")] public static BsonObjectId Create(byte[] value) { if (value != null) { return new BsonObjectId(value); } else { return null; } } /// /// Creates a new instance of the BsonObjectId class. /// /// The timestamp. /// The machine hash. /// The pid. /// The increment. /// A BsonObjectId. [Obsolete("Use new BsonObjectId(int timestamp, int machine, short pid, int increment) instead.")] public static BsonObjectId Create(int timestamp, int machine, short pid, int increment) { return new BsonObjectId(timestamp, machine, pid, increment); } /// /// Creates a new BsonObjectId. /// /// An object to be mapped to a BsonObjectId. /// A BsonObjectId or null. public new static BsonObjectId Create(object value) { if (value != null) { return (BsonObjectId)BsonTypeMapper.MapToBsonValue(value, BsonType.ObjectId); } else { return null; } } /// /// Creates a new instance of the BsonObjectId class. /// /// A string. /// A BsonObjectId. [Obsolete("Use new BsonObjectId(string value) instead.")] public static BsonObjectId Create(string value) { if (value != null) { return new BsonObjectId(value); } else { return null; } } /// /// Generates a new BsonObjectId with a unique value. /// /// A BsonObjectId. [Obsolete("Use new BsonObjectId(ObjectId.GenerateNewId()) instead.")] public static BsonObjectId GenerateNewId() { return new BsonObjectId(ObjectId.GenerateNewId()); } /// /// Generates a new BsonObjectId with a unique value (with the timestamp component based on a given DateTime). /// /// The timestamp component (expressed as a DateTime). /// A BsonObjectId. [Obsolete("Use new BsonObjectId(ObjectId.GenerateNewId(DateTime timestamp)) instead.")] public static BsonObjectId GenerateNewId(DateTime timestamp) { return new BsonObjectId(ObjectId.GenerateNewId(timestamp)); } /// /// Generates a new BsonObjectId with a unique value (with the given timestamp). /// /// The timestamp component. /// A BsonObjectId. [Obsolete("Use new BsonObjectId(ObjectId.GenerateNewId(int timestamp)) instead.")] public static BsonObjectId GenerateNewId(int timestamp) { return new BsonObjectId(ObjectId.GenerateNewId(timestamp)); } /// /// Parses a string and creates a new BsonObjectId. /// /// The string value. /// A BsonObjectId. [Obsolete("Use new BsonObjectId(ObjectId.Parse(string s)) instead.")] public static BsonObjectId Parse(string s) { return new BsonObjectId(ObjectId.Parse(s)); } /// /// Tries to parse a string and create a new BsonObjectId. /// /// The string value. /// The new BsonObjectId. /// True if the string was parsed successfully. [Obsolete("Use ObjectId.TryParse instead.")] public static bool TryParse(string s, out BsonObjectId value) { // don't throw ArgumentNullException if s is null ObjectId objectId; if (ObjectId.TryParse(s, out objectId)) { value = new BsonObjectId(objectId); return true; } else { value = null; return false; } } // public methods /// /// Compares this BsonObjectId to another BsonObjectId. /// /// The other BsonObjectId. /// A 32-bit signed integer that indicates whether this BsonObjectId is less than, equal to, or greather than the other. public int CompareTo(BsonObjectId other) { if (other == null) { return 1; } return _value.CompareTo(other.Value); } /// /// Compares the BsonObjectId to another BsonValue. /// /// The other BsonValue. /// A 32-bit signed integer that indicates whether this BsonObjectId is less than, equal to, or greather than the other BsonValue. public override int CompareTo(BsonValue other) { if (other == null) { return 1; } var otherObjectId = other as BsonObjectId; if (otherObjectId != null) { return _value.CompareTo(otherObjectId.Value); } return CompareTypeTo(other); } /// /// Compares this BsonObjectId to another BsonObjectId. /// /// The other BsonObjectId. /// True if the two BsonObjectId values are equal. public bool Equals(BsonObjectId rhs) { if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; } return this.Value == rhs.Value; } /// /// Compares this BsonObjectId to another object. /// /// The other object. /// True if the other object is a BsonObjectId and equal to this one. public override bool Equals(object obj) { return Equals(obj as BsonObjectId); // works even if obj is null or of a different type } /// /// Gets the hash code. /// /// The hash code. public override int GetHashCode() { int hash = 17; hash = 37 * hash + BsonType.GetHashCode(); hash = 37 * hash + _value.GetHashCode(); return hash; } /// /// Converts the BsonObjectId to a byte array. /// /// A byte array. [Obsolete("Use Value.ToByteArray() instead.")] public byte[] ToByteArray() { return _value.ToByteArray(); } /// /// Returns a string representation of the value. /// /// A string representation of the value. public override string ToString() { return _value.ToString(); } } }