Skip to content

Commit

Permalink
CycloneDX#349 Simplify equals check
Browse files Browse the repository at this point in the history
Signed-off-by: James Thompson <[email protected]>
  • Loading branch information
thompson-tomo committed Sep 8, 2024
1 parent a42f0bf commit 9bed4fd
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 56 deletions.
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,12 @@ public bool NonNullableModified

public override bool Equals(object obj)
{
var other = obj as Component;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as Component);
}

public bool Equals(Component obj)
{
return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(obj);
return obj != null && Json.Serializer.Serialize(this) == Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,12 @@ public void WriteXml(System.Xml.XmlWriter writer) {

public override bool Equals(object obj)
{
var other = obj as Composition;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as Composition);
}

public bool Equals(Composition obj)
{
return CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
return obj != null && CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/Dependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,12 @@ public class Dependency: IEquatable<Dependency>

public override bool Equals(object obj)
{
var other = obj as Dependency;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as Dependency);
}

public bool Equals(Dependency obj)
{
return CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
return obj != null && CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/ExternalReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,12 @@ public enum ExternalReferenceType

public override bool Equals(object obj)
{
var other = obj as ExternalReference;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as ExternalReference);
}

public bool Equals(ExternalReference obj)
{
return CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
return obj != null && CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,12 @@ public LicenseChoiceList LicensesSerialized

public override bool Equals(object obj)
{
var other = obj as Service;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as Service);
}

public bool Equals(Service obj)
{
return CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
return obj != null && CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,12 @@ public class Tool: IEquatable<Tool>

public override bool Equals(object obj)
{
var other = obj as Tool;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as Tool);
}

public bool Equals(Tool obj)
{
return CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
return obj != null && CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down
10 changes: 2 additions & 8 deletions src/CycloneDX.Core/Models/Vulnerabilities/Vulnerability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,12 @@ public DateTime? Rejected

public override bool Equals(object obj)
{
var other = obj as Vulnerability;
if (other == null)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(other);
return Equals(obj as Vulnerability);
}

public bool Equals(Vulnerability obj)
{
return CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
return obj != null && CycloneDX.Json.Serializer.Serialize(this) == CycloneDX.Json.Serializer.Serialize(obj);
}

public override int GetHashCode()
Expand Down

0 comments on commit 9bed4fd

Please sign in to comment.