Skip to content

[Types] Enhance equality comparison #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/redmine-net-api/Internals/HashCodeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public static int GetHashCode<T>(IList<T> list, int hash) where T : class
return hashCode;
}

hashCode = (hashCode * 13) + list.Count;
hashCode = (hashCode * 17) + list.Count;

foreach (var t in list)
{
hashCode *= 13;
hashCode *= 17;
if (t != null)
{
hashCode += t.GetHashCode();
Expand Down
63 changes: 49 additions & 14 deletions src/redmine-net-api/Types/Attachment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2011 - 2023 Adrian Popescu

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,7 +31,8 @@ namespace Redmine.Net.Api.Types
/// </summary>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
[XmlRoot(RedmineKeys.ATTACHMENT)]
public sealed class Attachment : Identifiable<Attachment>
public sealed class Attachment :
Identifiable<Attachment>
{
#region Properties
/// <summary>
Expand Down Expand Up @@ -186,15 +187,28 @@ public override void WriteJson(JsonWriter writer)
public override bool Equals(Attachment other)
{
if (other == null) return false;
return Id == other.Id
&& FileName == other.FileName
&& FileSize == other.FileSize
&& ContentType == other.ContentType
&& Author == other.Author
&& ThumbnailUrl == other.ThumbnailUrl
&& CreatedOn == other.CreatedOn
&& Description == other.Description
&& ContentUrl == other.ContentUrl;
return base.Equals(other)
&& string.Equals(FileName, other.FileName, StringComparison.OrdinalIgnoreCase)
&& string.Equals(ContentType, other.ContentType, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Description, other.Description, StringComparison.OrdinalIgnoreCase)
&& string.Equals(ContentUrl, other.ContentUrl, StringComparison.OrdinalIgnoreCase)
&& string.Equals(ThumbnailUrl, other.ThumbnailUrl, StringComparison.OrdinalIgnoreCase)
&& Equals(Author, other.Author)
&& FileSize == other.FileSize
&& CreatedOn == other.CreatedOn;
}

/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals(obj as Attachment);
}

/// <summary>
Expand All @@ -209,15 +223,36 @@ public override int GetHashCode()
hashCode = HashCodeHelper.GetHashCode(FileName, hashCode);
hashCode = HashCodeHelper.GetHashCode(FileSize, hashCode);
hashCode = HashCodeHelper.GetHashCode(ContentType, hashCode);
hashCode = HashCodeHelper.GetHashCode(Author, hashCode);
hashCode = HashCodeHelper.GetHashCode(CreatedOn, hashCode);
hashCode = HashCodeHelper.GetHashCode(Description, hashCode);
hashCode = HashCodeHelper.GetHashCode(ContentUrl, hashCode);
hashCode = HashCodeHelper.GetHashCode(ThumbnailUrl, hashCode);

hashCode = HashCodeHelper.GetHashCode(Author, hashCode);
hashCode = HashCodeHelper.GetHashCode(CreatedOn, hashCode);
return hashCode;
}
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(Attachment left, Attachment right)
{
return Equals(left, right);
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(Attachment left, Attachment right)
{
return !Equals(left, right);
}
#endregion

private string DebuggerDisplay =>
Expand Down
26 changes: 24 additions & 2 deletions src/redmine-net-api/Types/ChangeSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2011 - 2023 Adrian Popescu

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -178,14 +178,36 @@ public override int GetHashCode()
{
unchecked
{
var hashCode = 13;
var hashCode = 17;
hashCode = HashCodeHelper.GetHashCode(Revision, hashCode);
hashCode = HashCodeHelper.GetHashCode(User, hashCode);
hashCode = HashCodeHelper.GetHashCode(Comments, hashCode);
hashCode = HashCodeHelper.GetHashCode(CommittedOn, hashCode);
return hashCode;
}
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(ChangeSet left, ChangeSet right)
{
return Equals(left, right);
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(ChangeSet left, ChangeSet right)
{
return !Equals(left, right);
}
#endregion

/// <summary>
Expand Down
79 changes: 49 additions & 30 deletions src/redmine-net-api/Types/CustomField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,22 @@ public bool Equals(CustomField other)
{
if (other == null) return false;

return Id == other.Id
&& IsFilter == other.IsFilter
&& IsRequired == other.IsRequired
&& Multiple == other.Multiple
&& Searchable == other.Searchable
&& Visible == other.Visible
&& string.Equals(CustomizedType,other.CustomizedType, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Description,other.Description, StringComparison.OrdinalIgnoreCase)
&& string.Equals(DefaultValue,other.DefaultValue, StringComparison.OrdinalIgnoreCase)
&& string.Equals(FieldFormat,other.FieldFormat, StringComparison.OrdinalIgnoreCase)
&& MaxLength == other.MaxLength
&& MinLength == other.MinLength
&& string.Equals(Name,other.Name, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Regexp,other.Regexp, StringComparison.OrdinalIgnoreCase)
&& PossibleValues.Equals(other.PossibleValues)
&& Roles.Equals(other.Roles)
&& Trackers.Equals(other.Trackers);
return base.Equals(other)
&& string.Equals(CustomizedType, other.CustomizedType, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Description, other.Description, StringComparison.OrdinalIgnoreCase)
&& string.Equals(FieldFormat, other.FieldFormat, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Regexp, other.Regexp, StringComparison.OrdinalIgnoreCase)
&& string.Equals(DefaultValue, other.DefaultValue, StringComparison.Ordinal)
&& MinLength == other.MinLength
&& MaxLength == other.MaxLength
&& IsRequired == other.IsRequired
&& IsFilter == other.IsFilter
&& Searchable == other.Searchable
&& Multiple == other.Multiple
&& Visible == other.Visible
&& Equals(PossibleValues, other.PossibleValues)
&& Equals(Trackers, other.Trackers)
&& Equals(Roles, other.Roles);
}

/// <summary>
Expand All @@ -247,27 +246,47 @@ public override int GetHashCode()
{
unchecked
{
var hashCode = 13;
hashCode = HashCodeHelper.GetHashCode(Id, hashCode);
hashCode = HashCodeHelper.GetHashCode(IsFilter, hashCode);
hashCode = HashCodeHelper.GetHashCode(IsRequired, hashCode);
hashCode = HashCodeHelper.GetHashCode(Description, hashCode);
hashCode = HashCodeHelper.GetHashCode(Multiple, hashCode);
hashCode = HashCodeHelper.GetHashCode(Searchable, hashCode);
hashCode = HashCodeHelper.GetHashCode(Visible, hashCode);
var hashCode = base.GetHashCode();
hashCode = HashCodeHelper.GetHashCode(CustomizedType, hashCode);
hashCode = HashCodeHelper.GetHashCode(DefaultValue, hashCode);
hashCode = HashCodeHelper.GetHashCode(Description, hashCode);
hashCode = HashCodeHelper.GetHashCode(FieldFormat, hashCode);
hashCode = HashCodeHelper.GetHashCode(MaxLength, hashCode);
hashCode = HashCodeHelper.GetHashCode(MinLength, hashCode);
hashCode = HashCodeHelper.GetHashCode(Name, hashCode);
hashCode = HashCodeHelper.GetHashCode(Regexp, hashCode);
hashCode = HashCodeHelper.GetHashCode(MinLength, hashCode);
hashCode = HashCodeHelper.GetHashCode(MaxLength, hashCode);
hashCode = HashCodeHelper.GetHashCode(IsRequired, hashCode);
hashCode = HashCodeHelper.GetHashCode(IsFilter, hashCode);
hashCode = HashCodeHelper.GetHashCode(Searchable, hashCode);
hashCode = HashCodeHelper.GetHashCode(Multiple, hashCode);
hashCode = HashCodeHelper.GetHashCode(DefaultValue, hashCode);
hashCode = HashCodeHelper.GetHashCode(Visible, hashCode);
hashCode = HashCodeHelper.GetHashCode(PossibleValues, hashCode);
hashCode = HashCodeHelper.GetHashCode(Roles, hashCode);
hashCode = HashCodeHelper.GetHashCode(Trackers, hashCode);
hashCode = HashCodeHelper.GetHashCode(Roles, hashCode);
return hashCode;
}
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(CustomField left, CustomField right)
{
return Equals(left, right);
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(CustomField left, CustomField right)
{
return !Equals(left, right);
}
#endregion

private string DebuggerDisplay =>
Expand Down
28 changes: 26 additions & 2 deletions src/redmine-net-api/Types/CustomFieldPossibleValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public void WriteJson(JsonWriter writer) { }
public bool Equals(CustomFieldPossibleValue other)
{
if (other == null) return false;
return Value == other.Value;
return string.Equals(Value, other.Value, StringComparison.Ordinal)
&& string.Equals(Label, other.Label, StringComparison.Ordinal);
}

/// <summary>
Expand All @@ -163,11 +164,34 @@ public override int GetHashCode()
{
unchecked
{
var hashCode = 13;
var hashCode = 17;
hashCode = HashCodeHelper.GetHashCode(Value, hashCode);
hashCode = HashCodeHelper.GetHashCode(Label, hashCode);
return hashCode;
}
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(CustomFieldPossibleValue left, CustomFieldPossibleValue right)
{
return Equals(left, right);
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(CustomFieldPossibleValue left, CustomFieldPossibleValue right)
{
return !Equals(left, right);
}
#endregion

/// <summary>
Expand Down
31 changes: 28 additions & 3 deletions src/redmine-net-api/Types/CustomFieldValue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright 2011 - 2023 Adrian Popescu

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -30,7 +30,10 @@ namespace Redmine.Net.Api.Types
/// </summary>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
[XmlRoot(RedmineKeys.VALUE)]
public class CustomFieldValue : IXmlSerializable, IJsonSerializable, IEquatable<CustomFieldValue>, ICloneable
public class CustomFieldValue :
IXmlSerializable
,IJsonSerializable
,IEquatable<CustomFieldValue>
{
/// <summary>
///
Expand Down Expand Up @@ -166,12 +169,34 @@ public override int GetHashCode()
{
unchecked
{
var hashCode = 13;
var hashCode = 17;
hashCode = HashCodeHelper.GetHashCode(Info, hashCode);
return hashCode;
}
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(CustomFieldValue left, CustomFieldValue right)
{
return Equals(left, right);
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(CustomFieldValue left, CustomFieldValue right)
{
return !Equals(left, right);
}

#endregion

#region Implementation of IClonable
Expand Down
Loading
Loading