Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
134 lines (118 sloc) 3.44 KB
/*
* User: scorder
* Date: 7/8/2009
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace MongoDB.Driver
{
/// <summary>
/// Description of Document.
/// </summary>
public class Document : System.Collections.DictionaryBase
{
private List<String> orderedKeys = new List<String>();
public Document(){
}
public Object this[ String key ] {
get{
return Dictionary[key];
}
set{
if(orderedKeys.Contains(key) == false){
orderedKeys.Add(key);
}
Dictionary[key] = value;
}
}
public ICollection Keys {
get {
return(orderedKeys);
}
}
public ICollection Values {
get {
return( Dictionary.Values );
}
}
public void Add( String key, Object value ) {
Dictionary.Add( key, value );
//Relies on ArgumentException from above if key already exists.
orderedKeys.Add(key);
}
public Document Append(String key, Object value){
this.Add(key,value);
return this;
}
public Document Update(Document from){
if(from == null) return this;
foreach(String key in from.Keys){
this[key] = from[key];
}
return this;
}
public bool Contains( String key ) {
return( orderedKeys.Contains( key ) );
}
public void Remove( String key ) {
Dictionary.Remove( key );
orderedKeys.Remove(key);
}
/// <summary>
/// TODO Fix any accidental reordering issues.
/// </summary>
/// <param name="dest"></param>
public void CopyTo(Document dest){
foreach(String key in orderedKeys){
dest[key] = this[key];
}
}
public override bool Equals(object obj)
{
if (obj is Document)
{
Document doc = obj as Document;
if (this.Contains("_id"))
{
if (doc.Contains("_id"))
{
return this["_id"].Equals(doc["_id"]);
}
else
{
return false;
}
}
else if (doc.Contains("_id"))
{
return false;
}
else
{
return base.Equals(doc);
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static bool operator ==(Document a, Document b)
{
if (((object)a) == null && ((object)b) == null) return true;
else if ( ((object)a) != null) return a.Equals(b);
else return false;
}
public static bool operator != (Document a, Document b)
{
if (((object)a) == null && ((object)b) == null) return false;
else if (((object)a) != null) return !a.Equals(b);
else return true;
}
}
}