|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="AnnotatingElementComparer.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2026 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.Annotations; |
| 28 | + using SysML2.NET.Extensions.Comparers; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Provides an equality comparison for <see cref="IAnnotatingElement"/> instances |
| 32 | + /// based on their semantic content rather than object reference identity. |
| 33 | + /// </summary> |
| 34 | + /// <remarks> |
| 35 | + /// This comparer is intended for use in deserialization, testing, and model |
| 36 | + /// validation scenarios where two <see cref="IAnnotatingElement"/> instances |
| 37 | + /// originating from different sources (e.g. JSON and MessagePack) must be |
| 38 | + /// considered equal if they represent the same SysML v2 model element. |
| 39 | + /// </remarks> |
| 40 | + [GeneratedCode("SysML2.NET", "latest")] |
| 41 | + public sealed class AnnotatingElementComparer : IEqualityComparer<IAnnotatingElement> |
| 42 | + { |
| 43 | + /// <summary> |
| 44 | + /// The <see cref="StringComparer"/> used for all string comparisons performed |
| 45 | + /// by this comparer. |
| 46 | + /// </summary> |
| 47 | + /// <remarks> |
| 48 | + /// <see cref="StringComparer.Ordinal"/> is used to ensure culture-independent, |
| 49 | + /// deterministic comparison semantics suitable for identifiers and |
| 50 | + /// model-level string values. |
| 51 | + /// </remarks> |
| 52 | + private static readonly StringComparer OrdinalStringComparer = StringComparer.Ordinal; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Determines whether two <see cref="IAnnotatingElement"/> instances are equal. |
| 56 | + /// </summary> |
| 57 | + /// <param name="x"> |
| 58 | + /// The first <see cref="IAnnotatingElement"/> to compare. |
| 59 | + /// </param> |
| 60 | + /// <param name="y"> |
| 61 | + /// The second <see cref="IAnnotatingElement"/> to compare. |
| 62 | + /// </param> |
| 63 | + /// <returns> |
| 64 | + /// <c>true</c> if both instances represent the same <see cref="IAnnotatingElement"/> |
| 65 | + /// according to their semantic content; otherwise, <c>false</c>. |
| 66 | + /// </returns> |
| 67 | + /// <remarks> |
| 68 | + /// This method performs a deep, deterministic comparison of all relevant |
| 69 | + /// properties, ignoring object reference identity and any transient or |
| 70 | + /// derived state. |
| 71 | + /// </remarks> |
| 72 | + public bool Equals(IAnnotatingElement x, IAnnotatingElement y) |
| 73 | + { |
| 74 | + if (ReferenceEquals(x, y)) return true; |
| 75 | + |
| 76 | + if (x is null || y is null) return false; |
| 77 | + |
| 78 | + if (x.Id != y.Id) return false; |
| 79 | + |
| 80 | + if (!StringSequenceEquality.OrderedEqual(x.AliasIds, y.AliasIds)) return false; |
| 81 | + |
| 82 | + if (!OrdinalStringComparer.Equals(x.DeclaredName, y.DeclaredName)) return false; |
| 83 | + |
| 84 | + if (!OrdinalStringComparer.Equals(x.DeclaredShortName, y.DeclaredShortName)) return false; |
| 85 | + |
| 86 | + if (!OrdinalStringComparer.Equals(x.ElementId, y.ElementId)) return false; |
| 87 | + |
| 88 | + if (x.IsImpliedIncluded != y.IsImpliedIncluded) return false; |
| 89 | + |
| 90 | + if (!GuidSequenceEquality.OrderedEqual(x.OwnedRelationship, y.OwnedRelationship)) return false; |
| 91 | + |
| 92 | + if (x.OwningRelationship != y.OwningRelationship) return false; |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Returns a hash code for the specified <see cref="IAnnotatingElement"/>. |
| 99 | + /// </summary> |
| 100 | + /// <param name="obj"> |
| 101 | + /// The <see cref="IAnnotatingElement"/> for which a hash code is to be returned. |
| 102 | + /// </param> |
| 103 | + /// <returns> |
| 104 | + /// A hash code based on the stable identity of the element. |
| 105 | + /// </returns> |
| 106 | + /// <remarks> |
| 107 | + /// The hash code is intentionally derived from the element identity only, |
| 108 | + /// ensuring stability and compatibility with collection-based equality |
| 109 | + /// checks |
| 110 | + /// </remarks> |
| 111 | + public int GetHashCode(IAnnotatingElement obj) |
| 112 | + { |
| 113 | + return HashCode.Combine(typeof(IAnnotatingElement), obj.Id); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
0 commit comments