2013-10-05 04:49:54 +00:00
|
|
|
using System.Collections;
|
2013-09-11 06:33:47 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Messaging.Commands
|
|
|
|
{
|
|
|
|
public class CommandEqualityComparer : IEqualityComparer<Command>
|
|
|
|
{
|
|
|
|
public static readonly CommandEqualityComparer Instance = new CommandEqualityComparer();
|
|
|
|
|
|
|
|
private CommandEqualityComparer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(Command x, Command y)
|
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
if (x.GetType() != y.GetType())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-15 00:33:19 +00:00
|
|
|
|
2013-09-11 06:33:47 +00:00
|
|
|
var xProperties = x.GetType().GetProperties();
|
|
|
|
var yProperties = y.GetType().GetProperties();
|
|
|
|
|
|
|
|
foreach (var xProperty in xProperties)
|
|
|
|
{
|
|
|
|
if (xProperty.Name == "Id")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:08:53 +00:00
|
|
|
if (xProperty.DeclaringType == typeof(Command))
|
2013-09-18 06:19:36 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-15 00:33:19 +00:00
|
|
|
var yProperty = yProperties.Single(p => p.Name == xProperty.Name);
|
2013-09-11 06:33:47 +00:00
|
|
|
|
|
|
|
var xValue = xProperty.GetValue(x, null);
|
|
|
|
var yValue = yProperty.GetValue(y, null);
|
|
|
|
|
|
|
|
if (xValue == null && yValue == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xValue == null || yValue == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-05 04:49:54 +00:00
|
|
|
if (typeof(IEnumerable).IsAssignableFrom(xProperty.PropertyType))
|
|
|
|
{
|
2018-08-23 21:05:20 +00:00
|
|
|
var xValueCollection = ((IEnumerable)xValue).Cast<object>();
|
|
|
|
var yValueCollection = ((IEnumerable)yValue).Cast<object>();
|
2013-10-05 04:49:54 +00:00
|
|
|
|
2018-08-23 21:05:20 +00:00
|
|
|
var xNotY = xValueCollection.Except(yValueCollection);
|
|
|
|
var yNotX = yValueCollection.Except(xValueCollection);
|
|
|
|
|
|
|
|
if (xNotY.Any() || yNotX.Any())
|
2013-10-05 04:49:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!xValue.Equals(yValue))
|
2013-09-11 06:33:47 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetHashCode(Command obj)
|
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
return obj.GetHashCode();
|
2013-09-11 06:33:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|