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 =_userComparer ?? new EntityEqualityComparer(); /// /// Gets an to be used to compare guilds. /// public static IEqualityComparer GuildComparer => _guildComparer = _guildComparer ?? new EntityEqualityComparer(); /// /// Gets an to be used to compare channels. /// public static IEqualityComparer ChannelComparer => _channelComparer = _channelComparer ?? new EntityEqualityComparer(); /// /// Gets an to be used to compare roles. /// public static IEqualityComparer RoleComparer => _roleComparer = _roleComparer ?? new EntityEqualityComparer(); /// /// Gets an to be used to compare messages. /// public static IEqualityComparer MessageComparer => _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) { if (x == null && y == null) return true; if (x == null && y != null) return false; if (x != null && y == null) return false; var tuple = new Tuple(x, y); return tuple.Item1.Id.Equals(tuple.Item2.Id); } public override int GetHashCode(TEntity obj) { return obj?.Id.GetHashCode() ?? 0; } } } }