| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="QuantityExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Primitives.Conversion; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Primitives; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public static class QuantityExtensions |
| | | 15 | | { |
| | | 16 | | extension(int value) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates a new quantity with the specified value and unit. This method allows you to easily create a quantity |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="unit">The unit of measurement for the quantity.</param> |
| | | 22 | | /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam> |
| | | 23 | | /// <returns>A new instance of <see cref="Quantity"/> with the specified value and unit.</returns> |
| | | 24 | | public Quantity<TUnit> Of<TUnit>(TUnit unit) |
| | | 25 | | where TUnit : struct, Enum |
| | 18 | 26 | | => Quantity.Of(value, unit); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | extension(double value) |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Creates a new quantity with the specified value and unit. This method allows you to easily create a quantity |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="unit">The unit of measurement for the quantity.</param> |
| | | 35 | | /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam> |
| | | 36 | | /// <returns>A new instance of <see cref="Quantity"/> with the specified value and unit.</returns> |
| | | 37 | | public Quantity<TUnit> Of<TUnit>(TUnit unit) |
| | | 38 | | where TUnit : struct, Enum |
| | 18 | 39 | | => Quantity.Of(value, unit); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | extension(decimal value) |
| | | 43 | | { |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates a new quantity with the specified value and unit. This method allows you to easily create a quantity |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="unit">The unit of measurement for the quantity.</param> |
| | | 48 | | /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam> |
| | | 49 | | /// <returns>A new instance of <see cref="Quantity"/> with the specified value and unit.</returns> |
| | | 50 | | public Quantity<TUnit> Of<TUnit>(TUnit unit) |
| | | 51 | | where TUnit : struct, Enum |
| | 3 | 52 | | => Quantity.Of((double)value, unit); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | extension<TUnit>(Quantity<TUnit> quantity) |
| | | 56 | | where TUnit : struct, Enum |
| | | 57 | | { |
| | | 58 | | /// <summary> |
| | | 59 | | /// Converts the quantity to the specified target unit using the appropriate converter from the registry. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="target">The target unit to convert the quantity to.</param> |
| | | 62 | | /// <returns>A new <see cref="Quantity{TUnit}"/> instance representing the converted quantity.</returns> |
| | | 63 | | public Quantity<TUnit> To(TUnit target) |
| | | 64 | | { |
| | 3 | 65 | | var converter = UnitConverterRegistry.Get<TUnit>(); |
| | | 66 | | |
| | 3 | 67 | | var converted = converter.Convert(quantity.Value, quantity.Unit, target); |
| | | 68 | | |
| | 3 | 69 | | return new(converted, target); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Adds another quantity to this quantity, converting the other quantity to this quantity's unit if necessary. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="right">The quantity to add.</param> |
| | | 76 | | /// <returns>A new <see cref="Quantity{TUnit}"/> instance representing the sum of the two quantities.</returns> |
| | | 77 | | public Quantity<TUnit> Add(Quantity<TUnit> right) |
| | | 78 | | { |
| | 6 | 79 | | var converter = UnitConverterRegistry.Get<TUnit>(); |
| | | 80 | | |
| | 6 | 81 | | var rightValue = converter.Convert(right.Value, right.Unit, quantity.Unit); |
| | | 82 | | |
| | 6 | 83 | | return new(quantity.Value + rightValue, quantity.Unit); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Subtracts another quantity from this quantity, converting the other quantity to this quantity's unit if nece |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="right">The quantity to subtract.</param> |
| | | 90 | | /// <returns>A new <see cref="Quantity{TUnit}"/> instance representing the difference of the two quantities.</re |
| | | 91 | | public Quantity<TUnit> Subtract(Quantity<TUnit> right) |
| | | 92 | | { |
| | 3 | 93 | | var converter = UnitConverterRegistry.Get<TUnit>(); |
| | | 94 | | |
| | 3 | 95 | | var rightValue = converter.Convert(right.Value, right.Unit, quantity.Unit); |
| | | 96 | | |
| | 3 | 97 | | return new(quantity.Value - rightValue, quantity.Unit); |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |