/* 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;
namespace MongoDB.Bson
{
///
/// Represents a BSON element.
///
[Serializable]
public class BsonElement : IComparable, IEquatable
{
// private fields
private string _name;
private BsonValue _value;
// constructors
// NOTE: for every public BsonElement constructor there is a matching constructor, Add and Set method in BsonDocument
// used when cloning an existing element, caller will set name and value
private BsonElement()
{
}
///
/// Initializes a new instance of the BsonElement class.
///
/// The name of the element.
/// The value of the element.
public BsonElement(string name, BsonValue value)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
if (value == null)
{
throw new ArgumentNullException("value");
}
ValidateElementName(name);
_name = name;
_value = value;
}
// public properties
///
/// Gets the name of the element.
///
public string Name
{
get { return _name; }
}
///
/// Gets or sets the value of the element.
///
public BsonValue Value
{
get { return _value; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_value = value;
}
}
// public operators
///
/// Compares two BsonElements.
///
/// The first BsonElement.
/// The other BsonElement.
/// True if the two BsonElements are equal (or both null).
public static bool operator ==(BsonElement lhs, BsonElement rhs)
{
return object.Equals(lhs, rhs);
}
///
/// Compares two BsonElements.
///
/// The first BsonElement.
/// The other BsonElement.
/// True if the two BsonElements are not equal (or one is null and the other is not).
public static bool operator !=(BsonElement lhs, BsonElement rhs)
{
return !(lhs == rhs);
}
// public static methods
///
/// Creates a new instance of the BsonElement class.
///
/// Whether to create the BsonElement or return null.
/// The name of the element.
/// The value of the element.
/// A BsonElement or null.
[Obsolete("Use new BsonElement(string name, BsonValue value) instead.")]
public static BsonElement Create(bool condition, string name, BsonValue value)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
if (condition && value != null)
{
return new BsonElement(name, value);
}
else
{
return null;
}
}
///
/// Creates a new instance of the BsonElement class.
///
/// The name of the element.
/// The value of the element.
/// A BsonElement or null.
[Obsolete("Use new BsonElement(string name, BsonValue value) instead.")]
public static BsonElement Create(string name, BsonValue value)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
if (value != null)
{
return new BsonElement(name, value);
}
else
{
return null;
}
}
// private static methods
private static void ValidateElementName(string name)
{
if (name.IndexOf('\0') >= 0)
{
throw new ArgumentException("Element name cannot contain null (0x00) characters");
}
}
// public methods
///
/// Creates a shallow clone of the element (see also DeepClone).
///
/// A shallow clone of the element.
public BsonElement Clone()
{
var clone = new BsonElement();
clone._name = _name;
clone._value = _value;
return clone;
}
///
/// Creates a deep clone of the element (see also Clone).
///
/// A deep clone of the element.
public BsonElement DeepClone()
{
var clone = new BsonElement();
clone._name = _name;
clone._value = _value.DeepClone();
return clone;
}
///
/// Compares this BsonElement to another BsonElement.
///
/// The other BsonElement.
/// A 32-bit signed integer that indicates whether this BsonElement is less than, equal to, or greather than the other.
public int CompareTo(BsonElement other)
{
if (other == null) { return 1; }
int r = _name.CompareTo(other._name);
if (r != 0) { return r; }
return _value.CompareTo(other._value);
}
///
/// Compares this BsonElement to another BsonElement.
///
/// The other BsonElement.
/// True if the two BsonElement values are equal.
public bool Equals(BsonElement rhs)
{
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
return _name == rhs._name && _value == rhs._value;
}
///
/// Compares this BsonElement to another object.
///
/// The other object.
/// True if the other object is a BsonElement and equal to this one.
public override bool Equals(object obj)
{
return Equals(obj as BsonElement); // 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 + _name.GetHashCode();
hash = 37 * hash + _value.GetHashCode();
return hash;
}
///
/// Returns a string representation of the value.
///
/// A string representation of the value.
public override string ToString()
{
return string.Format("{0}={1}", _name, _value);
}
}
}