Skip to content

Commit 5d65646

Browse files
authored
Constraint the Abs extension method to ILinearQuantity<TQuantity> (#1607)
- replace the constraint from `IQuantity` to `ILinearQuantity<TQuantity>` - moved the extension from `UnitMath` class to the `LinearQuantityExtensions`
1 parent 342641e commit 5d65646

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

UnitsNet.Tests/UnitMathTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ public void AbsoluteValueOfNegativeReturnsPositive()
3737
Assert.StrictEqual(-quantity, result);
3838
}
3939

40-
[Fact]
41-
public void AbsoluteValueOfNullReferenceThrowsException()
42-
{
43-
IQuantity quantity = null!;
44-
45-
Assert.Throws<NullReferenceException>(() => quantity.Abs());
46-
}
47-
4840
[Fact]
4941
public void AverageOfEmptySourceThrowsException()
5042
{

UnitsNet/Extensions/LinearQuantityExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,29 @@ public static TQuantity Average<TSource, TQuantity, TUnit>(this IEnumerable<TSou
255255
{
256256
return source.Select(selector).Average(targetUnit);
257257
}
258+
259+
/// <summary>
260+
/// Returns the absolute value of the specified quantity.
261+
/// </summary>
262+
/// <typeparam name="TQuantity">
263+
/// The type of the quantity, which must implement <see cref="ILinearQuantity{TSelf}" />.
264+
/// </typeparam>
265+
/// <param name="value">
266+
/// The quantity whose absolute value is to be calculated.
267+
/// </param>
268+
/// <returns>
269+
/// A quantity of type <typeparamref name="TQuantity" /> representing the absolute value of the input quantity.
270+
/// </returns>
271+
/// <exception cref="NullReferenceException">
272+
/// Thrown if the input <paramref name="value" /> is <c>null</c>.
273+
/// </exception>
274+
public static TQuantity Abs<TQuantity>(this TQuantity value)
275+
where TQuantity : ILinearQuantity<TQuantity>
276+
{
277+
#if NET
278+
return TQuantity.Create(Math.Abs(value.Value), value.UnitKey);
279+
#else
280+
return value.QuantityInfo.Create(Math.Abs(value.Value), value.UnitKey);
281+
#endif
282+
}
258283
}

UnitsNet/UnitMath.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,6 @@ namespace UnitsNet
1010
/// </summary>
1111
public static class UnitMath
1212
{
13-
/// <summary>
14-
/// Returns the absolute value of the specified quantity.
15-
/// </summary>
16-
/// <typeparam name="TQuantity">
17-
/// The type of the quantity, which must implement <see cref="IQuantity" />.
18-
/// </typeparam>
19-
/// <param name="value">
20-
/// The quantity whose absolute value is to be calculated.
21-
/// </param>
22-
/// <returns>
23-
/// A quantity of type <typeparamref name="TQuantity" /> representing the absolute value of the input quantity.
24-
/// </returns>
25-
/// <exception cref="NullReferenceException">
26-
/// Thrown if the input <paramref name="value" /> is <c>null</c>.
27-
/// </exception>
28-
public static TQuantity Abs<TQuantity>(this TQuantity value) where TQuantity : IQuantity
29-
{
30-
// TODO see about constraining to IQuantityInstance<TQuantity>
31-
// #if NET
32-
// return TQuantity.Create(QuantityValue.Abs(value.Value), value.UnitKey);
33-
// #else
34-
// return value.QuantityInfo.Create(QuantityValue.Abs(value.Value), value.UnitKey);
35-
// #endif
36-
return (TQuantity)value.QuantityInfo.From(Math.Abs(value.Value), value.UnitKey);
37-
}
38-
3913
/// <summary>Returns the smaller of two <typeparamref name="TQuantity" /> values.</summary>
4014
/// <typeparam name="TQuantity">The type of quantities to compare.</typeparam>
4115
/// <param name="val1">The first of two <typeparamref name="TQuantity" /> values to compare.</param>

0 commit comments

Comments
 (0)