|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="NullableStringEqualityComparer.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.Comparer |
| 22 | +{ |
| 23 | + using System; |
| 24 | + using System.Collections.Generic; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Provides equality comparison for string, handling null case |
| 28 | + /// </summary> |
| 29 | + public sealed class NullSafeStringComparer : IEqualityComparer<string> |
| 30 | + { |
| 31 | + /// <summary>Determines whether the specified objects are equal.</summary> |
| 32 | + /// <param name="x">The first object of type <paramref name="T" /> to compare.</param> |
| 33 | + /// <param name="y">The second object of type <paramref name="T" /> to compare.</param> |
| 34 | + /// <returns> |
| 35 | + /// <see langword="true" /> if the specified objects are equal; otherwise, <see langword="false" />.</returns> |
| 36 | + public bool Equals(string x, string y) |
| 37 | + { |
| 38 | + if (x == null && y == null) |
| 39 | + { |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + if (x == null || y == null) |
| 44 | + { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + return string.Equals(x, y, StringComparison.Ordinal); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary>Returns a hash code for the specified object.</summary> |
| 52 | + /// <param name="obj">The <see cref="T:System.Object" /> for which a hash code is to be returned.</param> |
| 53 | + /// <returns>A hash code for the specified object.</returns> |
| 54 | + /// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj" /> is a reference type and <paramref name="obj" /> is <see langword="null" />.</exception> |
| 55 | + public int GetHashCode(string? obj) |
| 56 | + { |
| 57 | + return obj?.GetHashCode() ?? 0; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets the instance of the <see cref="NullSafeStringComparer"/> |
| 62 | + /// </summary> |
| 63 | + public static readonly NullSafeStringComparer Instance = new(); |
| 64 | + } |
| 65 | +} |
0 commit comments