Skip to content

Commit 649123b

Browse files
committed
Add Equals and GetHashCode.
1 parent ef97f06 commit 649123b

3 files changed

Lines changed: 92 additions & 7 deletions

File tree

src/Berrysoft.Tsinghua.Net/FluxUser.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ public FluxUser(string username, long flux, TimeSpan onlineTime, decimal balance
3838
/// </summary>
3939
public decimal Balance { get; }
4040

41+
/// <summary>
42+
/// Determines whether the username of the two <see cref="FluxUser"/> are equal.
43+
/// </summary>
44+
/// <param name="obj">The other object.</param>
45+
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
46+
public override bool Equals(object obj)
47+
{
48+
if (obj is FluxUser other)
49+
{
50+
return Username == other.Username;
51+
}
52+
return false;
53+
}
54+
/// <summary>
55+
/// Returns the hash value of this object.
56+
/// </summary>
57+
/// <returns>The hash value.</returns>
58+
public override int GetHashCode()
59+
{
60+
return Username?.GetHashCode() ?? 0;
61+
}
62+
4163
internal static FluxUser Parse(string fluxstr)
4264
{
4365
string[] r = fluxstr.Split(',');

src/Berrysoft.Tsinghua.Net/LogResponse.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ namespace Berrysoft.Tsinghua.Net
88
/// </summary>
99
public class LogResponse
1010
{
11+
/// <summary>
12+
/// Initialize a new instance of <see cref="LogResponse"/> class.
13+
/// </summary>
14+
/// <param name="succeed">Whether the command is succeed.</param>
15+
/// <param name="message">The formatted response message.</param>
16+
public LogResponse(bool succeed, string message)
17+
{
18+
Succeed = succeed;
19+
Message = message;
20+
}
21+
1122
/// <summary>
1223
/// Shows whether the command is succeed.
1324
/// </summary>
@@ -18,14 +29,25 @@ public class LogResponse
1829
public string Message { get; }
1930

2031
/// <summary>
21-
/// Initialize a new instance of <see cref="LogResponse"/> class.
32+
/// Determines whether the two <see cref="LogResponse"/> are equal.
2233
/// </summary>
23-
/// <param name="succeed">Whether the command is succeed.</param>
24-
/// <param name="message">The formatted response message.</param>
25-
public LogResponse(bool succeed, string message)
34+
/// <param name="obj">The other object.</param>
35+
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
36+
public override bool Equals(object obj)
2637
{
27-
Succeed = succeed;
28-
Message = message;
38+
if (obj is LogResponse other)
39+
{
40+
return Succeed == other.Succeed && Message == other.Message;
41+
}
42+
return false;
43+
}
44+
/// <summary>
45+
/// Returns the hash value of this object.
46+
/// </summary>
47+
/// <returns>The hash value.</returns>
48+
public override int GetHashCode()
49+
{
50+
return Succeed.GetHashCode() ^ (Message?.GetHashCode() ?? 0);
2951
}
3052

3153
internal static LogResponse ParseFromNet(string response)

src/Berrysoft.Tsinghua.Net/UseregHelper.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.Globalization;
54
using System.Linq;
@@ -39,6 +38,27 @@ public NetUser(IPAddress address, DateTime loginTime, string client)
3938
/// The client used by this connection. It may be "Unknown" through <see cref="NetHelper"/>, and "Windows NT", "Windows 8", "Windows 7" or "Unknown" through <see cref="AuthHelper"/>.
4039
/// </summary>
4140
public string Client { get; }
41+
/// <summary>
42+
/// Determines whether the two <see cref="NetUser"/> are equal.
43+
/// </summary>
44+
/// <param name="obj">The other object.</param>
45+
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
46+
public override bool Equals(object obj)
47+
{
48+
if (obj is NetUser other)
49+
{
50+
return Address.Equals(other.Address) && LoginTime == other.LoginTime && Client == other.Client;
51+
}
52+
return false;
53+
}
54+
/// <summary>
55+
/// Returns the hash value of this object.
56+
/// </summary>
57+
/// <returns>The hash value.</returns>
58+
public override int GetHashCode()
59+
{
60+
return (Address?.GetHashCode() ?? 0) ^ LoginTime.GetHashCode() ^ (Client?.GetHashCode() ?? 0);
61+
}
4262
}
4363

4464
/// <summary>
@@ -70,6 +90,27 @@ public NetDetail(DateTime login, DateTime logout, long flux)
7090
/// The flux has been used.
7191
/// </summary>
7292
public long Flux { get; }
93+
/// <summary>
94+
/// Determines whether the two <see cref="NetDetail"/> are equal.
95+
/// </summary>
96+
/// <param name="obj">The other object.</param>
97+
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
98+
public override bool Equals(object obj)
99+
{
100+
if (obj is NetDetail other)
101+
{
102+
return LoginTime == other.LoginTime && LogoutTime == other.LogoutTime && Flux == other.Flux;
103+
}
104+
return false;
105+
}
106+
/// <summary>
107+
/// Returns the hash value of this object.
108+
/// </summary>
109+
/// <returns>The hash value.</returns>
110+
public override int GetHashCode()
111+
{
112+
return LoginTime.GetHashCode() ^ LogoutTime.GetHashCode() ^ Flux.GetHashCode();
113+
}
73114
}
74115

75116
/// <summary>

0 commit comments

Comments
 (0)