Improved equality checks for cache objects

This commit is contained in:
RogueException
2015-10-30 13:10:48 -03:00
parent eb0c14e287
commit 38dd1efa0f
9 changed files with 26 additions and 0 deletions

View File

@@ -207,6 +207,8 @@ namespace Discord
user.UpdateChannelPermissions(this);
}
public override bool Equals(object obj) => obj is Channel && (obj as Channel).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => Name ?? Id;
}
}

View File

@@ -77,5 +77,9 @@ namespace Discord
uint mask = (uint)~(0xFF << bit);
_rawValue = (_rawValue & mask) | ((uint)value << bit);
}
public override bool Equals(object obj) => obj is Color && (obj as Color)._rawValue == _rawValue;
public override int GetHashCode() => _rawValue.GetHashCode();
public override string ToString() => '#' + _rawValue.ToString("X");
}
}

View File

@@ -69,6 +69,8 @@ namespace Discord
_client.GlobalUsers.TryRemove(Id);
}
public override bool Equals(object obj) => obj is GlobalUser && (obj as GlobalUser).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => Id;
}
}

View File

@@ -112,6 +112,8 @@ namespace Discord
CreatedAt = model.CreatedAt.Value;
}
public override bool Equals(object obj) => obj is Invite && (obj as Invite).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => XkcdCode ?? Id;
}
}

View File

@@ -235,6 +235,8 @@ namespace Discord
}
}
public override bool Equals(object obj) => obj is Message && (obj as Message).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => $"{User}: {RawText}";
}
}

View File

@@ -137,6 +137,9 @@ namespace Discord
if (_isLocked)
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() => _rawValue.GetHashCode();
}
public sealed class DualChannelPermissions
@@ -214,5 +217,10 @@ namespace Discord
Deny.SetBit(pos, false);
}
}
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());
}
}

View File

@@ -70,6 +70,8 @@ namespace Discord
member.UpdateServerPermissions();
}
public override bool Equals(object obj) => obj is Role && (obj as Role).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => Name ?? Id;
}
}

View File

@@ -252,6 +252,8 @@ namespace Discord
}
}
public override bool Equals(object obj) => obj is Server && (obj as Server).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => Name ?? Id;
}
}

View File

@@ -370,6 +370,8 @@ namespace Discord
return _roles.ContainsKey(role.Id);
}
public override bool Equals(object obj) => obj is User && (obj as User).Id == Id;
public override int GetHashCode() => Id.GetHashCode();
public override string ToString() => Name ?? Id;
}
}