|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +namespace Microsoft.EntityFrameworkCore.ChangeTracking; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// A <see cref="ValueComparer{T}"/> for lists of primitive items. The list can be typed as <see cref="IEnumerable{T}"/>, |
| 8 | +/// but can only be used with instances that implement <see cref="IList{T}"/>. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// <para> |
| 12 | +/// This comparer should be used for nullable value types. Use <see cref="NullableValueTypeListComparer{TElement}"/> for reference |
| 13 | +/// types and non-nullable value types. |
| 14 | +/// </para> |
| 15 | +/// <para> |
| 16 | +/// See <see href="https://aka.ms/efcore-docs-value-comparers">EF Core value comparers</see> for more information and examples. |
| 17 | +/// </para> |
| 18 | +/// </remarks> |
| 19 | +/// <typeparam name="TElement">The element type.</typeparam> |
| 20 | +public sealed class NullableValueTypeListComparer<TElement> : ValueComparer<IEnumerable<TElement?>> |
| 21 | + where TElement : struct |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Creates a new instance of the list comparer. |
| 25 | + /// </summary> |
| 26 | + /// <param name="elementComparer">The comparer to use for comparing elements.</param> |
| 27 | + public NullableValueTypeListComparer(ValueComparer<TElement> elementComparer) |
| 28 | + : base( |
| 29 | + (a, b) => Compare(a, b, elementComparer), |
| 30 | + o => GetHashCode(o, elementComparer), |
| 31 | + source => Snapshot(source, elementComparer)) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b, ValueComparer<TElement> elementComparer) |
| 36 | + { |
| 37 | + if (ReferenceEquals(a, b)) |
| 38 | + { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + if (a is null) |
| 43 | + { |
| 44 | + return b is null; |
| 45 | + } |
| 46 | + |
| 47 | + if (b is null) |
| 48 | + { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + if (a is IList<TElement?> aList && b is IList<TElement?> bList) |
| 53 | + { |
| 54 | + if (aList.Count != bList.Count) |
| 55 | + { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + for (var i = 0; i < aList.Count; i++) |
| 60 | + { |
| 61 | + var (el1, el2) = (aList[i], bList[i]); |
| 62 | + if (el1 is null) |
| 63 | + { |
| 64 | + if (el2 is null) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + if (el2 is null) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + if (!elementComparer.Equals(el1, el2)) |
| 78 | + { |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + throw new InvalidOperationException( |
| 87 | + CoreStrings.BadListType( |
| 88 | + (a is IList<TElement?> ? b : a).GetType().ShortDisplayName(), |
| 89 | + typeof(NullableValueTypeListComparer<TElement>).ShortDisplayName(), |
| 90 | + typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); |
| 91 | + } |
| 92 | + |
| 93 | + private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TElement> elementComparer) |
| 94 | + { |
| 95 | + var hash = new HashCode(); |
| 96 | + |
| 97 | + foreach (var el in source) |
| 98 | + { |
| 99 | + hash.Add(el == null ? 0 : elementComparer.GetHashCode(el)); |
| 100 | + } |
| 101 | + |
| 102 | + return hash.ToHashCode(); |
| 103 | + } |
| 104 | + |
| 105 | + private static IList<TElement?> Snapshot(IEnumerable<TElement?> source, ValueComparer<TElement> elementComparer) |
| 106 | + { |
| 107 | + if (!(source is IList<TElement?> sourceList)) |
| 108 | + { |
| 109 | + throw new InvalidOperationException( |
| 110 | + CoreStrings.BadListType( |
| 111 | + source.GetType().ShortDisplayName(), |
| 112 | + typeof(NullableValueTypeListComparer<TElement>).ShortDisplayName(), |
| 113 | + typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); |
| 114 | + } |
| 115 | + |
| 116 | + if (sourceList.GetType().IsArray) |
| 117 | + { |
| 118 | + var snapshot = new TElement?[sourceList.Count]; |
| 119 | + |
| 120 | + for (var i = 0; i < sourceList.Count; i++) |
| 121 | + { |
| 122 | + var instance = sourceList[i]; |
| 123 | + snapshot[i] = instance == null ? null : (TElement?)elementComparer.Snapshot(instance); |
| 124 | + } |
| 125 | + |
| 126 | + return snapshot; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + var snapshot = source is List<TElement?> |
| 131 | + ? new List<TElement?>(sourceList.Count) |
| 132 | + : (IList<TElement?>)Activator.CreateInstance(source.GetType())!; |
| 133 | + |
| 134 | + foreach (var e in sourceList) |
| 135 | + { |
| 136 | + snapshot.Add(e == null ? null : (TElement?)elementComparer.Snapshot(e)); |
| 137 | + } |
| 138 | + |
| 139 | + return snapshot; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments