Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit da286ca

Browse files
committed
Implementing equality ops in all structs to avoid the default reflection implementation
1 parent d9fde96 commit da286ca

14 files changed

+691
-4
lines changed

src/GitHub.Api/Authentication/Keychain.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,48 @@ public struct Connection
1010
{
1111
public UriString Host;
1212
public string Username;
13+
14+
public override int GetHashCode()
15+
{
16+
int hash = 17;
17+
hash = hash * 23 + (Host?.GetHashCode() ?? 0);
18+
hash = hash * 23 + (Username?.GetHashCode() ?? 0);
19+
return hash;
20+
}
21+
22+
public override bool Equals(object other)
23+
{
24+
if (other is Connection)
25+
return Equals((Connection)other);
26+
return false;
27+
}
28+
29+
public bool Equals(Connection other)
30+
{
31+
return
32+
object.Equals(Host, other.Host) &&
33+
String.Equals(Username, other.Username)
34+
;
35+
}
36+
37+
public static bool operator ==(Connection lhs, Connection rhs)
38+
{
39+
// If both are null, or both are same instance, return true.
40+
if (ReferenceEquals(lhs, rhs))
41+
return true;
42+
43+
// If one is null, but not both, return false.
44+
if (((object)lhs == null) || ((object)rhs == null))
45+
return false;
46+
47+
// Return true if the fields match:
48+
return lhs.Equals(rhs);
49+
}
50+
51+
public static bool operator !=(Connection lhs, Connection rhs)
52+
{
53+
return !(lhs == rhs);
54+
}
1355
}
1456

1557
class ConnectionCacheItem

src/GitHub.Api/Extensions/StringExtensions.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,45 @@ public struct StringResult
156156
public string Chunk;
157157
public int Start;
158158
public int End;
159+
160+
public override int GetHashCode()
161+
{
162+
int hash = 17;
163+
hash = hash * 23 + (Chunk?.GetHashCode() ?? 0);
164+
hash = hash * 23 + Start.GetHashCode();
165+
hash = hash * 23 + End.GetHashCode();
166+
return hash;
167+
}
168+
169+
public override bool Equals(object other)
170+
{
171+
if (other is StringResult)
172+
return Equals((StringResult)other);
173+
return false;
174+
}
175+
176+
public bool Equals(StringResult other)
177+
{
178+
return String.Equals(Chunk, other.Chunk) && Start == other.Start && End == other.End;
179+
}
180+
181+
public static bool operator ==(StringResult lhs, StringResult rhs)
182+
{
183+
// If both are null, or both are same instance, return true.
184+
if (ReferenceEquals(lhs, rhs))
185+
return true;
186+
187+
// If one is null, but not both, return false.
188+
if (((object)lhs == null) || ((object)rhs == null))
189+
return false;
190+
191+
// Return true if the fields match:
192+
return lhs.Equals(rhs);
193+
}
194+
195+
public static bool operator !=(StringResult lhs, StringResult rhs)
196+
{
197+
return !(lhs == rhs);
198+
}
159199
}
160200
}

src/GitHub.Api/Git/GitAheadBehindStatus.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,45 @@ public GitAheadBehindStatus(int ahead, int behind)
1616
this.behind = behind;
1717
}
1818

19+
public override int GetHashCode()
20+
{
21+
int hash = 17;
22+
hash = hash * 23 + ahead.GetHashCode();
23+
hash = hash * 23 + behind.GetHashCode();
24+
return hash;
25+
}
26+
27+
public override bool Equals(object other)
28+
{
29+
if (other is GitAheadBehindStatus)
30+
return Equals((GitAheadBehindStatus)other);
31+
return false;
32+
}
33+
34+
public bool Equals(GitAheadBehindStatus other)
35+
{
36+
return ahead == other.ahead && behind == other.behind;
37+
}
38+
39+
public static bool operator ==(GitAheadBehindStatus lhs, GitAheadBehindStatus rhs)
40+
{
41+
// If both are null, or both are same instance, return true.
42+
if (ReferenceEquals(lhs, rhs))
43+
return true;
44+
45+
// If one is null, but not both, return false.
46+
if (((object)lhs == null) || ((object)rhs == null))
47+
return false;
48+
49+
// Return true if the fields match:
50+
return lhs.Equals(rhs);
51+
}
52+
53+
public static bool operator !=(GitAheadBehindStatus lhs, GitAheadBehindStatus rhs)
54+
{
55+
return !(lhs == rhs);
56+
}
57+
1958
public int Ahead => ahead;
2059

2160
public int Behind => behind;

src/GitHub.Api/Git/GitBranch.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,49 @@ public GitBranch(string name, string tracking, bool active)
2020
this.isActive = active;
2121
}
2222

