Skip to content

Commit 6c5f25c

Browse files
committed
Added UnitInfo overload in UnitAbbreviationsCache
Added UnitInfo overload of MapUnitToAbbreviation, MapUnitToDefaultAbbreviation, GetDefaultAbbreviation and GetUnitAbbreviations
1 parent 414fe57 commit 6c5f25c

1 file changed

Lines changed: 52 additions & 10 deletions

File tree

UnitsNet/CustomCode/UnitAbbreviationsCache.cs

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ public void MapUnitToAbbreviation<TUnitType>(TUnitType unit, IFormatProvider? fo
146146
/// <param name="abbreviations">Unit abbreviations to add.</param>
147147
public void MapUnitToAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider, params IEnumerable<string> abbreviations)
148148
{
149-
PerformAbbreviationMapping(unitKey, formatProvider, false, abbreviations);
149+
MapUnitToAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviations);
150+
}
151+
152+
/// <inheritdoc cref="MapUnitToAbbreviation{TUnitType}(TUnitType,IEnumerable{string})"/>>
153+
/// <param name="unitInfo">The info representing the unit.</param>
154+
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
155+
/// <param name="abbreviations">Unit abbreviations to add.</param>
156+
public void MapUnitToAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider, params IEnumerable<string> abbreviations)
157+
{
158+
AddAbbreviation(unitInfo, formatProvider, false, abbreviations);
150159
}
151160

152161
#endregion
@@ -212,16 +221,20 @@ public void MapUnitToDefaultAbbreviation(Type unitType, int unitValue, IFormatPr
212221
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
213222
public void MapUnitToDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider, string abbreviation)
214223
{
215-
PerformAbbreviationMapping(unitKey, formatProvider, true, abbreviation);
224+
MapUnitToDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviation);
216225
}
217226

218-
#endregion
219-
220-
private void PerformAbbreviationMapping(UnitKey unitKey, IFormatProvider? formatProvider, bool setAsDefault, params IEnumerable<string> abbreviations)
227+
/// <inheritdoc cref="MapUnitToDefaultAbbreviation{TUnitType}(TUnitType,string)"/>>
228+
/// <param name="unitInfo">The info representing the unit.</param>
229+
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
230+
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
231+
public void MapUnitToDefaultAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider, string abbreviation)
221232
{
222-
AddAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, setAsDefault, abbreviations);
233+
AddAbbreviation(unitInfo, formatProvider, true, abbreviation);
223234
}
224-
235+
236+
#endregion
237+
225238
/// <summary>
226239
/// Gets the default abbreviation for a given unit type and its numeric enum value.
227240
/// 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
282295
/// </exception>
283296
public string GetDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider = null)
284297
{
285-
IReadOnlyList<string> abbreviations = GetUnitAbbreviations(unitKey, formatProvider);
298+
return GetDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider);
299+
}
300+
301+
/// <inheritdoc cref="GetDefaultAbbreviation{TUnitType}" />
302+
/// <param name="unitInfo">The info representing the unit.</param>
303+
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
304+
/// <exception cref="UnitNotFoundException">
305+
/// Thrown when no unit information is found for the specified
306+
/// <paramref name="unitInfo" />.
307+
/// </exception>
308+
/// <exception cref="InvalidOperationException">
309+
/// Thrown when no abbreviations are mapped for the specified unit.
310+
/// </exception>
311+
public string GetDefaultAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider = null)
312+
{
313+
IReadOnlyList<string> abbreviations = GetUnitAbbreviations(unitInfo, formatProvider);
286314
if (abbreviations.Count == 0)
287315
{
288-
throw new InvalidOperationException($"No abbreviations were found for {unitKey.UnitEnumType.Name}.{(Enum)unitKey}. Make sure that the unit abbreviations are mapped.");
316+
throw new InvalidOperationException($"No abbreviations were found for {unitInfo.QuantityInfo.Name}.{unitInfo.Name}. Make sure that the unit abbreviations are mapped.");
289317
}
290318

291319
return abbreviations[0];
@@ -341,13 +369,27 @@ public IReadOnlyList<string> GetUnitAbbreviations(Type unitType, int unitValue,
341369
/// <paramref name="unitKey" />.
342370
/// </exception>
343371
public IReadOnlyList<string> GetUnitAbbreviations(UnitKey unitKey, IFormatProvider? formatProvider = null)
372+
{
373+
return GetUnitAbbreviations(Quantities.GetUnitInfo(unitKey), formatProvider);
374+
}
375+
/// <summary>
376+
/// Retrieves the unit abbreviations for a specified unit info and optional format provider.
377+
/// </summary>
378+
/// <param name="unitInfo">The key representing the unit info and value.</param>
379+
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
380+
/// <returns>A read-only collection of unit abbreviation strings.</returns>
381+
/// <exception cref="UnitNotFoundException">
382+
/// Thrown when no unit information is found for the specified
383+
/// <paramref name="unitInfo" />.
384+
/// </exception>
385+
public IReadOnlyList<string> GetUnitAbbreviations(UnitInfo unitInfo, IFormatProvider? formatProvider = null)
344386
{
345387
if (formatProvider is not CultureInfo culture)
346388
{
347389
culture = CultureInfo.CurrentCulture;
348390
}
349391

350-
return GetAbbreviationsWithFallbackCulture(Quantities.GetUnitInfo(unitKey), culture);
392+
return GetAbbreviationsWithFallbackCulture(unitInfo, culture);
351393
}
352394

353395
/// <summary>

0 commit comments

Comments
 (0)