< Summary

Information
Class: MyNet.Primitives.Comparers.NullableComparer<T>
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Comparers/NullableComparer.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 79
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Compare(...)100%1010100%
Compare(...)100%1010100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Comparers/NullableComparer.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NullableComparer.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9
 10namespace 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>
 16public 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
 1235        if (x == null && y == null)
 336            return 0;
 37
 38        // Any object is greater than null
 939        if (x != null && y == null)
 340            return 1;
 41
 642        if (x == null)
 343            return -1;
 44
 45        // Otherwise compare the two values
 346        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
 15665        if (x == null && y == null)
 666            return 0;
 67
 68        // Any object is greater than null
 15069        if (x != null && y == null)
 670            return 1;
 71
 14472        if (x == null)
 673            return -1;
 74
 75        // Otherwise compare the two values
 13876        return x.CompareTo((T)y!);
 77    }
 78}
 79