Skip to content

Commit

Permalink
Fix equality for Binary.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 22, 2010
1 parent 656d955 commit cf4030b
Showing 1 changed file with 50 additions and 47 deletions.
97 changes: 50 additions & 47 deletions source/MongoDB/Binary.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace MongoDB
{
Expand Down Expand Up @@ -86,21 +87,6 @@ public IEnumerator<byte> GetEnumerator()
yield return b;
}

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name = "other" /> parameter; otherwise, false.
/// </returns>
/// <param name = "other">An object to compare with this object.
/// </param>
public bool Equals(Binary other)
{
if(ReferenceEquals(null, other))
return false;
return ReferenceEquals(this, other) || Equals(other.Bytes, Bytes);
}

/// <summary>
/// Performs an implicit conversion from <see cref="MongoDB.Binary"/> to <see cref="System.Byte"/>.
/// </summary>
Expand All @@ -127,38 +113,6 @@ public bool Equals(Binary other)
return new Binary(bytes);
}

/// <summary>
/// Determines whether the specified <see cref = "T:System.Object" /> is equal to the current <see cref = "T:System.Object" />.
/// </summary>
/// <returns>
/// true if the specified <see cref = "T:System.Object" /> is equal to the current <see cref = "T:System.Object" />; otherwise, false.
/// </returns>
/// <param name = "obj">The <see cref = "T:System.Object" /> to compare with the current <see cref = "T:System.Object" />.
/// </param>
/// <exception cref = "T:System.NullReferenceException">The <paramref name = "obj" /> parameter is null.
/// </exception>
/// <filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if(ReferenceEquals(null, obj))
return false;
if(ReferenceEquals(this, obj))
return true;
return obj.GetType() == typeof(Binary) && Equals((Binary)obj);
}

/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref = "T:System.Object" />.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
return (Bytes != null ? Bytes.GetHashCode() : 0);
}

/// <summary>
/// Implements the operator ==.
/// </summary>
Expand All @@ -181,6 +135,55 @@ public override int GetHashCode()
return !Equals(left, right);
}

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
public bool Equals(Binary other)
{
if(ReferenceEquals(null, other))
return false;
if(ReferenceEquals(this, other))
return true;
return other.Bytes.SequenceEqual(Bytes) && Equals(other.Subtype, Subtype);
}

/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="T:System.NullReferenceException">
/// The <paramref name="obj"/> parameter is null.
/// </exception>
public override bool Equals(object obj)
{
if(ReferenceEquals(null, obj))
return false;
if(ReferenceEquals(this, obj))
return true;
return obj.GetType() == typeof(Binary) && Equals((Binary)obj);
}

/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
unchecked
{
return ((Bytes != null ? Bytes.GetHashCode() : 0)*397) ^ Subtype.GetHashCode();
}
}

/// <summary>
/// Returns a <see cref = "System.String" /> that represents this instance.
/// </summary>
Expand Down

0 comments on commit cf4030b

Please sign in to comment.