/* 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 boolean value.
///
[Serializable]
public class BsonBoolean : BsonValue, IComparable, IEquatable
{
// private static fields
private static BsonBoolean __falseInstance = new BsonBoolean(false);
private static BsonBoolean __trueInstance = new BsonBoolean(true);
// private fields
private bool _value;
// constructors
///
/// Initializes a new instance of the BsonBoolean class.
///
/// The value.
public BsonBoolean(bool value)
: base(BsonType.Boolean)
{
_value = value;
}
// public static properties
///
/// Gets the instance of BsonBoolean that represents false.
///
public static BsonBoolean False
{
get { return __falseInstance; }
}
///
/// Gets the instance of BsonBoolean that represents true.
///
public static BsonBoolean True
{
get { return __trueInstance; }
}
// public properties
///
/// Gets the BsonBoolean as a bool.
///
[Obsolete("Use Value instead.")]
public override object RawValue
{
get { return _value; }
}
///
/// Gets the value of this BsonBoolean.
///
public bool Value
{
get { return _value; }
}
// public operators
///
/// Converts a bool to a BsonBoolean.
///
/// A bool.
/// A BsonBoolean.
public static implicit operator BsonBoolean(bool value)
{
return value ? __trueInstance : __falseInstance;
}
///
/// Compares two BsonBoolean values.
///
/// The first BsonBoolean.
/// The other BsonBoolean.
/// True if the two BsonBoolean values are not equal according to ==.
public static bool operator !=(BsonBoolean lhs, BsonBoolean rhs)
{
return !(lhs == rhs);
}
///
/// Compares two BsonBoolean values.
///
/// The first BsonBoolean.
/// The other BsonBoolean.
/// True if the two BsonBoolean values are equal according to ==.
public static bool operator ==(BsonBoolean lhs, BsonBoolean rhs)
{
if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
return lhs.Equals(rhs);
}
// public static methods
///
/// Returns one of the two possible BsonBoolean values.
///
/// The bool value.
/// The corresponding BsonBoolean value.
[Obsolete("Use implicit conversion to BsonBoolean or new BsonBoolean(bool value) instead.")]
public static BsonBoolean Create(bool value)
{
return value ? __trueInstance : __falseInstance;
}
///
/// Returns one of the two possible BsonBoolean values.
///
/// An object to be mapped to a BsonBoolean.
/// A BsonBoolean or null.
public new static BsonBoolean Create(object value)
{
if (value != null)
{
return (BsonBoolean)BsonTypeMapper.MapToBsonValue(value, BsonType.Boolean);
}
else
{
return null;
}
}
// public methods
///
/// Compares this BsonBoolean to another BsonBoolean.
///
/// The other BsonBoolean.
/// A 32-bit signed integer that indicates whether this BsonBoolean is less than, equal to, or greather than the other.
public int CompareTo(BsonBoolean other)
{
if (other == null) { return 1; }
return (_value ? 1 : 0).CompareTo(other._value ? 1 : 0);
}
///
/// Compares the BsonBoolean to another BsonValue.
///
/// The other BsonValue.
/// A 32-bit signed integer that indicates whether this BsonBoolean is less than, equal to, or greather than the other BsonValue.
public override int CompareTo(BsonValue other)
{
if (other == null) { return 1; }
var otherBoolean = other as BsonBoolean;
if (otherBoolean != null)
{
return (_value ? 1 : 0).CompareTo(otherBoolean._value ? 1 : 0);
}
return CompareTypeTo(other);
}
///
/// Compares this BsonBoolean to another BsonBoolean.
///
/// The other BsonBoolean.
/// True if the two BsonBoolean values are equal.
public bool Equals(BsonBoolean rhs)
{
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
return _value == rhs._value;
}
///
/// Compares this BsonBoolean to another object.
///
/// The other object.
/// True if the other object is a BsonBoolean and equal to this one.
public override bool Equals(object obj)
{
return Equals(obj as BsonBoolean); // 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;
}
///
/// Returns a string representation of the value.
///
/// A string representation of the value.
public override string ToString()
{
return XmlConvert.ToString(_value);
}
}
}