Merge pull request #1477 from deadsurgeon42/user-equatable

Implement IEquatable<User> for User class
This commit is contained in:
Ivan 2017-08-05 21:50:26 +02:00 committed by GitHub
commit 9dce68e6e2

View file

@ -363,7 +363,7 @@ namespace TShockAPI.DB
}
/// <summary>A database user.</summary>
public class User
public class User : IEquatable<User>
{
/// <summary>The database ID of the user.</summary>
public int ID { get; set; }
@ -581,6 +581,67 @@ namespace TShockAPI.DB
return HashPassword(Encoding.UTF8.GetBytes(password));
}
#region IEquatable
/// <summary>Indicates whether the current <see cref="User"/> is equal to another <see cref="User"/>.</summary>
/// <returns>true if the <see cref="User"/> is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
/// <param name="other">An <see cref="User"/> to compare with this <see cref="User"/>.</param>
public bool Equals(User other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return ID == other.ID && string.Equals(Name, other.Name);
}
/// <summary>Indicates whether the current <see cref="User"/> is equal to another object.</summary>
/// <returns>true if the <see cref="User"/> is equal to the <paramref name="obj" /> parameter; otherwise, false.</returns>
/// <param name="obj">An <see cref="object"/> to compare with this <see cref="User"/>.</param>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((User)obj);
}
/// <summary>Serves as the hash function. </summary>
/// <returns>A hash code for the current <see cref="User"/>.</returns>
public override int GetHashCode()
{
unchecked
{
return (ID * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
/// <summary>
/// Compares equality of two <see cref="User"/> objects.
/// </summary>
/// <param name="left">Left hand of the comparison.</param>
/// <param name="right">Right hand of the comparison.</param>
/// <returns>true if the <see cref="User"/> objects are equal; otherwise, false.</returns>
public static bool operator ==(User left, User right)
{
return Equals(left, right);
}
/// <summary>
/// Compares equality of two <see cref="User"/> objects.
/// </summary>
/// <param name="left">Left hand of the comparison.</param>
/// <param name="right">Right hand of the comparison.</param>
/// <returns>true if the <see cref="User"/> objects aren't equal; otherwise, false.</returns>
public static bool operator !=(User left, User right)
{
return !Equals(left, right);
}
#endregion
public override string ToString()
{
return Name;
}
}
/// <summary>UserManagerException - An exception generated by the user manager.</summary>
@ -643,4 +704,4 @@ namespace TShockAPI.DB
{
}
}
}
}