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