using System; using System.Collections.Generic; namespace Discord { /// /// Represents a collection of for various Discord objects. /// public static class DiscordComparers { /// /// Gets an to be used to compare users. /// public static IEqualityComparer UserComparer => _userComparer ??= new EntityEqualityComparer(); /// /// Gets an to be used to compare guilds. /// public static IEqualityComparer GuildComparer => _guildComparer ??= new EntityEqualityComparer(); /// /// Gets an to be used to compare channels. /// public static IEqualityComparer ChannelComparer => _channelComparer ??= new EntityEqualityComparer(); /// /// Gets an to be used to compare roles. /// public static IEqualityComparer RoleComparer => _roleComparer ??= new EntityEqualityComparer(); /// /// Gets an to be used to compare messages. /// public static IEqualityComparer MessageComparer => _messageComparer ??= new EntityEqualityComparer(); private static IEqualityComparer _userComparer; private static IEqualityComparer _guildComparer; private static IEqualityComparer _channelComparer; private static IEqualityComparer _roleComparer; private static IEqualityComparer _messageComparer; private sealed class EntityEqualityComparer : EqualityComparer where TEntity : IEntity where TId : IEquatable { public override bool Equals(TEntity x, TEntity y) { return (x, y) switch { (null, null) => true, (null, _) => false, (_, null) => false, var (l, r) => l.Id.Equals(r.Id) }; } public override int GetHashCode(TEntity obj) { return obj?.Id.GetHashCode() ?? 0; } } } }