Skip to content

Commit 86a37a8

Browse files
YohDeadfallflobernd
authored andcommitted
Fixed the equality contract on Metrics type (#7733)
1 parent 75fb9c1 commit 86a37a8

File tree

1 file changed

+12
-4
lines changed
  • src/Elastic.Clients.Elasticsearch/Core/Infer/Metric

1 file changed

+12
-4
lines changed

src/Elastic.Clients.Elasticsearch/Core/Infer/Metric/Metrics.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.Linq;
87
using Elastic.Transport;
98

109
namespace Elastic.Clients.Elasticsearch;
@@ -53,8 +52,8 @@ public bool Equals(Metrics other)
5352
{
5453
if (other is null) return false;
5554

56-
// Equality is true when the metrics names in both instances are equal, regardless of their order in the set.
57-
return Values.OrderBy(t => t).SequenceEqual(other.Values.OrderBy(t => t));
55+
// Equality is true when both instances have the same metric names.
56+
return Values.SetEquals(other.Values);
5857
}
5958

6059
string IUrlParameter.GetString(ITransportConfiguration settings) => GetString();
@@ -71,7 +70,16 @@ private string GetString()
7170
}
7271

7372
/// <inheritdoc />
74-
public override int GetHashCode() => Values != null ? Values.GetHashCode() : 0;
73+
public override int GetHashCode()
74+
{
75+
// Lifting the minimal target framework to .NET Standard 2.1
76+
// would be the best solution ever due to the HashCode type.
77+
var hashCode = 0;
78+
foreach (var metric in Values)
79+
hashCode = (hashCode * 397) ^ metric.GetHashCode();
80+
81+
return hashCode;
82+
}
7583

7684
public static bool operator ==(Metrics left, Metrics right) => Equals(left, right);
7785
public static bool operator !=(Metrics left, Metrics right) => !Equals(left, right);

0 commit comments

Comments
 (0)