-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |