|
5 | 5 |
|
6 | 6 | using System; |
7 | 7 | using System.Collections.Generic; |
8 | | -using NuGet.Shared; |
| 8 | +using System.Linq; |
| 9 | +using NuGet.Versioning; |
9 | 10 |
|
10 | 11 | namespace NuGet.CommandLine.XPlat.Commands.Why |
11 | 12 | { |
12 | 13 | /// <summary> |
13 | 14 | /// Represents a node in the package dependency graph. |
14 | 15 | /// </summary> |
15 | | - internal class DependencyNode |
| 16 | + internal abstract record DependencyNode(string Id, HashSet<DependencyNode> Children) |
16 | 17 | { |
17 | | - public string Id { get; set; } |
18 | | - public string Version { get; set; } |
19 | | - public HashSet<DependencyNode> Children { get; set; } |
| 18 | + public abstract bool Equals(DependencyNode? other); |
20 | 19 |
|
21 | | - public DependencyNode(string id, string version) |
| 20 | + public abstract override int GetHashCode(); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Represents a project node in the dependency graph. |
| 25 | + /// </summary> |
| 26 | + internal record ProjectNode(string Id, HashSet<DependencyNode> Children) |
| 27 | + : DependencyNode(Id, Children) |
| 28 | + { |
| 29 | + public virtual bool Equals(ProjectNode? other) |
22 | 30 | { |
23 | | - Id = id ?? throw new ArgumentNullException(nameof(id)); |
24 | | - Version = version ?? throw new ArgumentNullException(nameof(version)); |
25 | | - Children = new HashSet<DependencyNode>(new DependencyNodeComparer()); |
| 31 | + if (other == null) |
| 32 | + return false; |
| 33 | + |
| 34 | + return string.Equals(Id, other.Id, StringComparison.OrdinalIgnoreCase) |
| 35 | + && Children.SetEquals(other.Children); |
26 | 36 | } |
27 | 37 |
|
28 | 38 | public override int GetHashCode() |
29 | 39 | { |
30 | | - var hashCodeCombiner = new HashCodeCombiner(); |
31 | | - hashCodeCombiner.AddObject(Id); |
32 | | - hashCodeCombiner.AddObject(Version); |
33 | | - hashCodeCombiner.AddUnorderedSequence(Children); |
34 | | - return hashCodeCombiner.CombinedHash; |
| 40 | + var hash = new HashCode(); |
| 41 | + hash.Add(Id, StringComparer.OrdinalIgnoreCase); |
| 42 | + foreach (var child in Children.OrderBy(c => c.Id, StringComparer.OrdinalIgnoreCase)) |
| 43 | + { |
| 44 | + hash.Add(child); |
| 45 | + } |
| 46 | + return hash.ToHashCode(); |
35 | 47 | } |
36 | 48 | } |
37 | 49 |
|
38 | | - internal class DependencyNodeComparer : IEqualityComparer<DependencyNode> |
| 50 | + /// <summary> |
| 51 | + /// Represents a package node in the dependency graph. |
| 52 | + /// </summary> |
| 53 | + internal record PackageNode(string Id, NuGetVersion ResolvedVersion, VersionRange RequestedVersion, HashSet<DependencyNode> Children) |
| 54 | + : DependencyNode(Id, Children) |
39 | 55 | { |
40 | | - public bool Equals(DependencyNode? x, DependencyNode? y) |
| 56 | + public virtual bool Equals(PackageNode? other) |
41 | 57 | { |
42 | | - if (x == null || y == null) |
| 58 | + if (other == null) |
43 | 59 | return false; |
44 | 60 |
|
45 | | - return string.Equals(x.Id, y.Id, StringComparison.CurrentCultureIgnoreCase); |
| 61 | + return string.Equals(Id, other.Id, StringComparison.OrdinalIgnoreCase) |
| 62 | + && ResolvedVersion.Equals(other.ResolvedVersion) |
| 63 | + && RequestedVersion.Equals(other.RequestedVersion) |
| 64 | + && Children.SetEquals(other.Children); |
46 | 65 | } |
47 | 66 |
|
48 | | - public int GetHashCode(DependencyNode obj) |
| 67 | + public override int GetHashCode() |
49 | 68 | { |
50 | | - return obj.Id.GetHashCode(); |
| 69 | + var hash = new HashCode(); |
| 70 | + hash.Add(Id, StringComparer.OrdinalIgnoreCase); |
| 71 | + hash.Add(ResolvedVersion); |
| 72 | + hash.Add(RequestedVersion); |
| 73 | + foreach (var child in Children.OrderBy(c => c.Id, StringComparer.OrdinalIgnoreCase)) |
| 74 | + { |
| 75 | + hash.Add(child); |
| 76 | + } |
| 77 | + return hash.ToHashCode(); |
51 | 78 | } |
52 | 79 | } |
53 | 80 | } |
0 commit comments