23+
public override int GetHashCode()
24+
{
25+
int hash = 17;
26+
hash = hash * 23 + (name?.GetHashCode() ?? 0);
27+
hash = hash * 23 + (tracking?.GetHashCode() ?? 0);
28+
hash = hash * 23 + isActive.GetHashCode();
29+
return hash;
30+
}
31+
32+
public override bool Equals(object other)
33+
{
34+
if (other is GitBranch)
35+
return Equals((GitBranch)other);
36+
return false;
37+
}
38+
39+
public bool Equals(GitBranch other)
40+
{
41+
return
42+
String.Equals(name, other.name) &&
43+
String.Equals(tracking, other.tracking) &&
44+
isActive == other.isActive;
45+
}
46+
47+
public static bool operator ==(GitBranch lhs, GitBranch rhs)
48+
{
49+
// If both are null, or both are same instance, return true.
50+
if (ReferenceEquals(lhs, rhs))
51+
return true;
52+
53+
// If one is null, but not both, return false.
54+
if (((object)lhs == null) || ((object)rhs == null))
55+
return false;
56+
57+
// Return true if the fields match:
58+
return lhs.Equals(rhs);
59+
}
60+
61+
public static bool operator !=(GitBranch lhs, GitBranch rhs)
62+
{
63+
return !(lhs == rhs);
64+
}
65+
2366
public string Name => name;
2467
public string Tracking => tracking;
2568
public bool IsActive => isActive;

src/GitHub.Api/Git/GitClient.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,48 @@ public GitUser(string name, string email)
492492
this.email = email;
493493
}
494494

495+
public override int GetHashCode()
496+
{
497+
int hash = 17;
498+
hash = hash * 23 + (name?.GetHashCode() ?? 0);
499+
hash = hash * 23 + (email?.GetHashCode() ?? 0);
500+
return hash;
501+
}
502+
503+
public override bool Equals(object other)
504+
{
505+
if (other is GitUser)
506+
return Equals((GitUser)other);
507+
return false;
508+
}
509+
510+
public bool Equals(GitUser other)
511+
{
512+
return
513+
String.Equals(name, other.name) &&
514+
String.Equals(email, other.email)
515+
;
516+
}
517+
518+
public static bool operator ==(GitUser lhs, GitUser rhs)
519+
{
520+
// If both are null, or both are same instance, return true.
521+
if (ReferenceEquals(lhs, rhs))
522+
return true;
523+
524+
// If one is null, but not both, return false.
525+
if (((object)lhs == null) || ((object)rhs == null))
526+
return false;
527+
528+
// Return true if the fields match:
529+
return lhs.Equals(rhs);
530+
}
531+
532+
public static bool operator !=(GitUser lhs, GitUser rhs)
533+
{
534+
return !(lhs == rhs);
535+
}
536+
495537
public override string ToString()
496538
{
497539
return $"Name:\"{Name}\" Email:\"{Email}\"";

src/GitHub.Api/Git/GitConfig.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,48 @@ public ConfigRemote(string name, string url)
2020
this.url = url;
2121
}
2222

23+
public override int GetHashCode()
24+
{
25+
int hash = 17;
26+
hash = hash * 23 + (name?.GetHashCode() ?? 0);
27+
hash = hash * 23 + (url?.GetHashCode() ?? 0);
28+
return hash;
29+
}
30+
31+
public override bool Equals(object other)
32+
{
33+
if (other is ConfigRemote)
34+
return Equals((ConfigRemote)other);
35+
return false;
36+
}
37+
38+
public bool Equals(ConfigRemote other)
39+
{
40+
return
41+
String.Equals(name, other.name) &&
42+
String.Equals(url, other.url)
43+
;
44+
}
45+
46+
public static bool operator ==(ConfigRemote lhs, ConfigRemote rhs)
47+
{
48+
// If both are null, or both are same instance, return true.
49+
if (ReferenceEquals(lhs, rhs))
50+
return true;
51+
52+
// If one is null, but not both, return false.
53+
if (((object)lhs == null) || ((object)rhs == null))
54+
return false;
55+
56+
// Return true if the fields match:
57+
return lhs.Equals(rhs);
58+
}
59+
60+
public static bool operator !=(ConfigRemote lhs, ConfigRemote rhs)
61+
{
62+
return !(lhs == rhs);
63+
}
64+
2365
public string Name => name;
2466

2567
public string Url => url;
@@ -50,6 +92,48 @@ public ConfigBranch(string name, ConfigRemote? remote)
5092
this.remote = remote ?? ConfigRemote.Default;
5193
}
5294

95+
public override int GetHashCode()
96+
{
97+
int hash = 17;
98+
hash = hash * 23 + (name?.GetHashCode() ?? 0);
99+
hash = hash * 23 + remote.GetHashCode();
100+
return hash;
101+
}
102+
103+
public override bool Equals(object other)
104+
{
105+
if (other is ConfigBranch)
106+
return Equals((ConfigBranch)other);
107+
return false;
108+
}
109+
110+
public bool Equals(ConfigBranch other)
111+
{
112+
return
113+
String.Equals(name, other.name) &&
114+
remote.Equals(other.remote)
115+
;
116+
}
117+
118+
public static bool operator ==(ConfigBranch lhs, ConfigBranch rhs)
119+
{
120+
// If both are null, or both are same instance, return true.
121+
if (ReferenceEquals(lhs, rhs))
122+
return true;
123+
124+
// If one is null, but not both, return false.
125+
if (((object)lhs == null) || ((object)rhs == null))
126+
return false;
127+
128+
// Return true if the fields match:
129+
return lhs.Equals(rhs);
130+
}
131+
132+
public static bool operator !=(ConfigBranch lhs, ConfigBranch rhs)
133+
{
134+
return !(lhs == rhs);
135+
}
136+
53137
public bool IsTracking => Remote.HasValue;
54138

55139
public string Name => name;

0 commit comments

Comments
 (0)