Skip to content

Commit

Permalink
Adds KookComparers
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Aug 30, 2024
1 parent 638ce4b commit 8d66ccf
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Kook.Net.Core/Utils/KookComparers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace Kook;

/// <summary>
/// 用于比较 KOOK 各种实体的 <see cref="T:System.Collections.Generic.IEqualityComparer`1"/>。
/// </summary>
public static class KookComparers
{
/// <summary>
/// 获取一个用于比较 <see cref="T:Kook.IUser"/> 的 <see cref="T:System.Collections.Generic.IEqualityComparer`1"/>。
/// </summary>
public static IEqualityComparer<IUser> UserComparer { get; } = new EntityEqualityComparer<IUser, ulong>();

/// <summary>
/// 获取一个用于比较 <see cref="T:Kook.IGuild"/> 的 <see cref="T:System.Collections.Generic.IEqualityComparer`1"/>。
/// </summary>
public static IEqualityComparer<IGuild> GuildComparer { get; } = new EntityEqualityComparer<IGuild, ulong>();

/// <summary>
/// 获取一个用于比较 <see cref="T:Kook.IChannel"/> 的 <see cref="T:System.Collections.Generic.IEqualityComparer`1"/>。
/// </summary>
public static IEqualityComparer<IChannel> ChannelComparer { get; } = new EntityEqualityComparer<IChannel, ulong>();

/// <summary>
/// 获取一个用于比较 <see cref="T:Kook.IRole"/> 的 <see cref="T:System.Collections.Generic.IEqualityComparer`1"/>。
/// </summary>
public static IEqualityComparer<IRole> RoleComparer { get; } = new EntityEqualityComparer<IRole, uint>();

/// <summary>
/// 获取一个用于比较 <see cref="T:Kook.IMessage"/> 的 <see cref="T:System.Collections.Generic.IEqualityComparer`1"/>。
/// </summary>
public static IEqualityComparer<IMessage> MessageComparer { get; } = new EntityEqualityComparer<IMessage, Guid>();

private sealed class EntityEqualityComparer<TEntity, TId> : EqualityComparer<TEntity>
where TEntity : IEntity<TId>
where TId : IEquatable<TId>
{
public override bool Equals(TEntity? x, TEntity? y)
{
return (x, y) switch
{
(null, null) => true,
(null, _) => false,
(_, null) => false,
_ => x.Id.Equals(y.Id)
};
}

public override int GetHashCode(TEntity obj) => obj?.Id.GetHashCode() ?? 0;
}
}

0 comments on commit 8d66ccf

Please sign in to comment.