Skip to content

Commit

Permalink
♻️Replace obsolete method usages with UnitsNetSetup.Default
Browse files Browse the repository at this point in the history
Replace obsolete static methods for UnitAbbreviations, QuantityParser, UnitParser and UnitConverter, with the new `UnitsNetSetup.Default` static instance.
  • Loading branch information
angularsen committed Dec 27, 2024
1 parent 89c3017 commit 52aba98
Show file tree
Hide file tree
Showing 269 changed files with 949 additions and 810 deletions.
10 changes: 5 additions & 5 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public static string GetAbbreviation({_unitEnumName} unit)
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static string GetAbbreviation({_unitEnumName} unit, IFormatProvider? provider)
{{
return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider);
return UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit, provider);
}}
#endregion
Expand Down Expand Up @@ -484,7 +484,7 @@ private void GenerateStaticParseMethods()
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static {_quantity.Name} Parse(string str, IFormatProvider? provider)
{{
return QuantityParser.Default.Parse<{_quantity.Name}, {_unitEnumName}>(
return UnitsNetSetup.Default.QuantityParser.Parse<{_quantity.Name}, {_unitEnumName}>(
str,
provider,
From);
Expand Down Expand Up @@ -515,7 +515,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
{{
return QuantityParser.Default.TryParse<{_quantity.Name}, {_unitEnumName}>(
return UnitsNetSetup.Default.QuantityParser.TryParse<{_quantity.Name}, {_unitEnumName}>(
str,
provider,
From,
Expand Down Expand Up @@ -548,7 +548,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
public static {_unitEnumName} ParseUnit(string str, IFormatProvider? provider)
{{
return UnitParser.Default.Parse<{_unitEnumName}>(str, provider);
return UnitsNetSetup.Default.UnitParser.Parse<{_unitEnumName}>(str, provider);
}}
/// <inheritdoc cref=""TryParseUnit(string,IFormatProvider,out UnitsNet.Units.{_unitEnumName})""/>
Expand All @@ -569,7 +569,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
{{
return UnitParser.Default.TryParse<{_unitEnumName}>(str, provider, out unit);
return UnitsNetSetup.Default.UnitParser.TryParse<{_unitEnumName}>(str, provider, out unit);
}}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType,
if (!typeof(IQuantity).IsAssignableFrom(quantityType))
return false;
var parser = QuantityParser.Default;
var parser = UnitsNetSetup.Default.QuantityParser;
return quantityType switch
{");
Expand Down
3 changes: 2 additions & 1 deletion CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public string Generate()
using System.Globalization;
using System.Linq;
using System.Threading;
using UnitsNet.Tests.Helpers;
using UnitsNet.Tests.TestsBase;
using UnitsNet.Units;
using Xunit;
Expand Down Expand Up @@ -584,7 +585,7 @@ public void HasAtLeastOneAbbreviationSpecified()
var units = Enum.GetValues(typeof({_unitEnumName})).Cast<{_unitEnumName}>();
foreach (var unit in units)
{{
var defaultAbbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit);
var defaultAbbreviation = UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit);
}}
}}
Expand Down
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ if (Quantity.TryParse(typeof(Length), "3cm", out IQuantity quantity2)
[UnitParser](UnitsNet/CustomCode/UnitParser.cs) parses unit abbreviation strings to unit enum values.

```c#
Enum unit = UnitParser.Default.Parse("cm", typeof(LengthUnit)); // LengthUnit.Centimeter
Enum unit = UnitsNetSetup.Default.UnitParser.Parse("cm", typeof(LengthUnit)); // LengthUnit.Centimeter
if (UnitParser.Default.TryParse("cm", typeof(LengthUnit), out Enum unit2))
if (UnitsNetSetup.Default.UnitParser.TryParse("cm", typeof(LengthUnit), out Enum unit2))
{
// Use unit2 as LengthUnit.Centimeter
}
Expand Down Expand Up @@ -277,14 +277,16 @@ Read more at [Extending-with-Custom-Units](https://github.com/angularsen/UnitsNe
#### Map between unit enum values and unit abbreviations
```c#
// Map unit enum values to unit abbreviations
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
UnitAbbreviationsCache.Default.GetDefaultAbbreviation(HowMuchUnit.Some); // "sm"
UnitParser.Default.Parse<HowMuchUnit>("sm"); // HowMuchUnit.Some
var unitAbbreviations = UnitsNetSetup.Default.UnitAbbreviations;
unitAbbreviations.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
unitAbbreviations.GetDefaultAbbreviation(HowMuchUnit.Some); // "sm"
UnitsNetSetup.Default.UnitParser.Parse<HowMuchUnit>("sm"); // HowMuchUnit.Some
```

#### Convert between units of custom quantity
```c#
var unitConverter = UnitConverter.Default;
var unitConverter = UnitsNetSetup.Default.UnitConverter;
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Lots, HowMuchUnit.Some, x => new HowMuch(x.Value * 2, HowMuchUnit.Some));
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Tons, HowMuchUnit.Lots, x => new HowMuch(x.Value * 10, HowMuchUnit.Lots));
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Tons, HowMuchUnit.Some, x => new HowMuch(x.Value * 20, HowMuchUnit.Some));
Expand All @@ -299,23 +301,32 @@ Console.WriteLine(Convert(HowMuchUnit.Tons)); // 10 tns
```

#### Parse custom quantity
[QuantityParser](UnitsNet/CustomCode/QuantityParser.cs) parses quantity strings to `IQuantity` by providing a `UnitAbbreviationsCache` with custom units and unit abbreviations.
[QuantityParser](UnitsNet/CustomCode/QuantityParser.cs) parses quantity strings to `IQuantity` by mapping custom units to unit abbreviations in `UnitAbbreviationsCache`.

```c#
// Alternatively, manipulate the global UnitAbbreviationsCache.Default.
var unitAbbreviationsCache = new UnitAbbreviationsCache();
// Map custom units to abbreviations
var unitAbbreviationsCache = UnitsNetSetup.Default.UnitAbbreviations;
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "tn");

var quantityParser = new QuantityParser(unitAbbreviationsCache);
var quantityParser = UnitsNetSetup.Default.QuantityParser;

// 1 Some
// "1 sm" => new HowMuch(1, HowMuchUnit.Some)
HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>(
str: "1 sm",
formatProvider: null,
fromDelegate: (value, unit) => new HowMuch((double) value, unit));
```

```c#
// Alternatively, create your own instances to not change the global singleton instances.
var unitAbbreviationsCache = UnitAbbreviationsCache.CreateDefault(); // or .CreateEmpty()
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "tn");

var quantityParser = new QuantityParser(unitAbbreviationsCache);
```


### Example: Unit converter app
[Source code](https://github.com/angularsen/UnitsNet/tree/master/Samples/UnitConverter.Wpf) for `Samples/UnitConverter.Wpf`<br/>
Expand Down

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

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

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

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

3 changes: 2 additions & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs

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

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

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

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

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

3 changes: 2 additions & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs

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

3 changes: 2 additions & 1 deletion UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs

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

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

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

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

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

Loading

0 comments on commit 52aba98

Please sign in to comment.