From 9bed4fd9c7877ee2e056551629c19949f03b1cd0 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 5 Sep 2024 21:09:41 +1000 Subject: [PATCH] #349 Simplify equals check Signed-off-by: James Thompson --- src/CycloneDX.Core/Models/Component.cs | 10 ++-------- src/CycloneDX.Core/Models/Composition.cs | 10 ++-------- src/CycloneDX.Core/Models/Dependency.cs | 10 ++-------- src/CycloneDX.Core/Models/ExternalReference.cs | 10 ++-------- src/CycloneDX.Core/Models/Service.cs | 10 ++-------- src/CycloneDX.Core/Models/Tool.cs | 10 ++-------- .../Models/Vulnerabilities/Vulnerability.cs | 10 ++-------- 7 files changed, 14 insertions(+), 56 deletions(-) diff --git a/src/CycloneDX.Core/Models/Component.cs b/src/CycloneDX.Core/Models/Component.cs index 64bf1e50..31fece49 100644 --- a/src/CycloneDX.Core/Models/Component.cs +++ b/src/CycloneDX.Core/Models/Component.cs @@ -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() diff --git a/src/CycloneDX.Core/Models/Composition.cs b/src/CycloneDX.Core/Models/Composition.cs index cf5cb654..16f4b06f 100644 --- a/src/CycloneDX.Core/Models/Composition.cs +++ b/src/CycloneDX.Core/Models/Composition.cs @@ -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() diff --git a/src/CycloneDX.Core/Models/Dependency.cs b/src/CycloneDX.Core/Models/Dependency.cs index 7b0fe510..6d7a9520 100644 --- a/src/CycloneDX.Core/Models/Dependency.cs +++ b/src/CycloneDX.Core/Models/Dependency.cs @@ -38,18 +38,12 @@ public class Dependency: IEquatable 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() diff --git a/src/CycloneDX.Core/Models/ExternalReference.cs b/src/CycloneDX.Core/Models/ExternalReference.cs index 69204fca..8f69dd7d 100644 --- a/src/CycloneDX.Core/Models/ExternalReference.cs +++ b/src/CycloneDX.Core/Models/ExternalReference.cs @@ -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() diff --git a/src/CycloneDX.Core/Models/Service.cs b/src/CycloneDX.Core/Models/Service.cs index d27c1df1..96edfe84 100644 --- a/src/CycloneDX.Core/Models/Service.cs +++ b/src/CycloneDX.Core/Models/Service.cs @@ -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() diff --git a/src/CycloneDX.Core/Models/Tool.cs b/src/CycloneDX.Core/Models/Tool.cs index 48282a17..a9e15103 100644 --- a/src/CycloneDX.Core/Models/Tool.cs +++ b/src/CycloneDX.Core/Models/Tool.cs @@ -50,18 +50,12 @@ public class Tool: IEquatable 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() diff --git a/src/CycloneDX.Core/Models/Vulnerabilities/Vulnerability.cs b/src/CycloneDX.Core/Models/Vulnerabilities/Vulnerability.cs index 83b6b68b..c87c55c2 100644 --- a/src/CycloneDX.Core/Models/Vulnerabilities/Vulnerability.cs +++ b/src/CycloneDX.Core/Models/Vulnerabilities/Vulnerability.cs @@ -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()