forked from discord-net/Discord.Net
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add entity equality comparers * Fix namespace #whoops * Add Message comparer. * Add comment explaining the specialized implementation * Remove specialized implementation, as per feedback
- Loading branch information
1 parent
d60da95
commit a255215
Showing
1 changed file
with
45 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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Discord | ||
{ | ||
public static class DiscordComparers | ||
{ | ||
// TODO: simplify with '??=' slated for C# 8.0 | ||
public static IEqualityComparer<IUser> UserComparer => _userComparer ?? (_userComparer = new EntityEqualityComparer<IUser, ulong>()); | ||
public static IEqualityComparer<IGuild> GuildComparer => _guildComparer ?? (_guildComparer = new EntityEqualityComparer<IGuild, ulong>()); | ||
public static IEqualityComparer<IChannel> ChannelComparer => _channelComparer ?? (_channelComparer = new EntityEqualityComparer<IChannel, ulong>()); | ||
public static IEqualityComparer<IRole> RoleComparer => _roleComparer ?? (_roleComparer = new EntityEqualityComparer<IRole, ulong>()); | ||
public static IEqualityComparer<IMessage> MessageComparer => _messageComparer ?? (_messageComparer = new EntityEqualityComparer<IMessage, ulong>()); | ||
|
||
private static IEqualityComparer<IUser> _userComparer; | ||
private static IEqualityComparer<IGuild> _guildComparer; | ||
private static IEqualityComparer<IChannel> _channelComparer; | ||
private static IEqualityComparer<IRole> _roleComparer; | ||
private static IEqualityComparer<IMessage> _messageComparer; | ||
|
||
private sealed class EntityEqualityComparer<TEntity, TId> : EqualityComparer<TEntity> | ||
where TEntity : IEntity<TId> | ||
where TId : IEquatable<TId> | ||
{ | ||
public override bool Equals(TEntity x, TEntity y) | ||
{ | ||
bool xNull = x == null; | ||
bool yNull = y == null; | ||
|
||
if (xNull && yNull) | ||
return true; | ||
|
||
if (xNull ^ yNull) | ||
return false; | ||
|
||
return x.Id.Equals(y.Id); | ||
} | ||
|
||
public override int GetHashCode(TEntity obj) | ||
{ | ||
return obj?.Id.GetHashCode() ?? 0; | ||
} | ||
} | ||
} | ||
} |