Skip to content

Commit f5682d8

Browse files
committed
Simplify interfaces
1 parent 60a875a commit f5682d8

7 files changed

Lines changed: 89 additions & 117 deletions

File tree

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ namespace UnitsNet
6060
/// </remarks>");
6161

6262
Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity));
63-
Writer.WL(@$"
63+
Writer.W(@$"
6464
[DataContract]
6565
[DebuggerTypeProxy(typeof(QuantityDisplay))]
66-
public readonly partial struct {_quantity.Name} :");
66+
public readonly partial struct {_quantity.Name}");
6767
GenerateInterfaceExtensions();
6868

6969
Writer.WL($@"
@@ -103,62 +103,47 @@ namespace UnitsNet
103103

104104
private void GenerateInterfaceExtensions()
105105
{
106-
// generate the base interface (either IVectorQuantity, IAffineQuantity or ILogarithmicQuantity)
106+
Writer.W($" : IQuantity<{_quantity.Name}, {_unitEnumName}>, ");
107+
108+
// generate ILogarithmicQuantity, IAffineQuantity or ILinearQuantity
107109
if (_quantity.Logarithmic)
108110
{
109-
Writer.WL(@$"
110-
ILogarithmicQuantity<{_quantity.Name}, {_unitEnumName}>,");
111+
Writer.WL($"ILogarithmicQuantity<{_quantity.Name}>");
111112
}
112113
else if (!string.IsNullOrEmpty(_quantity.AffineOffsetType))
113114
{
114-
Writer.WL(@$"
115-
IAffineQuantity<{_quantity.Name}, {_unitEnumName}, {_quantity.AffineOffsetType}>,");
115+
Writer.WL($"IAffineQuantity<{_quantity.Name}, {_quantity.AffineOffsetType}>");
116116
}
117-
else // the default quantity type implements the IVectorQuantity interface
117+
else // the default quantity type implements the ILinearQuantity interface
118118
{
119-
Writer.WL(@$"
120-
IArithmeticQuantity<{_quantity.Name}, {_unitEnumName}>,");
121-
}
122-
123-
Writer.WL(@"
124-
#if NET7_0_OR_GREATER");
125-
if (!_quantity.IsAffine)
126-
{
127-
Writer.WL($@"
128-
IDivisionOperators<{_quantity.Name}, {_quantity.Name}, double>,");
119+
Writer.WL($"ILinearQuantity<{_quantity.Name}>");
129120
}
130121

131122
if (_quantity.Relations.Any(r => r.Operator is "*" or "/"))
132123
{
124+
Writer.WL(@"
125+
#if NET7_0_OR_GREATER");
133126
foreach (QuantityRelation relation in _quantity.Relations)
134127
{
135128
if (relation.LeftQuantity != _quantity) continue;
136129
switch (relation.Operator)
137130
{
138131
case "*":
139132
Writer.W(@"
140-
IMultiplyOperators");
133+
, IMultiplyOperators");
141134
break;
142135
case "/":
143136
Writer.W(@"
144-
IDivisionOperators");
137+
, IDivisionOperators");
145138
break;
146139
default:
147140
continue;
148141
}
149-
150-
Writer.WL($"<{relation.LeftQuantity.Name}, {relation.RightQuantity.Name}, {relation.ResultQuantity.Name}>,");
142+
Writer.WL($"<{relation.LeftQuantity.Name}, {relation.RightQuantity.Name}, {relation.ResultQuantity.Name}>");
151143
}
144+
Writer.WL(@$"
145+
#endif");
152146
}
153-
154-
Writer.WL(@$"
155-
IComparisonOperators<{_quantity.Name}, {_quantity.Name}, bool>,
156-
IParsable<{_quantity.Name}>,
157-
#endif
158-
IComparable,
159-
IComparable<{_quantity.Name}>,
160-
IEquatable<{_quantity.Name}>,
161-
IFormattable");
162147
}
163148

164149
private void GenerateQuantityInfo()

UnitsNet/Extensions/LogarithmicQuantityExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static TQuantity Sum<TSource, TQuantity>(this IEnumerable<TSource> source
176176
/// logarithmic space.
177177
/// </remarks>
178178
public static TQuantity Sum<TQuantity, TUnit>(this IEnumerable<TQuantity> quantities, TUnit targetUnit)
179-
where TQuantity : ILogarithmicQuantity<TQuantity, TUnit>
179+
where TQuantity : ILogarithmicQuantity<TQuantity>, IQuantity<TQuantity, TUnit>
180180
where TUnit : struct, Enum
181181
{
182182
if (quantities is null)
@@ -224,7 +224,7 @@ public static TQuantity Sum<TQuantity, TUnit>(this IEnumerable<TQuantity> quanti
224224
/// logarithmic space.
225225
/// </remarks>
226226
public static TQuantity Sum<TSource, TQuantity, TUnit>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, TUnit targetUnit)
227-
where TQuantity : ILogarithmicQuantity<TQuantity, TUnit>
227+
where TQuantity : ILogarithmicQuantity<TQuantity>, IQuantity<TQuantity, TUnit>
228228
where TUnit : struct, Enum
229229
{
230230
return source.Select(selector).Sum(targetUnit);
@@ -312,7 +312,7 @@ public static TQuantity ArithmeticMean<TSource, TQuantity>(this IEnumerable<TSou
312312
/// averaged, and then the result is converted back to logarithmic space using the same unit.
313313
/// </remarks>
314314
public static TQuantity ArithmeticMean<TQuantity, TUnit>(this IEnumerable<TQuantity> quantities, TUnit unit)
315-
where TQuantity : ILogarithmicQuantity<TQuantity, TUnit>
315+
where TQuantity : ILogarithmicQuantity<TQuantity>, IQuantity<TQuantity, TUnit>
316316
where TUnit : struct, Enum
317317
{
318318
if (quantities is null)
@@ -363,7 +363,7 @@ public static TQuantity ArithmeticMean<TQuantity, TUnit>(this IEnumerable<TQuant
363363
/// averaged, and then the result is converted back to logarithmic space using the same unit.
364364
/// </remarks>
365365
public static TQuantity ArithmeticMean<TSource, TQuantity, TUnit>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, TUnit targetUnit)
366-
where TQuantity : ILogarithmicQuantity<TQuantity, TUnit>
366+
where TQuantity : ILogarithmicQuantity<TQuantity>, IQuantity<TQuantity, TUnit>
367367
where TUnit : struct, Enum
368368
{
369369
return ArithmeticMean(source.Select(selector), targetUnit);
@@ -448,7 +448,7 @@ public static TQuantity GeometricMean<TSource, TQuantity>(this IEnumerable<TSour
448448
/// logarithmic quantities is equal to the sum the values, converted in unit of the first element.
449449
/// </remarks>
450450
public static TQuantity GeometricMean<TQuantity, TUnit>(this IEnumerable<TQuantity> quantities, TUnit targetUnit)
451-
where TQuantity : ILogarithmicQuantity<TQuantity, TUnit>
451+
where TQuantity : ILogarithmicQuantity<TQuantity>, IQuantity<TQuantity, TUnit>
452452
where TUnit : struct, Enum
453453
{
454454
if (quantities is null)
@@ -494,7 +494,7 @@ public static TQuantity GeometricMean<TQuantity, TUnit>(this IEnumerable<TQuanti
494494
/// logarithmic quantities is equal to the sum the values, converted in unit of the first element.
495495
/// </remarks>
496496
public static TQuantity GeometricMean<TSource, TQuantity, TUnit>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, TUnit targetUnit)
497-
where TQuantity : ILogarithmicQuantity<TQuantity, TUnit>
497+
where TQuantity : ILogarithmicQuantity<TQuantity>, IQuantity<TQuantity, TUnit>
498498
where TUnit : struct, Enum
499499
{
500500
return source.Select(selector).GeometricMean(targetUnit);

UnitsNet/IAffineQuantity.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,6 @@
77

88
namespace UnitsNet;
99

10-
/// <summary>
11-
/// An <see cref="IQuantity{TSelf, TUnitType}"/> that (in .NET 7+) implements generic math interfaces for arithmetic operations.
12-
/// </summary>
13-
/// <typeparam name="TSelf">The type itself, for the CRT pattern.</typeparam>
14-
/// <typeparam name="TUnitType">The underlying unit enum type.</typeparam>
15-
/// <typeparam name="TOffset"></typeparam>
16-
public interface IAffineQuantity<TSelf, TUnitType, TOffset> : IQuantity<TSelf, TUnitType>, IAffineQuantity<TSelf, TOffset>
17-
#if NET7_0_OR_GREATER
18-
, IAdditionOperators<TSelf, TOffset, TSelf>
19-
, ISubtractionOperators<TSelf, TSelf, TOffset>
20-
where TOffset : IAdditiveIdentity<TOffset, TOffset>
21-
#endif
22-
where TSelf : IAffineQuantity<TSelf, TUnitType, TOffset>
23-
where TUnitType : struct, Enum
24-
{
25-
}
26-
2710
/// <summary>
2811
/// An <see cref="IQuantity{TSelf}"/> that (in .NET 7+) implements generic math interfaces for arithmetic operations.
2912
/// </summary>
@@ -32,6 +15,8 @@ public interface IAffineQuantity<TSelf, TUnitType, TOffset> : IQuantity<TSelf, T
3215
public interface IAffineQuantity<TSelf, TOffset> : IQuantityOfType<TSelf>
3316
#if NET7_0_OR_GREATER
3417
, IAdditiveIdentity<TSelf, TOffset>
18+
, IAdditionOperators<TSelf, TOffset, TSelf>
19+
, ISubtractionOperators<TSelf, TSelf, TOffset>
3520
where TOffset : IAdditiveIdentity<TOffset, TOffset>
3621
#endif
3722
where TSelf : IAffineQuantity<TSelf, TOffset>

UnitsNet/IArithmeticQuantity.cs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,19 @@
77

88
namespace UnitsNet;
99

10-
/// <summary>
11-
/// Represents a quantity that has both magnitude and direction, supporting various arithmetic operations and
12-
/// comparisons.
13-
/// </summary>
14-
/// <remarks>
15-
/// This interface defines standard linear arithmetic operations such as addition, subtraction, multiplication, and
16-
/// division.
17-
/// These types of quantities naturally support comparison operations with either absolute or relative tolerance, which
18-
/// is useful for determining equality within a certain margin of error.
19-
/// <para>
20-
/// For more information, see the Wikipedia page on
21-
/// <a href="https://en.wikipedia.org/wiki/Dimensional_analysis#Geometry:_position_vs._displacement">
22-
/// Dimensional
23-
/// Analysis
24-
/// </a>
25-
/// .
26-
/// </para>
27-
/// </remarks>
28-
/// <typeparam name="TSelf">The type that implements this interface.</typeparam>
29-
public interface ILinearQuantity<TSelf> : IQuantityOfType<TSelf>
30-
#if NET7_0_OR_GREATER
31-
, IAdditiveIdentity<TSelf, TSelf>
32-
#endif
33-
where TSelf : ILinearQuantity<TSelf>
34-
{
35-
#if NET7_0_OR_GREATER
36-
/// <summary>
37-
/// The zero value of this quantity.
38-
/// </summary>
39-
static abstract TSelf Zero { get; }
40-
41-
static TSelf IAdditiveIdentity<TSelf, TSelf>.AdditiveIdentity
42-
{
43-
get => TSelf.Zero;
44-
}
45-
46-
#endif
47-
}
48-
4910
/// <summary>
5011
/// An <see cref="IQuantity{TSelf, TUnitType}" /> that (in .NET 7+) implements generic math interfaces for arithmetic
5112
/// operations.
5213
/// </summary>
5314
/// <typeparam name="TSelf">The type itself, for the CRT pattern.</typeparam>
54-
/// <typeparam name="TUnitType">The underlying unit enum type.</typeparam>
55-
public interface IArithmeticQuantity<TSelf, TUnitType> : IQuantity<TSelf, TUnitType>, ILinearQuantity<TSelf>
15+
public interface IArithmeticQuantity<TSelf>
5616
#if NET7_0_OR_GREATER
57-
, IAdditionOperators<TSelf, TSelf, TSelf>
17+
: IAdditionOperators<TSelf, TSelf, TSelf>
5818
, ISubtractionOperators<TSelf, TSelf, TSelf>
5919
, IMultiplyOperators<TSelf, double, TSelf>
6020
, IDivisionOperators<TSelf, double, TSelf>
6121
, IUnaryNegationOperators<TSelf, TSelf>
6222
#endif
63-
where TSelf : IArithmeticQuantity<TSelf, TUnitType>
64-
where TUnitType : struct, Enum
23+
where TSelf : IArithmeticQuantity<TSelf>
6524
{
6625
}

UnitsNet/ILinearQuantity.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
#if NET
5+
using System.Numerics;
6+
#endif
7+
8+
namespace UnitsNet;
9+
10+
/// <summary>
11+
/// Represents a quantity that has both magnitude and direction, supporting various arithmetic operations and
12+
/// comparisons.
13+
/// </summary>
14+
/// <remarks>
15+
/// This interface defines standard linear arithmetic operations such as addition, subtraction, multiplication, and
16+
/// division.
17+
/// These types of quantities naturally support comparison operations with either absolute or relative tolerance, which
18+
/// is useful for determining equality within a certain margin of error.
19+
/// <para>
20+
/// For more information, see the Wikipedia page on
21+
/// <a href="https://en.wikipedia.org/wiki/Dimensional_analysis#Geometry:_position_vs._displacement">
22+
/// Dimensional
23+
/// Analysis
24+
/// </a>
25+
/// .
26+
/// </para>
27+
/// </remarks>
28+
/// <typeparam name="TSelf">The type that implements this interface.</typeparam>
29+
public interface ILinearQuantity<TSelf> : IQuantityOfType<TSelf>, IArithmeticQuantity<TSelf>
30+
#if NET7_0_OR_GREATER
31+
, IAdditiveIdentity<TSelf, TSelf>
32+
#endif
33+
where TSelf : ILinearQuantity<TSelf>
34+
{
35+
#if NET7_0_OR_GREATER
36+
/// <summary>
37+
/// The zero value of this quantity.
38+
/// </summary>
39+
static abstract TSelf Zero { get; }
40+
41+
static TSelf IAdditiveIdentity<TSelf, TSelf>.AdditiveIdentity
42+
{
43+
get => TSelf.Zero;
44+
}
45+
46+
#endif
47+
}

UnitsNet/ILogarithmicQuantity.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@
77

88
namespace UnitsNet;
99

10-
/// <inheritdoc cref="ILogarithmicQuantity{TSelf}"/>
11-
/// <typeparam name="TSelf">The type itself, for the CRT pattern.</typeparam>
12-
/// <typeparam name="TUnitType">The underlying unit enum type.</typeparam>
13-
public interface ILogarithmicQuantity<TSelf, TUnitType> : IQuantity<TSelf, TUnitType>, ILogarithmicQuantity<TSelf>
14-
#if NET7_0_OR_GREATER
15-
, IAdditionOperators<TSelf, TSelf, TSelf>
16-
, ISubtractionOperators<TSelf, TSelf, TSelf>
17-
, IMultiplyOperators<TSelf, double, TSelf>
18-
, IDivisionOperators<TSelf, double, TSelf>
19-
, IUnaryNegationOperators<TSelf, TSelf>
20-
#endif
21-
where TSelf : ILogarithmicQuantity<TSelf, TUnitType>
22-
where TUnitType : struct, Enum
23-
{
24-
}
25-
2610
/// <summary>
2711
/// Represents a logarithmic quantity that supports arithmetic operations and implements generic math interfaces
2812
/// (in .NET 7+). This interface is designed for quantities that are logarithmic in nature, such as decibels.
@@ -34,7 +18,7 @@ public interface ILogarithmicQuantity<TSelf, TUnitType> : IQuantity<TSelf, TUnit
3418
/// to logarithmic quantities, including arithmetic operations and a logarithmic scaling factor.
3519
/// The logarithmic scale assumed here is base-10.
3620
/// </remarks>
37-
public interface ILogarithmicQuantity<TSelf> : IQuantityOfType<TSelf>
21+
public interface ILogarithmicQuantity<TSelf> : IQuantityOfType<TSelf>, IArithmeticQuantity<TSelf>
3822
#if NET7_0_OR_GREATER
3923
, IMultiplicativeIdentity<TSelf, TSelf>
4024
#endif

UnitsNet/IQuantity.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System.Numerics;
5+
46
namespace UnitsNet
57
{
68
/// <summary>
79
/// Represents a quantity.
810
/// </summary>
9-
public interface IQuantity : IFormattable
11+
public interface IQuantity : IComparable, IFormattable
1012
{
1113
/// <summary>
1214
/// Information about the quantity type, such as unit values and names.
@@ -117,8 +119,18 @@ Enum IQuantity.Unit
117119
/// methods, without having to include the unit type as additional generic parameter.
118120
/// </remarks>
119121
/// <typeparam name="TQuantity"></typeparam>
120-
public interface IQuantityOfType<out TQuantity> : IQuantity
122+
public interface IQuantityOfType<TQuantity> : IQuantity
123+
#if NET7_0_OR_GREATER
124+
, IComparisonOperators<TQuantity, TQuantity, bool>
125+
, IParsable<TQuantity>
126+
#endif
127+
, IComparable<TQuantity>
128+
, IEquatable<TQuantity>
121129
where TQuantity : IQuantity
130+
#if NET7_0_OR_GREATER
131+
, IComparisonOperators<TQuantity, TQuantity, bool>
132+
, IParsable<TQuantity>
133+
#endif
122134
{
123135
#if NET
124136
/// <summary>

0 commit comments

Comments
 (0)