Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions UnitsNet/CustomCode/UnitAbbreviationsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,16 @@ public void MapUnitToAbbreviation<TUnitType>(TUnitType unit, IFormatProvider? fo
/// <param name="abbreviations">Unit abbreviations to add.</param>
public void MapUnitToAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider, params IEnumerable<string> abbreviations)
{
PerformAbbreviationMapping(unitKey, formatProvider, false, abbreviations);
MapUnitToAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, abbreviations);
}

/// <inheritdoc cref="MapUnitToAbbreviation{TUnitType}(TUnitType,IEnumerable{string})"/>>
/// <param name="unitInfo">The info representing the unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <param name="abbreviations">Unit abbreviations to add.</param>
public void MapUnitToAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider, params IEnumerable<string> abbreviations)
{
AddAbbreviation(unitInfo, formatProvider, false, abbreviations);
}

#endregion
Expand Down Expand Up @@ -212,16 +221,20 @@ public void MapUnitToDefaultAbbreviation(Type unitType, int unitValue, IFormatPr
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
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<string> abbreviations)
/// <inheritdoc cref="MapUnitToDefaultAbbreviation{TUnitType}(TUnitType,string)"/>>
/// <param name="unitInfo">The info representing the unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
public void MapUnitToDefaultAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider, string abbreviation)
{
AddAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider, setAsDefault, abbreviations);
AddAbbreviation(unitInfo, formatProvider, true, abbreviation);
}


#endregion

/// <summary>
/// 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.
Expand Down Expand Up @@ -282,10 +295,25 @@ public string GetDefaultAbbreviation(Type unitType, int unitValue, IFormatProvid
/// </exception>
public string GetDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatProvider = null)
{
IReadOnlyList<string> abbreviations = GetUnitAbbreviations(unitKey, formatProvider);
return GetDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider);
}

/// <inheritdoc cref="GetDefaultAbbreviation{TUnitType}" />
/// <param name="unitInfo">The info representing the unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <exception cref="UnitNotFoundException">
/// Thrown when no unit information is found for the specified
/// <paramref name="unitInfo" />.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown when no abbreviations are mapped for the specified unit.
/// </exception>
public string GetDefaultAbbreviation(UnitInfo unitInfo, IFormatProvider? formatProvider = null)
{
IReadOnlyList<string> 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];
Expand Down Expand Up @@ -341,13 +369,27 @@ public IReadOnlyList<string> GetUnitAbbreviations(Type unitType, int unitValue,
/// <paramref name="unitKey" />.
/// </exception>
public IReadOnlyList<string> GetUnitAbbreviations(UnitKey unitKey, IFormatProvider? formatProvider = null)
{
return GetUnitAbbreviations(Quantities.GetUnitInfo(unitKey), formatProvider);
}
/// <summary>
/// Retrieves the unit abbreviations for a specified unit info and optional format provider.
/// </summary>
/// <param name="unitInfo">The key representing the unit info and value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <returns>A read-only collection of unit abbreviation strings.</returns>
/// <exception cref="UnitNotFoundException">
/// Thrown when no unit information is found for the specified
/// <paramref name="unitInfo" />.
/// </exception>
public IReadOnlyList<string> 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);
}

/// <summary>
Expand Down