| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NullableComparer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Primitives.Comparers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides comparison logic for nullable value types following the MSDN null comparison rules. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The underlying non-nullable value type that implements <see cref="IComparable{T}"/>.</typeparam> |
| | | 16 | | public class NullableComparer<T> : IComparer<T?>, IComparer<IComparable<T>?> |
| | | 17 | | where T : struct, IComparable<T> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Compares two nullable values of type <typeparamref name="T"/>. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="x">The first value to compare.</param> |
| | | 23 | | /// <param name="y">The second value to compare.</param> |
| | | 24 | | /// <returns> |
| | | 25 | | /// A signed integer that indicates the relative values of x and y: |
| | | 26 | | /// - Less than zero: x is less than y. |
| | | 27 | | /// - Zero: x equals y. |
| | | 28 | | /// - Greater than zero: x is greater than y. |
| | | 29 | | /// </returns> |
| | | 30 | | public int Compare(T? x, T? y) |
| | | 31 | | { |
| | | 32 | | // Compare nulls according MSDN specification |
| | | 33 | | |
| | | 34 | | // Two nulls are equal |
| | 12 | 35 | | if (x == null && y == null) |
| | 3 | 36 | | return 0; |
| | | 37 | | |
| | | 38 | | // Any object is greater than null |
| | 9 | 39 | | if (x != null && y == null) |
| | 3 | 40 | | return 1; |
| | | 41 | | |
| | 6 | 42 | | if (x == null) |
| | 3 | 43 | | return -1; |
| | | 44 | | |
| | | 45 | | // Otherwise compare the two values |
| | 3 | 46 | | return x.Value.CompareTo(y!.Value); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Compares two objects that implement <see cref="IComparable{T}"/>, allowing nulls. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="x">The first comparable object to compare.</param> |
| | | 53 | | /// <param name="y">The second comparable object to compare.</param> |
| | | 54 | | /// <returns> |
| | | 55 | | /// A signed integer that indicates the relative values of x and y: |
| | | 56 | | /// - Less than zero: x is less than y. |
| | | 57 | | /// - Zero: x equals y. |
| | | 58 | | /// - Greater than zero: x is greater than y. |
| | | 59 | | /// </returns> |
| | | 60 | | public int Compare(IComparable<T>? x, IComparable<T>? y) |
| | | 61 | | { |
| | | 62 | | // Compare nulls according MSDN specification |
| | | 63 | | |
| | | 64 | | // Two nulls are equal |
| | 156 | 65 | | if (x == null && y == null) |
| | 6 | 66 | | return 0; |
| | | 67 | | |
| | | 68 | | // Any object is greater than null |
| | 150 | 69 | | if (x != null && y == null) |
| | 6 | 70 | | return 1; |
| | | 71 | | |
| | 144 | 72 | | if (x == null) |
| | 6 | 73 | | return -1; |
| | | 74 | | |
| | | 75 | | // Otherwise compare the two values |
| | 138 | 76 | | return x.CompareTo((T)y!); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |