| | | 1 | | namespace Elsa.Workflows.Helpers; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A utility that compares two collections and returns a set of added, removed and unchanged items. |
| | | 5 | | /// </summary> |
| | | 6 | | public class Diff<T> |
| | | 7 | | { |
| | 959 | 8 | | internal Diff(ICollection<T> added, ICollection<T> removed, ICollection<T> unchanged) |
| | | 9 | | { |
| | 959 | 10 | | Added = added; |
| | 959 | 11 | | Removed = removed; |
| | 959 | 12 | | Unchanged = unchanged; |
| | 959 | 13 | | } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// The added items. |
| | | 17 | | /// </summary> |
| | 1917 | 18 | | public ICollection<T> Added { get; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The removed items. |
| | | 22 | | /// </summary> |
| | 1917 | 23 | | public ICollection<T> Removed { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The unchanged items. |
| | | 27 | | /// </summary> |
| | 958 | 28 | | public ICollection<T> Unchanged { get; } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// A factory class to construct new <see cref="Diff{T}"/> objects. |
| | | 33 | | /// </summary> |
| | | 34 | | public static class Diff |
| | | 35 | | { |
| | | 36 | | /// <summary> |
| | | 37 | | /// Creates a new diff. |
| | | 38 | | /// </summary> |
| | | 39 | | public static Diff<T> From<T>(ICollection<T> added, ICollection<T> removed, ICollection<T> unchanged) => new(added, |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Returns an empty diff. |
| | | 43 | | /// </summary> |
| | | 44 | | public static Diff<T> Empty<T>() => new(Array.Empty<T>(), Array.Empty<T>(), Array.Empty<T>()); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Create a diff between two sets. |
| | | 48 | | /// </summary> |
| | | 49 | | public static Diff<T> For<T>(ICollection<T> firstSet, ICollection<T> secondSet, IEqualityComparer<T>? comparer = def |
| | | 50 | | { |
| | | 51 | | var removed = firstSet.Except(secondSet, comparer).ToList(); |
| | | 52 | | var added = secondSet.Except(firstSet, comparer).ToList(); |
| | | 53 | | var unchanged = firstSet.Intersect(secondSet, comparer).ToList(); |
| | | 54 | | |
| | | 55 | | return new Diff<T>(added, removed, unchanged); |
| | | 56 | | } |
| | | 57 | | } |