Skip to content

Commit

Permalink
Add Equals and GetHashCode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Apr 24, 2019
1 parent ef97f06 commit 649123b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
22 changes: 22 additions & 0 deletions src/Berrysoft.Tsinghua.Net/FluxUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ public FluxUser(string username, long flux, TimeSpan onlineTime, decimal balance
/// </summary>
public decimal Balance { get; }

/// <summary>
/// Determines whether the username of the two <see cref="FluxUser"/> are equal.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
public override bool Equals(object obj)
{
if (obj is FluxUser other)
{
return Username == other.Username;
}
return false;
}
/// <summary>
/// Returns the hash value of this object.
/// </summary>
/// <returns>The hash value.</returns>
public override int GetHashCode()
{
return Username?.GetHashCode() ?? 0;
}

internal static FluxUser Parse(string fluxstr)
{
string[] r = fluxstr.Split(',');
Expand Down
34 changes: 28 additions & 6 deletions src/Berrysoft.Tsinghua.Net/LogResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ namespace Berrysoft.Tsinghua.Net
/// </summary>
public class LogResponse
{
/// <summary>
/// Initialize a new instance of <see cref="LogResponse"/> class.
/// </summary>
/// <param name="succeed">Whether the command is succeed.</param>
/// <param name="message">The formatted response message.</param>
public LogResponse(bool succeed, string message)
{
Succeed = succeed;
Message = message;
}

/// <summary>
/// Shows whether the command is succeed.
/// </summary>
Expand All @@ -18,14 +29,25 @@ public class LogResponse
public string Message { get; }

/// <summary>
/// Initialize a new instance of <see cref="LogResponse"/> class.
/// Determines whether the two <see cref="LogResponse"/> are equal.
/// </summary>
/// <param name="succeed">Whether the command is succeed.</param>
/// <param name="message">The formatted response message.</param>
public LogResponse(bool succeed, string message)
/// <param name="obj">The other object.</param>
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
public override bool Equals(object obj)
{
Succeed = succeed;
Message = message;
if (obj is LogResponse other)
{
return Succeed == other.Succeed && Message == other.Message;
}
return false;
}
/// <summary>
/// Returns the hash value of this object.
/// </summary>
/// <returns>The hash value.</returns>
public override int GetHashCode()
{
return Succeed.GetHashCode() ^ (Message?.GetHashCode() ?? 0);
}

internal static LogResponse ParseFromNet(string response)
Expand Down
43 changes: 42 additions & 1 deletion src/Berrysoft.Tsinghua.Net/UseregHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -39,6 +38,27 @@ public NetUser(IPAddress address, DateTime loginTime, string client)
/// 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"/>.
/// </summary>
public string Client { get; }
/// <summary>
/// Determines whether the two <see cref="NetUser"/> are equal.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
public override bool Equals(object obj)
{
if (obj is NetUser other)
{
return Address.Equals(other.Address) && LoginTime == other.LoginTime && Client == other.Client;
}
return false;
}
/// <summary>
/// Returns the hash value of this object.
/// </summary>
/// <returns>The hash value.</returns>
public override int GetHashCode()
{
return (Address?.GetHashCode() ?? 0) ^ LoginTime.GetHashCode() ^ (Client?.GetHashCode() ?? 0);
}
}

/// <summary>
Expand Down Expand Up @@ -70,6 +90,27 @@ public NetDetail(DateTime login, DateTime logout, long flux)
/// The flux has been used.
/// </summary>
public long Flux { get; }
/// <summary>
/// Determines whether the two <see cref="NetDetail"/> are equal.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns><see langword="true"/> if they're equal; otherwise, <see langword="false"/>.</returns>
public override bool Equals(object obj)
{
if (obj is NetDetail other)
{
return LoginTime == other.LoginTime && LogoutTime == other.LogoutTime && Flux == other.Flux;
}
return false;
}
/// <summary>
/// Returns the hash value of this object.
/// </summary>
/// <returns>The hash value.</returns>
public override int GetHashCode()
{
return LoginTime.GetHashCode() ^ LogoutTime.GetHashCode() ^ Flux.GetHashCode();
}
}

/// <summary>
Expand Down

0 comments on commit 649123b

Please sign in to comment.