Skip to content

refactor: rename to Quantity.DefaultProvider.Quantities #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public partial class Quantity
/// <summary>
/// Serves as a repository for predefined quantity conversion mappings, facilitating the automatic generation and retrieval of unit conversions in the UnitsNet library.
/// </summary>
internal static class Provider
internal static class DefaultProvider
{
/// <summary>
/// All QuantityInfo instances that are present in UnitsNet by default.
/// </summary>
internal static IReadOnlyList<QuantityInfo> DefaultQuantities => new QuantityInfo[]
{");
internal static IReadOnlyList<QuantityInfo> Quantities { get; } =
[");
foreach (var quantity in _quantities)
Writer.WL($@"
{quantity.Name}.Info,");
Writer.WL(@"
};
];
}
}");
return Writer.ToString();
Expand Down
7 changes: 4 additions & 3 deletions UnitsNet/CustomCode/UnitsNetSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public sealed class UnitsNetSetup
{
static UnitsNetSetup()
{
IReadOnlyCollection<QuantityInfo> quantityInfos = Quantity.Provider.DefaultQuantities;
IReadOnlyCollection<QuantityInfo> quantityInfos = Quantity.DefaultProvider.Quantities;

// note: in order to support the ConvertByAbbreviation, the unit converter should require a UnitParser in the constructor
var unitConverter = UnitConverter.CreateDefault();

Expand All @@ -37,7 +38,7 @@ public UnitsNetSetup(IEnumerable<QuantityInfo> quantityInfos, UnitConverter unit
{
var quantityInfoLookup = new QuantityInfoLookup(quantityInfos);
var unitAbbreviations = new UnitAbbreviationsCache(quantityInfoLookup);

UnitConverter = unitConverter;
UnitAbbreviations = unitAbbreviations;
Formatter = new QuantityFormatter(unitAbbreviations);
Expand Down Expand Up @@ -68,7 +69,7 @@ public UnitsNetSetup(IEnumerable<QuantityInfo> quantityInfos, UnitConverter unit
/// quantities.
/// </summary>
public UnitAbbreviationsCache UnitAbbreviations { get; }

/// <summary>
/// Converts a quantity to string using the specified format strings and culture-specific format providers.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions UnitsNet/GeneratedCode/Quantity.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions UnitsNet/QuantityInfoLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace UnitsNet;
/// </remarks>
internal class QuantityInfoLookup
{
private readonly QuantityInfo[] _quantities;
private readonly IReadOnlyList<QuantityInfo> _quantities;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a slight performance overhead of using the interface on the private field, but if you prefer clarity- it's fine by me..

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was collateral damage, I'll revert it. The idea was to accept IReadOnlyList as input and reuse instance if compatible, but I'll do the same for array instead.

private readonly Lazy<QuantityByNameLookupDictionary> _quantitiesByName;
private readonly Lazy<QuantityByTypeLookupDictionary> _quantitiesByType;
private readonly Lazy<QuantityByTypeLookupDictionary> _quantitiesByUnitType;
Expand Down Expand Up @@ -174,7 +174,7 @@ public IQuantity From(double value, UnitKey unit)
{
return GetUnitInfo(unit).From(value);
}

/// <summary>
/// Attempts to create a quantity from the specified value and unit.
/// </summary>
Expand All @@ -194,7 +194,7 @@ public bool TryFrom(double value, [NotNullWhen(true)] Enum? unit, [NotNullWhen(t
quantity = null;
return false;
}

if (!TryGetUnitInfo(unit, out UnitInfo? unitInfo))
{
quantity = null;
Expand Down Expand Up @@ -270,7 +270,7 @@ public UnitInfo GetUnitByName(string quantityName, string unitName)
Data = { ["quantityName"] = quantityName, ["unitName"] = unitName }
};
}

/// <summary>
/// Attempts to parse unit information based on its quantity and unit names.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet/UnitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void RegisterDefaultConversions(UnitConverter unitConverter)
if (unitConverter is null)
throw new ArgumentNullException(nameof(unitConverter));

foreach(var quantity in Quantity.Provider.DefaultQuantities)
foreach (var quantity in Quantity.DefaultProvider.Quantities)
{
var registerMethod = quantity.QuantityType.GetMethod(nameof(Length.RegisterDefaultConversions), BindingFlags.NonPublic | BindingFlags.Static);
registerMethod?.Invoke(null, new object[]{unitConverter});
Expand Down Expand Up @@ -369,7 +369,7 @@ public static bool TryConvertByName(double inputValue, string quantityName, stri
result = quantity.As(toUnitInfo.Value);
return true;
}

result = 0d;
return false;
}
Expand Down