Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 0263a15

Browse files
committed
We don't really need to overload all the comparison operators, just Equals
1 parent cf739f4 commit 0263a15

File tree

2 files changed

+16
-58
lines changed

2 files changed

+16
-58
lines changed

src/GitHub.App/Models/RepositoryModel.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,22 @@ public RepositoryModel(string name, UriString cloneUrl, bool isPrivate, bool isF
1717
public IAccount Owner { get; private set; }
1818
public override int GetHashCode()
1919
{
20-
return (Owner != null ? 0 : Owner.GetHashCode()) ^ base.GetHashCode();
20+
return Owner?.GetHashCode() ?? 0 ^ base.GetHashCode();
2121
}
2222

23-
/// <summary>
24-
/// This equality comparison will check if the references are the same,
25-
/// and if they're not, it will explicitely check if the contents of this
26-
/// object are the same. If you only want reference checking, call
27-
/// ReferenceEquals
28-
/// </summary>
29-
public static bool operator ==([AllowNull]RepositoryModel lhs, [AllowNull]RepositoryModel rhs)
23+
public override bool Equals([AllowNull]object obj)
3024
{
31-
if (ReferenceEquals(lhs, rhs))
25+
if (ReferenceEquals(this, obj))
3226
return true;
33-
if ((object)lhs == null || (object)rhs == null)
34-
return false;
35-
return lhs.Owner == rhs.Owner && (lhs as SimpleRepositoryModel) == (rhs as SimpleRepositoryModel);
36-
}
37-
38-
/// <summary>
39-
/// This equality comparison will check if the references are the same,
40-
/// and if they're not, it will explicitely check if the contents of this
41-
/// object are the same. If you only want reference checking, call
42-
/// ReferenceEquals
43-
/// </summary>
44-
public static bool operator !=([AllowNull]RepositoryModel lhs, [AllowNull]RepositoryModel rhs)
45-
{
46-
return !(lhs == rhs);
47-
}
48-
49-
public override bool Equals([AllowNull]object other)
50-
{
51-
return other != null && this == other as RepositoryModel;
27+
var other = obj as RepositoryModel;
28+
return other != null && Equals(Owner, other.Owner) && base.Equals(obj);
5229
}
5330

5431
bool IEquatable<RepositoryModel>.Equals([AllowNull]RepositoryModel other)
5532
{
56-
return other != null && this == other;
33+
if (ReferenceEquals(this, other))
34+
return true;
35+
return other != null && Equals(Owner, other.Owner) && base.Equals(other as SimpleRepositoryModel);
5736
}
5837

5938
internal string DebuggerDisplay

src/GitHub.Exports/Models/SimpleRepositoryModel.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,22 @@ public void SetIcon(bool isPrivate, bool isFork)
3535

3636
public override int GetHashCode()
3737
{
38-
return (Name == null ? 0 : Name.GetHashCode()) ^ (CloneUrl == null ? 0 : CloneUrl.GetHashCode()) ^ (LocalPath == null ? 0 : LocalPath.TrimEnd('\\').ToUpperInvariant().GetHashCode());
38+
return Name?.GetHashCode() ?? 0 ^ CloneUrl?.GetHashCode() ?? 0 ^ LocalPath?.TrimEnd('\\').ToUpperInvariant().GetHashCode() ?? 0;
3939
}
4040

41-
/// <summary>
42-
/// This equality comparison will check if the references are the same,
43-
/// and if they're not, it will explicitely check if the contents of this
44-
/// object are the same. If you only want reference checking, call
45-
/// ReferenceEquals
46-
/// </summary>
47-
public static bool operator==(SimpleRepositoryModel lhs, SimpleRepositoryModel rhs)
41+
public override bool Equals(object obj)
4842
{
49-
if (ReferenceEquals(lhs, rhs))
43+
if (ReferenceEquals(this, obj))
5044
return true;
51-
if ((object)lhs == null || (object)rhs == null)
52-
return false;
53-
return String.Equals(lhs.Name, rhs.Name) && String.Equals(lhs.CloneUrl, rhs.CloneUrl) && String.Equals(lhs.LocalPath?.TrimEnd('\\'), rhs.LocalPath?.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase);
54-
}
55-
56-
/// <summary>
57-
/// This equality comparison will check if the references are the same,
58-
/// and if they're not, it will explicitely check if the contents of this
59-
/// object are the same. If you only want reference checking, call
60-
/// ReferenceEquals
61-
/// </summary>
62-
public static bool operator!=(SimpleRepositoryModel lhs, SimpleRepositoryModel rhs)
63-
{
64-
return !(lhs == rhs);
65-
}
66-
67-
public override bool Equals(object other)
68-
{
69-
return other != null && this == other as SimpleRepositoryModel;
45+
var other = obj as SimpleRepositoryModel;
46+
return other != null && String.Equals(Name, other.Name) && String.Equals(CloneUrl, other.CloneUrl) && String.Equals(LocalPath?.TrimEnd('\\'), other.LocalPath?.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase);
7047
}
7148

7249
bool IEquatable<SimpleRepositoryModel>.Equals(SimpleRepositoryModel other)
7350
{
74-
return other != null && this == other;
51+
if (ReferenceEquals(this, other))
52+
return true;
53+
return other != null && String.Equals(Name, other.Name) && String.Equals(CloneUrl, other.CloneUrl) && String.Equals(LocalPath?.TrimEnd('\\'), other.LocalPath?.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase);
7554
}
7655

7756
internal string DebuggerDisplay

0 commit comments

Comments
 (0)