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