Browse Source

Added proper equality checks to Color, Permissions and StringEnums

pull/22/merge
RogueException 9 years ago
parent
commit
4eb8f4de35
5 changed files with 47 additions and 33 deletions
  1. +11
    -8
      src/Discord.Net/Enums/ChannelType.cs
  2. +7
    -6
      src/Discord.Net/Enums/PermissionTarget.cs
  3. +8
    -6
      src/Discord.Net/Enums/UserStatus.cs
  4. +9
    -5
      src/Discord.Net/Models/Color.cs
  5. +12
    -8
      src/Discord.Net/Models/Permissions.cs

+ 11
- 8
src/Discord.Net/Enums/ChannelType.cs View File

@@ -1,6 +1,8 @@
namespace Discord
using System;

namespace Discord
{
public sealed class ChannelType : StringEnum
public sealed class ChannelType : StringEnum, IEquatable<ChannelType>
{
/// <summary> A text-only channel. </summary>
public static ChannelType Text { get; } = new ChannelType("text");
@@ -25,10 +27,11 @@
}
}

public static implicit operator ChannelType(string value) => FromString(value);
public static bool operator ==(ChannelType a, ChannelType b) => a?.Value == b?.Value;
public static bool operator !=(ChannelType a, ChannelType b) => a?.Value != b?.Value;
public override bool Equals(object obj) => (obj as ChannelType)?.Value == Value;
public override int GetHashCode() => Value.GetHashCode();
}
public static implicit operator ChannelType(string value) => FromString(value);
public static bool operator ==(ChannelType a, ChannelType b) => ((object)a == null && (object)b == null) || (a?.Equals(b) ?? false);
public static bool operator !=(ChannelType a, ChannelType b) => !(a == b);
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => (obj as ChannelType)?.Equals(this) ?? false;
public bool Equals(ChannelType type) => type != null && type.Value == Value;
}
}

+ 7
- 6
src/Discord.Net/Enums/PermissionTarget.cs View File

@@ -25,10 +25,11 @@
}
}

public static implicit operator PermissionTarget(string value) => FromString(value);
public static bool operator ==(PermissionTarget a, PermissionTarget b) => a?.Value == b?.Value;
public static bool operator !=(PermissionTarget a, PermissionTarget b) => a?.Value != b?.Value;
public override bool Equals(object obj) => (obj as PermissionTarget)?.Value == Value;
public override int GetHashCode() => Value.GetHashCode();
}
public static implicit operator PermissionTarget(string value) => FromString(value);
public static bool operator ==(PermissionTarget a, PermissionTarget b) => ((object)a == null && (object)b == null) || (a?.Equals(b) ?? false);
public static bool operator !=(PermissionTarget a, PermissionTarget b) => !(a == b);
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => (obj as PermissionTarget)?.Equals(this) ?? false;
public bool Equals(PermissionTarget type) => type != null && type.Value == Value;
}
}

+ 8
- 6
src/Discord.Net/Enums/UserStatus.cs View File

@@ -29,10 +29,12 @@
}
}

public static implicit operator UserStatus(string value) => FromString(value);
public static bool operator ==(UserStatus a, UserStatus b) => a?.Value == b?.Value;
public static bool operator !=(UserStatus a, UserStatus b) => a?.Value != b?.Value;
public override bool Equals(object obj) => (obj as UserStatus)?.Value == Value;
public override int GetHashCode() => Value.GetHashCode();
}

public static implicit operator UserStatus(string value) => FromString(value);
public static bool operator ==(UserStatus a, UserStatus b) => ((object)a == null && (object)b == null) || (a?.Equals(b) ?? false);
public static bool operator !=(UserStatus a, UserStatus b) => !(a == b);
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => (obj as UserStatus)?.Equals(this) ?? false;
public bool Equals(UserStatus type) => type != null && type.Value == Value;
}
}

+ 9
- 5
src/Discord.Net/Models/Color.cs View File

@@ -2,7 +2,7 @@

namespace Discord
{
public sealed class Color
public sealed class Color : IEquatable<Color>
{
public static readonly Color Default = PresetColor(0);

@@ -77,8 +77,12 @@ namespace Discord
_rawValue = (_rawValue & mask) | ((uint)value << bit);
}

public override bool Equals(object obj) => obj is Color && (obj as Color)._rawValue == _rawValue;
public override int GetHashCode() => unchecked(_rawValue.GetHashCode() + 1678);
public override string ToString() => '#' + _rawValue.ToString("X");
}
public static bool operator ==(Color a, Color b) => ((object)a == null && (object)b == null) || (a?.Equals(b) ?? false);
public static bool operator !=(Color a, Color b) => !(a == b);
public override int GetHashCode() => _rawValue.GetHashCode();
public override bool Equals(object obj) => (obj as Color)?.Equals(this) ?? false;
public bool Equals(Color color) => color != null && color._rawValue == _rawValue;

public override string ToString() => '#' + _rawValue.ToString("X");
}
}

+ 12
- 8
src/Discord.Net/Models/Permissions.cs View File

@@ -77,7 +77,7 @@ namespace Discord
public bool ManageChannel { get { return GetBit(PermissionsBits.ManageChannel); } set { SetBit(PermissionsBits.ManageChannel, value); } }
}

public abstract class Permissions
public abstract class Permissions : IEquatable<Permissions>
{
private bool _isLocked;
private uint _rawValue;
@@ -144,8 +144,11 @@ namespace Discord
throw new InvalidOperationException("Unable to edit cached permissions directly, use Copy() to make an editable copy.");
}

public override bool Equals(object obj) => obj is Permissions && (obj as Permissions)._rawValue == _rawValue;
public override int GetHashCode() => unchecked(_rawValue.GetHashCode() + 393);
public static bool operator ==(Permissions a, Permissions b) => ((object)a == null && (object)b == null) || (a?.Equals(b) ?? false);
public static bool operator !=(Permissions a, Permissions b) => !(a == b);
public override int GetHashCode() => _rawValue.GetHashCode();
public override bool Equals(object obj) => (obj as Permissions)?.Equals(this) ?? false;
public bool Equals(Permissions permission) => permission?._rawValue == _rawValue;
}

public sealed class DualChannelPermissions
@@ -231,9 +234,10 @@ namespace Discord
}
public DualChannelPermissions Copy() => new DualChannelPermissions(Allow.RawValue, Deny.RawValue);

public override bool Equals(object obj) => obj is DualChannelPermissions &&
(obj as DualChannelPermissions).Allow.Equals(Allow) &&
(obj as DualChannelPermissions).Deny.Equals(Deny);
public override int GetHashCode() => unchecked(Allow.GetHashCode() + Deny.GetHashCode() + 1724);
}
public static bool operator ==(DualChannelPermissions a, DualChannelPermissions b) => ((object)a == null && (object)b == null) || (a?.Equals(b) ?? false);
public static bool operator !=(DualChannelPermissions a, DualChannelPermissions b) => !(a == b);
public override int GetHashCode() => Allow.GetHashCode() ^ Deny.GetHashCode();
public override bool Equals(object obj) => (obj as DualChannelPermissions)?.Equals(this) ?? false;
public bool Equals(DualChannelPermissions permission) => permission != null && permission.Allow == Allow && permission.Deny == Deny;
}
}

Loading…
Cancel
Save