Basic cleanups of IResourceLink equality checkers

This commit is contained in:
Zoe Roux 2021-01-28 22:39:40 +01:00
parent 079df8a1c7
commit 1902740d2f
2 changed files with 20 additions and 6 deletions

View File

@ -40,11 +40,12 @@ namespace Kyoo.Models
} }
} }
public class LinkComparer<T, T2> : IEqualityComparer<IResourceLink<T, T2>> public class LinkComparer<T, T1, T2> : IEqualityComparer<T>>
where T : IResource where T : IResourceLink<T1, T2>
where T1 : IResource
where T2 : IResource where T2 : IResource
{ {
public bool Equals(IResourceLink<T, T2> x, IResourceLink<T, T2> y) public bool Equals(T x, T y)
{ {
if (ReferenceEquals(x, y)) if (ReferenceEquals(x, y))
return true; return true;
@ -58,7 +59,7 @@ namespace Kyoo.Models
&& Utility.LinkEquals(x.Child, x.ChildID, y.Child, y.ChildID); && Utility.LinkEquals(x.Child, x.ChildID, y.Child, y.ChildID);
} }
public int GetHashCode(IResourceLink<T, T2> obj) public int GetHashCode(T obj)
{ {
return HashCode.Combine(obj.Parent, obj.ParentID, obj.Child, obj.ChildID); return HashCode.Combine(obj.Parent, obj.ParentID, obj.Child, obj.ChildID);
} }

View File

@ -356,6 +356,8 @@ namespace Kyoo
Type type = GetEnumerableType(eno); Type type = GetEnumerableType(eno);
if (typeof(IResource).IsAssignableFrom(type)) if (typeof(IResource).IsAssignableFrom(type))
return ResourceEquals(eno.Cast<IResource>(), ens.Cast<IResource>()); return ResourceEquals(eno.Cast<IResource>(), ens.Cast<IResource>());
// TODO find a way to run the equality checker with the right check.
// TODO maybe create a GetTypeOfGenericType
// if (IsOfGenericType(type, typeof(IResourceLink<,>))) // if (IsOfGenericType(type, typeof(IResourceLink<,>)))
// return ResourceEquals(eno.Cast<IResourceLink<,>>()) // return ResourceEquals(eno.Cast<IResourceLink<,>>())
return RunGenericMethod<bool>(typeof(Enumerable), "SequenceEqual", type, first, second); return RunGenericMethod<bool>(typeof(Enumerable), "SequenceEqual", type, first, second);
@ -393,7 +395,18 @@ namespace Kyoo
&& first.ID == secondID) && first.ID == secondID)
return true; return true;
return firstID == secondID; return firstID == secondID;
}
public static bool LinkEquals<T, T1, T2>([CanBeNull] IEnumerable<T> first, [CanBeNull] IEnumerable<T> second)
where T : IResourceLink<T1, T2>
where T1 : IResource
where T2 : IResource
{
if (ReferenceEquals(first, second))
return true;
if (first == null || second == null)
return false;
return first.SequenceEqual(second, new LinkComparer<T, T1, T2>());
} }
public static Expression<T> Convert<T>([CanBeNull] this Expression expr) public static Expression<T> Convert<T>([CanBeNull] this Expression expr)