Skip to content

Commit 33d5a51

Browse files
committed
Create QuantityType -> UnitInfo associations (DefaultUnits) in UnitSystem
- UnitSystem holds the list of default units for each (compatible?) quantity type and a method (GetDefaultUnitInfo) that provides the corresponding UnitInfo given a QuantityType - All methods in Quantity that use a UnitSystem now use the new GetDefaultUnitInfo - Added a method (in UnitSystem) for creating derived unit systems(WithDefaultUnit), providing a new QuantityType->UnitInfo association and optionally a new BaseUnits definition (as currently used by Equals and visible in public ctor- otherwise obsolete?)
1 parent 85a4723 commit 33d5a51

File tree

101 files changed

+1472
-1588
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1472
-1588
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,25 @@ private void GenerateInstanceConstructors()
183183
/// <param name=""value"">The numeric value to construct this quantity with.</param>
184184
/// <param name=""unitSystem"">The unit system to create the quantity with.</param>
185185
/// <exception cref=""ArgumentNullException"">The given <see cref=""UnitSystem""/> is null.</exception>
186-
/// <exception cref=""ArgumentException"">No unit was found for the given <see cref=""UnitSystem""/>.</exception>
186+
/// <exception cref=""ArgumentException"">No default unit was found for the given <see cref=""UnitSystem""/>.</exception>
187187
public {_quantity.Name}({_valueType} value, UnitSystem unitSystem)
188188
{{
189-
if(unitSystem == null) throw new ArgumentNullException(nameof(unitSystem));
190-
191-
var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
192-
var firstUnitInfo = unitInfos.FirstOrDefault();
189+
if(unitSystem == null) throw new ArgumentNullException(nameof(unitSystem));
193190
");
194191

195192
Writer.WL(_quantity.BaseType == "double"
196193
? @"
197-
_value = Guard.EnsureValidNumber(value, nameof(value));"
194+
_value = Guard.EnsureValidNumber(value, nameof(value));
195+
"
198196
: @"
199-
_value = value;");
200-
Writer.WL(@"
201-
_unit = firstUnitInfo?.Value ?? throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
202-
}
197+
_value = value;
198+
");
199+
200+
Writer.WL($@"
201+
var defaultUnitInfo = unitSystem.GetDefaultUnitInfo(QuantityType) as UnitInfo<{_unitEnumName}>;
202+
203+
_unit = defaultUnitInfo?.Value ?? throw new ArgumentException(""No default unit was defined for the given UnitSystem."", nameof(unitSystem));
204+
}}
203205
");
204206
}
205207

@@ -821,13 +823,12 @@ public double As(UnitSystem unitSystem)
821823
if(unitSystem == null)
822824
throw new ArgumentNullException(nameof(unitSystem));
823825
824-
var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
826+
var defaultUnitInfo = unitSystem.GetDefaultUnitInfo(QuantityType) as UnitInfo<{_unitEnumName}>;
825827
826-
var firstUnitInfo = unitInfos.FirstOrDefault();
827-
if(firstUnitInfo == null)
828-
throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
828+
if(defaultUnitInfo == null)
829+
throw new ArgumentException(""No default unit was found for the given UnitSystem."", nameof(unitSystem));
829830
830-
return As(firstUnitInfo.Value);
831+
return As(defaultUnitInfo.Value);
831832
}}
832833
833834
/// <inheritdoc />
@@ -864,13 +865,12 @@ IQuantity IQuantity.ToUnit(Enum unit)
864865
if(unitSystem == null)
865866
throw new ArgumentNullException(nameof(unitSystem));
866867
867-
var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
868+
var defaultUnitInfo = unitSystem.GetDefaultUnitInfo(QuantityType) as UnitInfo<{_unitEnumName}>;
868869
869-
var firstUnitInfo = unitInfos.FirstOrDefault();
870-
if(firstUnitInfo == null)
871-
throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
870+
if(defaultUnitInfo == null)
871+
throw new ArgumentException(""No default unit was found for the given UnitSystem."", nameof(unitSystem));
872872
873-
return ToUnit(firstUnitInfo.Value);
873+
return ToUnit(defaultUnitInfo.Value);
874874
}}
875875
876876
/// <inheritdoc />

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Xunit;
55
using UnitsNet.Units;
66
using System;
7+
using System.Linq;
78

89
namespace UnitsNet.Tests.CustomCode
910
{
@@ -186,6 +187,16 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
186187
Assert.Equal(LengthUnit.Meter, length.Unit);
187188
}
188189

190+
[Fact]
191+
public void Constructor_UnitSystemMySmallSI_AssignsMillimiters()
192+
{
193+
var myDefaultLengthUnit = Length.Info.UnitInfos.First(x => x.Value == LengthUnit.Millimeter);
194+
var myUnitSystem = UnitSystem.SI.WithDefaultUnit(QuantityType.Length, myDefaultLengthUnit);
195+
196+
var length = new Length(1.0, myUnitSystem);
197+
Assert.Equal(LengthUnit.Millimeter, length.Unit);
198+
}
199+
189200
[Fact]
190201
public void Constructor_UnitSystemWithNoMatchingBaseUnits_ThrowsArgumentException()
191202
{

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/Angle.g.cs

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)