From 6c5f25cb6d7cd92c194cf21e0c0145cb7af55448 Mon Sep 17 00:00:00 2001 From: William Bonazzoli Date: Sat, 11 Apr 2026 10:02:21 +0200 Subject: [PATCH 1/3] Added UnitInfo overload in UnitAbbreviationsCache Added UnitInfo overload of MapUnitToAbbreviation, MapUnitToDefaultAbbreviation, GetDefaultAbbreviation and GetUnitAbbreviations --- UnitsNet/CustomCode/UnitAbbreviationsCache.cs | 62 ++++++++++++++++--- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs index 073cb50f03..613386f8ff 100644 --- a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs +++ b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs @@ -146,7 +146,16 @@ public void MapUnitToAbbreviation(TUnitType unit, IFormatProvider? fo /// Unit abbreviations to add. public void MapUnitToAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider, params IEnumerable abbreviations) { - PerformAbbreviationMapping(unitKey, formatProvider, false, abbreviations); + MapUnitToAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviations); + } + + /// > + /// The info representing the unit. + /// The format provider to use for lookup. Defaults to if null. + /// Unit abbreviations to add. + public void MapUnitToAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider, params IEnumerable abbreviations) + { + AddAbbreviation(unitInfo, formatProvider, false, abbreviations); } #endregion @@ -212,16 +221,20 @@ public void MapUnitToDefaultAbbreviation(Type unitType, int unitValue, IFormatPr /// Unit abbreviation to add as default. public void MapUnitToDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider, string abbreviation) { - PerformAbbreviationMapping(unitKey, formatProvider, true, abbreviation); + MapUnitToDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviation); } - #endregion - - private void PerformAbbreviationMapping(UnitKey unitKey, IFormatProvider? formatProvider, bool setAsDefault, params IEnumerable abbreviations) + /// > + /// The info representing the unit. + /// The format provider to use for lookup. Defaults to if null. + /// Unit abbreviation to add as default. + public void MapUnitToDefaultAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider, string abbreviation) { - AddAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, setAsDefault, abbreviations); + AddAbbreviation(unitInfo, formatProvider, true, abbreviation); } - + + #endregion + /// /// Gets the default abbreviation for a given unit type and its numeric enum value. /// If a unit has more than one abbreviation defined, then it returns the first one. @@ -282,10 +295,25 @@ public string GetDefaultAbbreviation(Type unitType, int unitValue, IFormatProvid /// public string GetDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider = null) { - IReadOnlyList abbreviations = GetUnitAbbreviations(unitKey, formatProvider); + return GetDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider); + } + + /// + /// The info representing the unit. + /// The format provider to use for lookup. Defaults to if null. + /// + /// Thrown when no unit information is found for the specified + /// . + /// + /// + /// Thrown when no abbreviations are mapped for the specified unit. + /// + public string GetDefaultAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider = null) + { + IReadOnlyList abbreviations = GetUnitAbbreviations(unitInfo, formatProvider); if (abbreviations.Count == 0) { - throw new InvalidOperationException($"No abbreviations were found for {unitKey.UnitEnumType.Name}.{(Enum)unitKey}. Make sure that the unit abbreviations are mapped."); + throw new InvalidOperationException($"No abbreviations were found for {unitInfo.QuantityInfo.Name}.{unitInfo.Name}. Make sure that the unit abbreviations are mapped."); } return abbreviations[0]; @@ -341,13 +369,27 @@ public IReadOnlyList GetUnitAbbreviations(Type unitType, int unitValue, /// . /// public IReadOnlyList GetUnitAbbreviations(UnitKey unitKey, IFormatProvider? formatProvider = null) + { + return GetUnitAbbreviations(Quantities.GetUnitInfo(unitKey), formatProvider); + } + /// + /// Retrieves the unit abbreviations for a specified unit info and optional format provider. + /// + /// The key representing the unit info and value. + /// The format provider to use for lookup. Defaults to if null. + /// A read-only collection of unit abbreviation strings. + /// + /// Thrown when no unit information is found for the specified + /// . + /// + public IReadOnlyList GetUnitAbbreviations(UnitInfo unitInfo, IFormatProvider? formatProvider = null) { if (formatProvider is not CultureInfo culture) { culture = CultureInfo.CurrentCulture; } - return GetAbbreviationsWithFallbackCulture(Quantities.GetUnitInfo(unitKey), culture); + return GetAbbreviationsWithFallbackCulture(unitInfo, culture); } /// From 295e10c7a98188da8971f0b35460e49b377c0cd2 Mon Sep 17 00:00:00 2001 From: William Bonazzoli Date: Sat, 9 May 2026 11:24:49 +0200 Subject: [PATCH 2/3] Fixing doc comment typos and missing blank line --- UnitsNet/CustomCode/UnitAbbreviationsCache.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs index 613386f8ff..286b092e54 100644 --- a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs +++ b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs @@ -105,7 +105,7 @@ public void MapUnitToAbbreviation(TUnitType unit, params IEnumerable< MapUnitToAbbreviation(UnitKey.ForUnit(unit), abbreviations); } - /// > + /// /// The unit enum type. /// The unit enum value. /// The format provider to use for lookup. Defaults to if null. @@ -121,7 +121,7 @@ public void MapUnitToAbbreviation(Type unitType, int unitValue, IFormatProvider? MapUnitToAbbreviation(UnitKey.Create(unitType, unitValue), formatProvider, abbreviations); } - /// > + /// /// The unit key value. /// Unit abbreviations to add. public void MapUnitToAbbreviation(UnitKey unitKey, params IEnumerable abbreviations) @@ -129,7 +129,7 @@ public void MapUnitToAbbreviation(UnitKey unitKey, params IEnumerable ab MapUnitToAbbreviation(unitKey, CultureInfo.CurrentCulture, abbreviations); } - /// > + /// /// The unit enum value. /// The format provider to use for lookup. Defaults to if null. /// Unit abbreviations to add. @@ -140,7 +140,7 @@ public void MapUnitToAbbreviation(TUnitType unit, IFormatProvider? fo MapUnitToAbbreviation(UnitKey.ForUnit(unit), formatProvider, abbreviations); } - /// > + /// /// The unit key value. /// The format provider to use for lookup. Defaults to if null. /// Unit abbreviations to add. @@ -149,7 +149,7 @@ public void MapUnitToAbbreviation(UnitKey unitKey, IFormatProvider? formatProvid MapUnitToAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviations); } - /// > + /// /// The info representing the unit. /// The format provider to use for lookup. Defaults to if null. /// Unit abbreviations to add. @@ -180,7 +180,7 @@ public void MapUnitToDefaultAbbreviation(TUnitType unit, string abbre MapUnitToDefaultAbbreviation(UnitKey.ForUnit(unit), abbreviation); } - /// > + /// /// The unit key value. /// Unit abbreviations to add as default. public void MapUnitToDefaultAbbreviation(UnitKey unitKey, string abbreviation) @@ -188,7 +188,7 @@ public void MapUnitToDefaultAbbreviation(UnitKey unitKey, string abbreviation) MapUnitToDefaultAbbreviation(unitKey, CultureInfo.CurrentCulture, abbreviation); } - /// > + /// /// The unit enum value. /// The format provider to use for lookup. Defaults to if null. /// Unit abbreviation to add as default. @@ -199,7 +199,7 @@ public void MapUnitToDefaultAbbreviation(TUnitType unit, IFormatProvi MapUnitToDefaultAbbreviation(UnitKey.ForUnit(unit), formatProvider, abbreviation); } - /// > + /// /// The unit enum type. /// The unit enum value. /// The format provider to use for lookup. Defaults to if null. @@ -215,7 +215,7 @@ public void MapUnitToDefaultAbbreviation(Type unitType, int unitValue, IFormatPr MapUnitToDefaultAbbreviation(UnitKey.Create(unitType, unitValue), formatProvider, abbreviation); } - /// > + /// /// The unit key value. /// The format provider to use for lookup. Defaults to if null. /// Unit abbreviation to add as default. @@ -224,7 +224,7 @@ public void MapUnitToDefaultAbbreviation(UnitKey unitKey, IFormatProvider? forma MapUnitToDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviation); } - /// > + /// /// The info representing the unit. /// The format provider to use for lookup. Defaults to if null. /// Unit abbreviation to add as default. @@ -372,10 +372,11 @@ public IReadOnlyList GetUnitAbbreviations(UnitKey unitKey, IFormatProvid { return GetUnitAbbreviations(Quantities.GetUnitInfo(unitKey), formatProvider); } + /// /// Retrieves the unit abbreviations for a specified unit info and optional format provider. /// - /// The key representing the unit info and value. + /// The unit info object representing the unit. /// The format provider to use for lookup. Defaults to if null. /// A read-only collection of unit abbreviation strings. /// From 19065a638c89aa1bc265c76edd49ab292fc17922 Mon Sep 17 00:00:00 2001 From: William Bonazzoli Date: Sat, 9 May 2026 11:25:51 +0200 Subject: [PATCH 3/3] Adding GetDefaultAbbreviation(UnitInfo unitInfo) test --- UnitsNet.Tests/UnitAbbreviationsCacheTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs index d029208eb4..a830732a09 100644 --- a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs +++ b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs @@ -87,7 +87,8 @@ public void GetDefaultAbbreviationReturnsTheExpectedAbbreviationWhenConstructedW Assert.Multiple(checks: [ () => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(MassUnit.Gram, AmericanCulture)); }, - () => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(typeof(MassUnit), (int)MassUnit.Gram, AmericanCulture)); } + () => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(typeof(MassUnit), (int)MassUnit.Gram, AmericanCulture)); }, + () => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(Mass.Info[MassUnit.Gram])); } ]); }