From 1902740d2f2bb6f5bb5e46c040fef2b126381ca0 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 28 Jan 2021 22:39:40 +0100 Subject: [PATCH] Basic cleanups of IResourceLink equality checkers --- Kyoo.Common/Models/Resources/IResource.cs | 9 +++++---- Kyoo.Common/Utility.cs | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Kyoo.Common/Models/Resources/IResource.cs b/Kyoo.Common/Models/Resources/IResource.cs index 74c8dcf4..dfdfcb63 100644 --- a/Kyoo.Common/Models/Resources/IResource.cs +++ b/Kyoo.Common/Models/Resources/IResource.cs @@ -40,11 +40,12 @@ namespace Kyoo.Models } } - public class LinkComparer : IEqualityComparer> - where T : IResource + public class LinkComparer : IEqualityComparer> + where T : IResourceLink + where T1 : IResource where T2 : IResource { - public bool Equals(IResourceLink x, IResourceLink y) + public bool Equals(T x, T y) { if (ReferenceEquals(x, y)) return true; @@ -58,7 +59,7 @@ namespace Kyoo.Models && Utility.LinkEquals(x.Child, x.ChildID, y.Child, y.ChildID); } - public int GetHashCode(IResourceLink obj) + public int GetHashCode(T obj) { return HashCode.Combine(obj.Parent, obj.ParentID, obj.Child, obj.ChildID); } diff --git a/Kyoo.Common/Utility.cs b/Kyoo.Common/Utility.cs index daeaece8..0c4f8c0d 100644 --- a/Kyoo.Common/Utility.cs +++ b/Kyoo.Common/Utility.cs @@ -356,8 +356,10 @@ namespace Kyoo Type type = GetEnumerableType(eno); if (typeof(IResource).IsAssignableFrom(type)) return ResourceEquals(eno.Cast(), ens.Cast()); + // TODO find a way to run the equality checker with the right check. + // TODO maybe create a GetTypeOfGenericType // if (IsOfGenericType(type, typeof(IResourceLink<,>))) - // return ResourceEquals(eno.Cast>()) + // return ResourceEquals(eno.Cast>()) return RunGenericMethod(typeof(Enumerable), "SequenceEqual", type, first, second); } @@ -393,7 +395,18 @@ namespace Kyoo && first.ID == secondID) return true; return firstID == secondID; - + } + + public static bool LinkEquals([CanBeNull] IEnumerable first, [CanBeNull] IEnumerable second) + where T : IResourceLink + 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()); } public static Expression Convert([CanBeNull] this Expression expr)