/* 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 string value.
///
[Serializable]
public class BsonString : BsonValue, IComparable, IEquatable
{
// private static fields
private static BsonString __emptyInstance = new BsonString("");
// private fields
private string _value;
// constructors
///
/// Initializes a new instance of the BsonString class.
///
/// The value.
public BsonString(string value)
: base(BsonType.String)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_value = value;
}
// public static properties
///
/// Gets an instance of BsonString that represents an empty string.
///
public static BsonString Empty
{
get { return __emptyInstance; }
}
// public properties
///
/// Gets the BsonString as a string.
///
[Obsolete("Use Value instead.")]
public override object RawValue
{
get { return _value; }
}
///
/// Gets the value of this BsonString.
///
public string Value
{
get { return _value; }
}
// public operators
///
/// Converts a string to a BsonString.
///
/// A string.
/// A BsonString.
public static implicit operator BsonString(string value)
{
return new BsonString(value);
}
///
/// Compares two BsonString values.
///
/// The first BsonString.
/// The other BsonString.
/// True if the two BsonString values are not equal according to ==.
public static bool operator !=(BsonString lhs, BsonString rhs)
{
return !(lhs == rhs);
}
///
/// Compares two BsonString values.
///
/// The first BsonString.
/// The other BsonString.
/// True if the two BsonString values are equal according to ==.
public static bool operator ==(BsonString lhs, BsonString rhs)
{
if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
return lhs.Equals(rhs);
}
// public static methods
///
/// Creates a new BsonString.
///
/// An object to be mapped to a BsonString.
/// A BsonString or null.
public new static BsonString Create(object value)
{
if (value != null)
{
return (BsonString)BsonTypeMapper.MapToBsonValue(value, BsonType.String);
}
else
{
return null;
}
}
///
/// Creates a new instance of the BsonString class.
///
/// A string.
/// A BsonString.
[Obsolete("Use new BsonString(string value) instead.")]
public static BsonString Create(string value)
{
if (value != null)
{
// TODO: are there any other commonly used strings worth checking for?
return value == "" ? __emptyInstance : new BsonString(value);
}
else
{
return null;
}
}
// public methods
///
/// Compares this BsonString to another BsonString.
///
/// The other BsonString.
/// A 32-bit signed integer that indicates whether this BsonString is less than, equal to, or greather than the other.
public int CompareTo(BsonString other)
{
if (other == null) { return 1; }
return _value.CompareTo(other.Value);
}
///
/// Compares the BsonString to another BsonValue.
///
/// The other BsonValue.
/// A 32-bit signed integer that indicates whether this BsonString is less than, equal to, or greather than the other BsonValue.
public override int CompareTo(BsonValue other)
{
if (other == null) { return 1; }
var otherString = other as BsonString;
if (otherString != null)
{
return _value.CompareTo(otherString.Value);
}
var otherSymbol = other as BsonSymbol;
if (otherSymbol != null)
{
return _value.CompareTo(otherSymbol.Name);
}
return CompareTypeTo(other);
}
///
/// Compares this BsonString to another BsonString.
///
/// The other BsonString.
/// True if the two BsonString values are equal.
public bool Equals(BsonString rhs)
{
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
return _value == rhs._value;
}
///
/// Compares this BsonString to another object.
///
/// The other object.
/// True if the other object is a BsonString and equal to this one.
public override bool Equals(object obj)
{
return Equals(obj as BsonString); // 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 != "";
}
///
/// Converts this BsonValue to a Double.
///
/// A Double.
public override double ToDouble()
{
return XmlConvert.ToDouble(_value);
}
///
/// Converts this BsonValue to an Int32.
///
/// An Int32.
public override int ToInt32()
{
return XmlConvert.ToInt32(_value);
}
///
/// Converts this BsonValue to an Int64.
///
/// An Int32.
public override long ToInt64()
{
return XmlConvert.ToInt64(_value);
}
///
/// Returns a string representation of the value.
///
/// A string representation of the value.
public override string ToString()
{
return _value;
}
}
}