Skip to content

Commit 39c664e

Browse files
[Add] DependencyComparer
1 parent 7b55a6c commit 39c664e

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="DependencyComparer.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.Extensions.Core.DTO.Comparers
22+
{
23+
using System;
24+
using System.CodeDom.Compiler;
25+
using System.Collections.Generic;
26+
27+
using SysML2.NET.Core.DTO.Root.Dependencies;
28+
29+
using SysML2.NET.Extensions.Comparers;
30+
31+
/// <summary>
32+
/// Provides an equality comparison for <see cref="IDependency"/> instances
33+
/// based on their semantic content rather than object reference identity.
34+
/// </summary>
35+
/// <remarks>
36+
/// This comparer is intended for use in deserialization, testing, and model
37+
/// validation scenarios where two <see cref="IDependency"/> instances
38+
/// originating from different sources (e.g. JSON and MessagePack) must be
39+
/// considered equal if they represent the same SysML v2 model element.
40+
/// </remarks>
41+
[GeneratedCode("SysML2.NET", "latest")]
42+
public class DependencyComparer : IEqualityComparer<IDependency>
43+
{
44+
/// <summary>
45+
/// The <see cref="StringComparer"/> used for all string comparisons performed
46+
/// by this comparer.
47+
/// </summary>
48+
/// <remarks>
49+
/// <see cref="StringComparer.Ordinal"/> is used to ensure culture-independent,
50+
/// deterministic comparison semantics suitable for identifiers and
51+
/// model-level string values.
52+
/// </remarks>
53+
private static readonly StringComparer OrdinalStringComparer = StringComparer.Ordinal;
54+
55+
/// <summary>
56+
/// Determines whether two <see cref="IDependency"/> instances are equal.
57+
/// </summary>
58+
/// <param name="x">
59+
/// The first <see cref="IDependency"/> to compare.
60+
/// </param>
61+
/// <param name="y">
62+
/// The second <see cref="IDependency"/> to compare.
63+
/// </param>
64+
/// <returns>
65+
/// <c>true</c> if both instances represent the same <see cref="IDependency"/>
66+
/// according to their semantic content; otherwise, <c>false</c>.
67+
/// </returns>
68+
/// <remarks>
69+
/// This method performs a deep, deterministic comparison of all relevant
70+
/// properties, ignoring object reference identity and any transient or
71+
/// derived state.
72+
/// </remarks>
73+
public bool Equals(IDependency x, IDependency y)
74+
{
75+
if (ReferenceEquals(x, y)) return true;
76+
77+
if (x is null || y is null) return false;
78+
79+
if (x.Id != y.Id) return false;
80+
81+
if (!StringSequenceEquality.OrderedEqual(x.AliasIds, y.AliasIds)) return false;
82+
83+
if (!GuidSequenceEquality.OrderedEqual(x.Client, y.Client)) return false;
84+
85+
if (!OrdinalStringComparer.Equals(x.DeclaredName, y.DeclaredName)) return false;
86+
87+
if (!OrdinalStringComparer.Equals(x.DeclaredShortName, y.DeclaredShortName)) return false;
88+
89+
if (!OrdinalStringComparer.Equals(x.ElementId, y.ElementId)) return false;
90+
91+
if (x.IsImplied != y.IsImplied) return false;
92+
93+
if (x.IsImpliedIncluded != y.IsImpliedIncluded) return false;
94+
95+
if (!GuidSequenceEquality.OrderedEqual(x.OwnedRelatedElement, y.OwnedRelatedElement)) return false;
96+
97+
if (!GuidSequenceEquality.OrderedEqual(x.OwnedRelationship, y.OwnedRelationship)) return false;
98+
99+
if (x.OwningRelatedElement != y.OwningRelatedElement) return false;
100+
101+
if (x.OwningRelationship != y.OwningRelationship) return false;
102+
103+
if (!GuidSequenceEquality.OrderedEqual(x.Supplier, y.Supplier)) return false;
104+
105+
return true;
106+
}
107+
108+
/// <summary>
109+
/// Returns a hash code for the specified <see cref="IDependency"/>.
110+
/// </summary>
111+
/// <param name="obj">
112+
/// The <see cref="IDependency"/> for which a hash code is to be returned.
113+
/// </param>
114+
/// <returns>
115+
/// A hash code based on the stable identity of the element.
116+
/// </returns>
117+
/// <remarks>
118+
/// The hash code is intentionally derived from the element identity only,
119+
/// ensuring stability and compatibility with collection-based equality
120+
/// checks
121+
/// </remarks>
122+
public int GetHashCode(IDependency obj)
123+
{
124+
return HashCode.Combine(typeof(IDependency), obj.Id);
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)