| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Quantity.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Primitives.Conversion; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides a factory method for creating instances of <see cref="Quantity{TUnit}"/>. The <see cref="Of{TUnit}"/> metho |
| | | 13 | | /// </summary> |
| | | 14 | | public static class Quantity |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates a new instance of <see cref="Quantity{TUnit}"/> with the specified value and unit. The <typeparamref nam |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="value">The numeric value of the quantity.</param> |
| | | 20 | | /// <param name="unit">The unit of measurement for the quantity.</param> |
| | | 21 | | /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam> |
| | | 22 | | /// <returns>A new instance of <see cref="Quantity{TUnit}"/> with the specified value and unit.</returns> |
| | | 23 | | public static Quantity<TUnit> Of<TUnit>(double value, TUnit unit) |
| | | 24 | | where TUnit : struct, Enum |
| | 75 | 25 | | => new(value, unit); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Represents a quantity of a specific unit. The <typeparamref name="TUnit"/> type parameter specifies the type of the |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="value">The numeric value of the quantity.</param> |
| | | 32 | | /// <param name="unit">The unit of measurement for the quantity.</param> |
| | | 33 | | /// <typeparam name="TUnit">The type of the unit, which must be an enum.</typeparam> |
| | | 34 | | public readonly struct Quantity<TUnit>(double value, TUnit unit) : IEquatable<Quantity<TUnit>>, IComparable<Quantity<TUn |
| | | 35 | | where TUnit : struct, Enum |
| | | 36 | | { |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the numeric value of the quantity. This property is read-only and is initialized through the constructor pa |
| | | 39 | | /// </summary> |
| | | 40 | | public double Value { get; } = value; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the unit of measurement for the quantity. This property is read-only and is initialized through the constru |
| | | 44 | | /// </summary> |
| | | 45 | | public TUnit Unit { get; } = unit; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Returns a string that represents the current quantity, formatted as "Value Unit". This method overrides the defa |
| | | 49 | | /// </summary> |
| | | 50 | | /// <returns>A string representation of the quantity.</returns> |
| | | 51 | | public override string ToString() => $"{Value} {Unit}"; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Determines whether the specified object is equal to the current quantity. This method overrides the default Equa |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="obj">The object to compare with the current quantity.</param> |
| | | 57 | | /// <returns><c>true</c> if the specified object is equal to the current quantity; otherwise, <c>false</c>.</returns |
| | | 58 | | public override bool Equals(object? obj) => obj is Quantity<TUnit> other && Equals(other); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Determines whether the specified quantity is equal to the current quantity. This method implements the <see cref |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="other">The quantity to compare with the current quantity.</param> |
| | | 64 | | /// <returns><c>true</c> if the specified quantity is equal to the current quantity; otherwise, <c>false</c>.</retur |
| | | 65 | | public bool Equals(Quantity<TUnit> other) |
| | | 66 | | { |
| | | 67 | | var converter = UnitConverterRegistry.Get<TUnit>(); |
| | | 68 | | |
| | | 69 | | return converter.Convert(Value, Unit, other.Unit).IsCloseTo(other.Value); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Returns a hash code for the current quantity. This method overrides the default <see cref="object.GetHashCode"/> |
| | | 74 | | /// </summary> |
| | | 75 | | /// <returns>A hash code for the current quantity.</returns> |
| | | 76 | | public override int GetHashCode() |
| | | 77 | | { |
| | | 78 | | var converter = UnitConverterRegistry.Get<TUnit>(); |
| | | 79 | | var canonicalValue = converter.Convert(Value, Unit, default); |
| | | 80 | | |
| | | 81 | | return HashCode.Combine(Math.Round(canonicalValue, 12), default(TUnit)); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Compares the current quantity with another quantity of the same type. This method implements the <see cref="ICom |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="other">The quantity to compare with the current quantity.</param> |
| | | 88 | | /// <returns>A signed integer that indicates the relative order of the quantities being compared.</returns> |
| | | 89 | | public int CompareTo(Quantity<TUnit> other) |
| | | 90 | | { |
| | | 91 | | var converter = UnitConverterRegistry.Get<TUnit>(); |
| | | 92 | | |
| | | 93 | | var left = converter.Convert(Value, Unit, other.Unit); |
| | | 94 | | |
| | | 95 | | return left.CompareTo(other.Value); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Determines whether two quantities are equal by comparing their values and units. This operator overload provides |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="left">The first quantity to compare.</param> |
| | | 102 | | /// <param name="right">The second quantity to compare.</param> |
| | | 103 | | /// <returns><c>true</c> if the specified quantities are equal; otherwise, <c>false</c>.</returns> |
| | | 104 | | public static bool operator ==(Quantity<TUnit> left, Quantity<TUnit> right) => left.Equals(right); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Determines whether two quantities are not equal by comparing their values and units. This operator overload prov |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="left">The first quantity to compare.</param> |
| | | 110 | | /// <param name="right">The second quantity to compare.</param> |
| | | 111 | | /// <returns><c>true</c> if the specified quantities are not equal; otherwise, <c>false</c>.</returns> |
| | | 112 | | public static bool operator !=(Quantity<TUnit> left, Quantity<TUnit> right) => !(left == right); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Determines whether one quantity is less than another quantity of the same type. This operator overload provides |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="left">The first quantity to compare.</param> |
| | | 118 | | /// <param name="right">The second quantity to compare.</param> |
| | | 119 | | /// <returns><c>true</c> if the left quantity is less than the right quantity; otherwise, <c>false</c>.</returns> |
| | | 120 | | public static bool operator <(Quantity<TUnit> left, Quantity<TUnit> right) => left.CompareTo(right) < 0; |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Determines whether one quantity is greater than another quantity of the same type. This operator overload provid |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="left">The first quantity to compare.</param> |
| | | 126 | | /// <param name="right">The second quantity to compare.</param> |
| | | 127 | | /// <returns><c>true</c> if the left quantity is greater than the right quantity; otherwise, <c>false</c>.</returns> |
| | | 128 | | public static bool operator >(Quantity<TUnit> left, Quantity<TUnit> right) => left.CompareTo(right) > 0; |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Determines whether one quantity is less than or equal to another quantity of the same type. This operator overlo |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="left">The first quantity to compare.</param> |
| | | 134 | | /// <param name="right">The second quantity to compare.</param> |
| | | 135 | | /// <returns><c>true</c> if the left quantity is less than or equal to the right quantity; otherwise, <c>false</c>.< |
| | | 136 | | public static bool operator <=(Quantity<TUnit> left, Quantity<TUnit> right) => left.CompareTo(right) <= 0; |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Determines whether one quantity is greater than or equal to another quantity of the same type. This operator ove |
| | | 140 | | /// </summary> |
| | | 141 | | /// <param name="left">The first quantity to compare.</param> |
| | | 142 | | /// <param name="right">The second quantity to compare.</param> |
| | | 143 | | /// <returns><c>true</c> if the left quantity is greater than or equal to the right quantity; otherwise, <c>false</c |
| | | 144 | | public static bool operator >=(Quantity<TUnit> left, Quantity<TUnit> right) => left.CompareTo(right) >= 0; |
| | | 145 | | } |
| | | 146 | | |