1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 16:02:55 +08:00

Allow IRulesetInfos of same type to be comparable

At first I was planning on making `CompareTo` implemented at
`IRulesetInfo` itself and shared across classes, but turns out it only
implements it explicitly and not allow direct `IRulesetInfo.Equals`
calls.

It messed with my head enough that I decided to just let each class have
its own implementation and only allow same type.
This commit is contained in:
Salman Ahmed 2022-02-11 04:00:40 +03:00
parent 481003fe38
commit 9574bc1382
2 changed files with 11 additions and 3 deletions

View File

@ -11,7 +11,7 @@ namespace osu.Game.Rulesets
/// <summary>
/// A representation of a ruleset's metadata.
/// </summary>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>, IComparable<RulesetInfo>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>, IComparable<IRulesetInfo>
{
/// <summary>
/// The user-exposed name of this ruleset.

View File

@ -12,7 +12,7 @@ namespace osu.Game.Rulesets
{
[ExcludeFromDynamicCompile]
[MapTo("Ruleset")]
public class RulesetInfo : RealmObject, IEquatable<RulesetInfo>, IRulesetInfo
public class RulesetInfo : RealmObject, IEquatable<RulesetInfo>, IComparable<RulesetInfo>, IRulesetInfo
{
[PrimaryKey]
public string ShortName { get; set; } = string.Empty;
@ -47,7 +47,7 @@ namespace osu.Game.Rulesets
return ShortName == other.ShortName;
}
public bool Equals(IRulesetInfo? other) => other is RulesetInfo b && Equals(b);
public bool Equals(IRulesetInfo? other) => other is RulesetInfo r && Equals(r);
public int CompareTo(RulesetInfo other)
{
@ -63,6 +63,14 @@ namespace osu.Game.Rulesets
return string.Compare(ShortName, other.ShortName, StringComparison.Ordinal);
}
public int CompareTo(IRulesetInfo other)
{
if (!(other is RulesetInfo ruleset))
throw new ArgumentException($@"Object is not of type {nameof(RulesetInfo)}.", nameof(other));
return CompareTo(ruleset);
}
public override int GetHashCode()
{
// Importantly, ignore the underlying realm hash code, as it will usually not match.