/* 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; using System.Globalization; namespace MongoDB.Bson { /// /// Represents a BSON double value. /// [Serializable] public class BsonDouble : BsonValue, IComparable, IEquatable { // private fields private double _value; // constructors /// /// Initializes a new instance of the BsonDouble class. /// /// The value. public BsonDouble(double value) : base(BsonType.Double) { _value = value; } // public properties /// /// Gets the BsonDouble as a double. /// [Obsolete("Use Value instead.")] public override object RawValue { get { return _value; } } /// /// Gets the value of this BsonDouble. /// public double Value { get { return _value; } } // public operators /// /// Converts a double to a BsonDouble. /// /// A double. /// A BsonDouble. public static implicit operator BsonDouble(double value) { return new BsonDouble(value); } /// /// Compares two BsonDouble values. /// /// The first BsonDouble. /// The other BsonDouble. /// True if the two BsonDouble values are not equal according to ==. public static bool operator !=(BsonDouble lhs, BsonDouble rhs) { return !(lhs == rhs); } /// /// Compares two BsonDouble values. /// /// The first BsonDouble. /// The other BsonDouble. /// True if the two BsonDouble values are equal according to ==. public static bool operator ==(BsonDouble lhs, BsonDouble rhs) { if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); } return lhs.OperatorEqualsImplementation(rhs); } // public static methods /// /// Creates a new instance of the BsonDouble class. /// /// A double. /// A BsonDouble. [Obsolete("Use new BsonDouble(double value) instead.")] public static BsonDouble Create(double value) { return new BsonDouble(value); } /// /// Creates a new instance of the BsonDouble class. /// /// An object to be mapped to a BsonDouble. /// A BsonDouble. public new static BsonDouble Create(object value) { if (value != null) { return (BsonDouble)BsonTypeMapper.MapToBsonValue(value, BsonType.Double); } else { return null; } } // public methods /// /// Compares this BsonDouble to another BsonDouble. /// /// The other BsonDouble. /// A 32-bit signed integer that indicates whether this BsonDouble is less than, equal to, or greather than the other. public int CompareTo(BsonDouble other) { if (other == null) { return 1; } return _value.CompareTo(other._value); } /// /// Compares the BsonDouble to another BsonValue. /// /// The other BsonValue. /// A 32-bit signed integer that indicates whether this BsonDouble is less than, equal to, or greather than the other BsonValue. public override int CompareTo(BsonValue other) { if (other == null) { return 1; } var otherDouble = other as BsonDouble; if (otherDouble != null) { return _value.CompareTo(otherDouble._value); } var otherInt32 = other as BsonInt32; if (otherInt32 != null) { return _value.CompareTo((double)otherInt32.Value); } var otherInt64 = other as BsonInt64; if (otherInt64 != null) { return _value.CompareTo((double)otherInt64.Value); } return CompareTypeTo(other); } /// /// Compares this BsonDouble to another BsonDouble. /// /// The other BsonDouble. /// True if the two BsonDouble values are equal. public bool Equals(BsonDouble rhs) { if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; } return _value.Equals(rhs._value); // use Equals instead of == so NaN is handled correctly } /// /// Compares this BsonDouble to another object. /// /// The other object. /// True if the other object is a BsonDouble and equal to this one. public override bool Equals(object obj) { return Equals(obj as BsonDouble); // 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; } /// /// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness). /// /// A Boolean. public override bool ToBoolean() { return !(double.IsNaN(_value) || _value == 0.0); } /// /// Converts this BsonValue to a Double. /// /// A Double. public override double ToDouble() { return _value; } /// /// Converts this BsonValue to an Int32. /// /// An Int32. public override int ToInt32() { return (int)_value; } /// /// Converts this BsonValue to an Int64. /// /// An Int32. public override long ToInt64() { return (long)_value; } /// /// Returns a string representation of the value. /// /// A string representation of the value. public override string ToString() { return _value.ToString("R", NumberFormatInfo.InvariantInfo); } // protected methods /// /// Compares this BsonDouble against another BsonValue. /// /// The other BsonValue. /// True if this BsonDouble and the other BsonValue are equal according to ==. protected override bool OperatorEqualsImplementation(BsonValue rhs) { var rhsDouble = rhs as BsonDouble; if (rhsDouble != null) { return _value == rhsDouble._value; // use == instead of Equals so NaN is handled correctly } var rhsInt32 = rhs as BsonInt32; if (rhsInt32 != null) { return _value == (double)rhsInt32.Value; } var rhsInt64 = rhs as BsonInt64; if (rhsInt64 != null) { return _value == (double)rhsInt64.Value; } return this.Equals(rhs); } } }