< Summary

Information
Class: MyNet.Observable.TemperatureValue
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Base/BoundedUnitValue.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 1
Coverable lines: 1
Total lines: 129
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Base/BoundedUnitValue.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="BoundedUnitValue.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Numerics;
 9using MyNet.Primitives;
 10using MyNet.Primitives.Intervals;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Observable;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16/// <summary>
 17/// Observable bounded numeric value with a measurement unit and conversion helpers.
 18/// </summary>
 19/// <typeparam name="T">The numeric type.</typeparam>
 20/// <typeparam name="TUnit">The unit enum type.</typeparam>
 21public class BoundedUnitValue<T, TUnit> : BoundedValue<T>, IBoundedUnitValue<T, TUnit>
 22    where T : struct, INumber<T>
 23    where TUnit : struct, Enum
 24{
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="BoundedUnitValue{T, TUnit}"/> class.
 27    /// </summary>
 28    /// <param name="unit">The measurement unit.</param>
 29    /// <param name="propertyDisplayKey">Translation key used in validation messages.</param>
 30    /// <param name="defaultValue">Optional default value.</param>
 31    public BoundedUnitValue(TUnit unit, string propertyDisplayKey = nameof(Value), T? defaultValue = null)
 32        : base(propertyDisplayKey, defaultValue) => Unit = unit;
 33
 34    /// <summary>
 35    /// Initializes a new instance of the <see cref="BoundedUnitValue{T, TUnit}"/> class with bounds.
 36    /// </summary>
 37    /// <param name="unit">The measurement unit.</param>
 38    /// <param name="min">Inclusive minimum, if any.</param>
 39    /// <param name="max">Inclusive maximum, if any.</param>
 40    /// <param name="propertyDisplayKey">Translation key used in validation messages.</param>
 41    /// <param name="defaultValue">Optional default value.</param>
 42    public BoundedUnitValue(TUnit unit, T? min, T? max, string propertyDisplayKey = nameof(Value), T? defaultValue = nul
 43        : base(min, max, propertyDisplayKey, defaultValue) => Unit = unit;
 44
 45    /// <summary>
 46    /// Initializes a new instance of the <see cref="BoundedUnitValue{T, TUnit}"/> class.
 47    /// </summary>
 48    /// <param name="unit">The measurement unit.</param>
 49    /// <param name="range">Inclusive bounds.</param>
 50    /// <param name="propertyDisplayKey">Translation key used in validation messages.</param>
 51    /// <param name="defaultValue">Optional default value.</param>
 52    public BoundedUnitValue(TUnit unit, Interval<T> range, string propertyDisplayKey = nameof(Value), T? defaultValue = 
 53        : base(range, propertyDisplayKey, defaultValue) => Unit = unit;
 54
 55    /// <inheritdoc />
 56    public TUnit Unit { get; }
 57
 58    /// <inheritdoc />
 59    public T Convert(TUnit unit)
 60    {
 61        if (!Value.HasValue)
 62            return default;
 63
 64        var numeric = double.CreateChecked(Value.Value);
 65        return T.CreateChecked(numeric.To(Unit, unit));
 66    }
 67
 68    /// <summary>
 69    /// Normalizes using the default unit range.
 70    /// </summary>
 71    public IBoundedUnitValue<T, TUnit> Normalize() => Normalize(null, null);
 72
 73    /// <inheritdoc />
 74    public IBoundedUnitValue<T, TUnit> Normalize(TUnit? minUnit, TUnit? maxUnit)
 75    {
 76        if (!Value.HasValue)
 77            return this;
 78
 79        var numeric = double.CreateChecked(Value.Value);
 80        var (newValue, newUnit) = numeric.Simplify(Unit, minUnit, maxUnit);
 81        return new BoundedUnitValue<T, TUnit>(newUnit, Range) { Value = T.CreateChecked(newValue) };
 82    }
 83
 84    /// <inheritdoc />
 85    public override string? ToString() => Value.HasValue ? $"{Value} {Unit}" : null;
 86}
 87
 88/// <summary>
 89/// Bounded file-size value.
 90/// </summary>
 91/// <remarks>
 92/// Initializes a new instance of the <see cref="FileSizeValue"/> class.
 93/// </remarks>
 94public class FileSizeValue(DataSizeUnit unit = DataSizeUnit.Byte) : BoundedUnitValue<double, DataSizeUnit>(unit);
 95
 96/// <summary>
 97/// Bounded length value.
 98/// </summary>
 99public class LengthValue : BoundedUnitValue<double, LengthUnit>
 100{
 101    /// <summary>
 102    /// Initializes a new instance of the <see cref="LengthValue"/> class.
 103    /// </summary>
 104    public LengthValue(LengthUnit unit = LengthUnit.Meter)
 105        : base(unit) { }
 106
 107    /// <summary>
 108    /// Initializes a new instance of the <see cref="LengthValue"/> class with bounds.
 109    /// </summary>
 110    public LengthValue(double? min, double? max, LengthUnit unit = LengthUnit.Meter)
 111        : base(unit, min, max) { }
 112}
 113
 114/// <summary>
 115/// Bounded mass value.
 116/// </summary>
 117/// <remarks>
 118/// Initializes a new instance of the <see cref="MassValue"/> class.
 119/// </remarks>
 120public class MassValue(MassUnit unit = MassUnit.Gram) : BoundedUnitValue<double, MassUnit>(unit);
 121
 122/// <summary>
 123/// Bounded temperature value.
 124/// </summary>
 125/// <remarks>
 126/// Initializes a new instance of the <see cref="TemperatureValue"/> class.
 127/// </remarks>
 0128public class TemperatureValue(TemperatureUnit unit = TemperatureUnit.Celsius) : BoundedUnitValue<double, TemperatureUnit
 129