/* Copyright 2010-2016 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
{
///
/// Represents a BSON JavaScript value.
///
#if NET45
[Serializable]
#endif
public class BsonJavaScript : BsonValue, IComparable, IEquatable
{
// private fields
private readonly string _code;
// constructors
///
/// Initializes a new instance of the BsonJavaScript class.
///
/// The JavaScript code.
public BsonJavaScript(string code)
{
if (code == null)
{
throw new ArgumentNullException("code");
}
_code = code;
}
// public properties
///
/// Gets the BsonType of this BsonValue.
///
public override BsonType BsonType
{
get { return BsonType.JavaScript; }
}
///
/// Gets the JavaScript code.
///
public string Code
{
get { return _code; }
}
///
/// Compares two BsonJavaScript values.
///
/// The first BsonJavaScript.
/// The other BsonJavaScript.
/// True if the two BsonJavaScript values are not equal according to ==.
public static bool operator !=(BsonJavaScript lhs, BsonJavaScript rhs)
{
return !(lhs == rhs);
}
///
/// Compares two BsonJavaScript values.
///
/// The first BsonJavaScript.
/// The other BsonJavaScript.
/// True if the two BsonJavaScript values are equal according to ==.
public static bool operator ==(BsonJavaScript lhs, BsonJavaScript rhs)
{
if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
return lhs.Equals(rhs);
}
// public operators
///
/// Converts a string to a BsonJavaScript.
///
/// A string.
/// A BsonJavaScript.
public static implicit operator BsonJavaScript(string code)
{
return new BsonJavaScript(code);
}
///
/// Creates a new BsonJavaScript.
///
/// An object to be mapped to a BsonJavaScript.
/// A BsonJavaScript or null.
public new static BsonJavaScript Create(object value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return (BsonJavaScript)BsonTypeMapper.MapToBsonValue(value, BsonType.JavaScript);
}
// public methods
///
/// Compares this BsonJavaScript to another BsonJavaScript.
///
/// The other BsonJavaScript.
/// A 32-bit signed integer that indicates whether this BsonJavaScript is less than, equal to, or greather than the other.
public int CompareTo(BsonJavaScript other)
{
if (other == null) { return 1; }
return _code.CompareTo(other._code);
}
///
/// Compares the BsonJavaScript to another BsonValue.
///
/// The other BsonValue.
/// A 32-bit signed integer that indicates whether this BsonJavaScript is less than, equal to, or greather than the other BsonValue.
public override int CompareTo(BsonValue other)
{
if (other == null) { return 1; }
var otherJavaScript = other as BsonJavaScript;
if (otherJavaScript != null)
{
return CompareTo(otherJavaScript);
}
return CompareTypeTo(other);
}
///
/// Compares this BsonJavaScript to another BsonJavaScript.
///
/// The other BsonJavaScript.
/// True if the two BsonJavaScript values are equal.
public bool Equals(BsonJavaScript rhs)
{
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
return _code == rhs._code;
}
///
/// Compares this BsonJavaScript to another object.
///
/// The other object.
/// True if the other object is a BsonJavaScript and equal to this one.
public override bool Equals(object obj)
{
return Equals(obj as BsonJavaScript); // 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 + _code.GetHashCode();
return hash;
}
///
/// Returns a string representation of the value.
///
/// A string representation of the value.
public override string ToString()
{
return string.Format("new BsonJavaScript(\"{0}\")", _code);
}
}
}