-
Notifications
You must be signed in to change notification settings - Fork 3
/
DynamicEqualityComparer.cs
50 lines (43 loc) · 2.38 KB
/
DynamicEqualityComparer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
namespace Cuemon.Collections.Generic
{
/// <summary>
/// Provides a factory based way to create and wrap an <see cref="IEqualityComparer{T}"/> implementation.
/// </summary>
public static class DynamicEqualityComparer
{
/// <summary>
/// Creates a dynamic instance of an <see cref="IEqualityComparer{T}"/> implementation wrapping <see cref="IEqualityComparer{T}.GetHashCode(T)"/> through <paramref name="hashCalculator"/> and <see cref="IEqualityComparer{T}.Equals(T,T)"/>. through <paramref name="equalityComparer"/>.
/// </summary>
/// <typeparam name="T">The type of objects to compare.</typeparam>
/// <param name="hashCalculator">The function delegate that calculates a hash code of the specified object and is invoked first.</param>
/// <param name="equalityComparer">The function delegate that determines whether the specified objects are equal. This delegate is invoked second if qualified.</param>
/// <returns>A dynamic instance of <see cref="IEqualityComparer{T}"/> for type <typeparamref name="T"/>.</returns>
/// <remarks>The function delegate, <paramref name="hashCalculator"/> (<see cref="IEqualityComparer{T}.GetHashCode(T)"/>), is evaluated with a conditional-AND before the second function delegate, <paramref name="equalityComparer"/> (<see cref="IEqualityComparer{T}.Equals(T,T)"/>), is ivoked.</remarks>
public static IEqualityComparer<T> Create<T>(Func<T, int> hashCalculator, Func<T, T, bool> equalityComparer)
{
return new DynamicEqualityComparer<T>(hashCalculator, equalityComparer);
}
}
internal class DynamicEqualityComparer<T> : EqualityComparer<T>
{
internal DynamicEqualityComparer(Func<T, int> hashCalculator, Func<T, T, bool> equalityComparer)
{
Validator.ThrowIfNull(equalityComparer);
Validator.ThrowIfNull(hashCalculator);
EqualityComparer = equalityComparer;
HashCalculator = hashCalculator;
}
private Func<T, T, bool> EqualityComparer { get; set; }
private Func<T, int> HashCalculator { get; set; }
public override bool Equals(T x, T y)
{
return EqualityComparer(x, y);
}
public override int GetHashCode(T obj)
{
return HashCalculator(obj);
}
}
}