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