< Summary

Information
Class: MyNet.Primitives.Conversion.UnitConverterRegistry
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/UnitConverterRegistry.cs
Tag: 323_28699572109
Line coverage
78%
Covered lines: 11
Uncovered lines: 3
Coverable lines: 14
Total lines: 58
Line coverage: 78.5%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Register(...)100%11100%
Get()100%22100%
Get(...)0%620%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/UnitConverterRegistry.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="UnitConverterRegistry.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.Conversion;
 11
 12/// <summary>
 13/// Provides a registry for unit converters, allowing for the registration and retrieval of converters based on their as
 14/// </summary>
 15public static class UnitConverterRegistry
 16{
 917    private static readonly Dictionary<Type, object> Converters = [];
 18
 19    static UnitConverterRegistry()
 20    {
 921        Register(new DataSizeConverter());
 922        Register(new LengthConverter());
 923        Register(new MassConverter());
 924        Register(new TimeConverter());
 925        Register(new TemperatureConverter());
 926    }
 27
 28    /// <summary>
 29    /// Registers a unit converter for a specific unit type. The converter is stored in the registry using the unit type
 30    /// </summary>
 31    /// <param name="converter">The unit converter to register.</param>
 32    /// <typeparam name="TUnit">The type of the unit.</typeparam>
 33    public static void Register<TUnit>(IUnitConverter<TUnit> converter)
 4534        where TUnit : struct, Enum => Converters[typeof(TUnit)] = converter;
 35
 36    /// <summary>
 37    /// Retrieves a registered unit converter for a specific unit type.
 38    /// </summary>
 39    /// <typeparam name="TUnit">The type of the unit.</typeparam>
 40    /// <returns>The registered unit converter.</returns>
 41    public static IUnitConverter<TUnit> Get<TUnit>()
 42        where TUnit : struct, Enum
 25543        => Converters.TryGetValue(typeof(TUnit), out var converter)
 25544            ? (IUnitConverter<TUnit>)converter
 25545            : throw new InvalidOperationException($"No converter registered for unit type '{typeof(TUnit).Name}'.");
 46
 47    /// <summary>
 48    /// Retrieves a registered unit converter for a specific unit type, using the provided type as the key.
 49    /// </summary>
 50    /// <param name="type">The type of the unit.</param>
 51    /// <returns>The registered unit converter.</returns>
 52    public static IUnitConverter<TUnit> Get<TUnit>(Type type)
 53        where TUnit : struct, Enum
 054        => Converters.TryGetValue(type, out var converter)
 055            ? (IUnitConverter<TUnit>)converter
 056            : throw new InvalidOperationException($"No converter registered for unit type '{type.Name}'.");
 57}
 58