using System;
using System.Collections.Generic;
namespace Discord
{
///
/// Represents a collection of for various Discord objects.
///
public static class DiscordComparers
{
// TODO: simplify with '??=' slated for C# 8.0
///
/// 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)
{
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;
}
}
}
}