Skip to content

Commit 0aab57b

Browse files
authored
Merge pull request #496 from angularsen/v4-remove-unitsystem-static
Remove UnitSystem static methods
2 parents cece5fb + 0e3dba3 commit 0aab57b

File tree

1 file changed

+79
-36
lines changed

1 file changed

+79
-36
lines changed

UnitsNet/CustomCode/UnitSystem.cs

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,26 @@
3131
// ReSharper disable once CheckNamespace
3232
namespace UnitsNet
3333
{
34+
/// <summary>
35+
/// Main facade for working with units dynamically and configuring default behavior for quantities' ToString():
36+
/// - Instance per culture via <see cref="GetCached(System.IFormatProvider)"/>, for caching number formatting and unit abbreviation localization
37+
/// - Use <see cref="Default"/> for local system's current culture
38+
/// - Override <see cref="DefaultCulture"/> to affect number formatting and localization of all quantities' ToString() when culture is not specified
39+
/// - <see cref="Parse"/> unit abbreviations with dynamic types
40+
/// - Dynamically add abbreviations to unit enums with <see cref="MapUnitToAbbreviation"/>
41+
/// </summary>
3442
[PublicAPI]
3543
public sealed partial class UnitSystem
3644
{
3745
private static readonly Dictionary<IFormatProvider, UnitSystem> CultureToInstance;
3846

3947
/// <summary>
4048
/// Fallback culture used by <see cref="GetAllAbbreviations{TUnitType}" /> and
41-
/// <see cref="GetDefaultAbbreviation{TUnitType}(TUnitType,CultureInfo)" />
49+
/// <see cref="GetDefaultAbbreviation{TUnitType}" />
4250
/// if no abbreviations are found with current <see cref="Culture" />.
4351
/// </summary>
4452
/// <example>
45-
/// User wants to call <see cref="Parse{TUnitType}(string,System.IFormatProvider)" /> or <see cref="object.ToString" /> with Russian
53+
/// User wants to call <see cref="Parse{TUnitType}" /> or <see cref="Length.ToString()" /> with Russian
4654
/// culture, but no translation is defined, so we return the US English definition as a last resort. If it's not
4755
/// defined there either, an exception is thrown.
4856
/// </example>
@@ -147,8 +155,15 @@ public UnitSystem() : this(DefaultCulture)
147155
#endif
148156
static IFormatProvider DefaultCulture { get; set; } = CultureInfo.CurrentUICulture;
149157

150-
public bool IsFallbackCulture => Culture.Equals(FallbackCulture);
158+
/// <summary>
159+
/// Whether this instance is for the <see cref="FallbackCulture"/>.
160+
/// </summary>
161+
private bool IsFallbackCulture => Culture.Equals(FallbackCulture);
151162

163+
/// <summary>
164+
/// Clear the cached singleton instances.
165+
/// Calling <see cref="Default"/> or <see cref="GetCached(string)"/> afterwards will create a new instance.
166+
/// </summary>
152167
[PublicAPI]
153168
public static void ClearCache()
154169
{
@@ -185,6 +200,11 @@ public static UnitSystem GetCached([CanBeNull] string cultureName)
185200
}
186201

187202
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
203+
/// <summary>
204+
/// Gets or creates the singleton instance configured with localized unit abbreviations and number formatting for the given culture.
205+
/// </summary>
206+
/// <param name="cultureInfo">The culture.</param>
207+
/// <returns>The instance.</returns>
188208
#if WINDOWS_UWP
189209
internal
190210
#else
@@ -205,19 +225,13 @@ static UnitSystem GetCached([CanBeNull] IFormatProvider cultureInfo)
205225
}
206226
}
207227

208-
[PublicAPI]
209-
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
210-
#if WINDOWS_UWP
211-
internal
212-
#else
213-
public
214-
#endif
215-
static TUnitType Parse<TUnitType>(string unitAbbreviation, IFormatProvider culture)
216-
where TUnitType : /*Enum constraint hack*/ struct, IComparable, IFormattable
217-
{
218-
return GetCached(culture).Parse<TUnitType>(unitAbbreviation);
219-
}
220-
228+
/// <summary>
229+
/// Parses a unit abbreviation for a given unit enumeration type.
230+
/// Example: Parse&lt;LengthUnit&gt;("km") => LengthUnit.Kilometer
231+
/// </summary>
232+
/// <param name="unitAbbreviation"></param>
233+
/// <typeparam name="TUnitType"></typeparam>
234+
/// <returns></returns>
221235
[PublicAPI]
222236
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
223237
#if WINDOWS_UWP
@@ -246,12 +260,10 @@ TUnitType Parse<TUnitType>(string unitAbbreviation)
246260
[PublicAPI]
247261
public object Parse(string unitAbbreviation, Type unitType)
248262
{
249-
AbbreviationMap abbrevToUnitValue;
250-
if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue))
263+
if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out var abbrevToUnitValue))
251264
throw new UnitNotFoundException($"No abbreviations defined for unit type [{unitType}] for culture [{Culture}].");
252265

253-
List<int> unitIntValues;
254-
List<object> unitValues = abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitIntValues)
266+
List<object> unitValues = abbrevToUnitValue.TryGetValue(unitAbbreviation, out var unitIntValues)
255267
? unitIntValues.Distinct().Cast<object>().ToList()
256268
: new List<object>();
257269

@@ -268,19 +280,13 @@ public object Parse(string unitAbbreviation, Type unitType)
268280
}
269281
}
270282

271-
[PublicAPI]
272-
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
273-
#if WINDOWS_UWP
274-
internal
275-
#else
276-
public
277-
#endif
278-
static string GetDefaultAbbreviation<TUnitType>(TUnitType unit, CultureInfo culture)
279-
where TUnitType : /*Enum constraint hack*/ struct, IComparable, IFormattable
280-
{
281-
return GetCached(culture).GetDefaultAbbreviation(unit);
282-
}
283-
283+
/// <summary>
284+
/// Gets the default abbreviation for a given unit. If a unit has more than one abbreviation defined, then it returns the first one.
285+
/// Example: GetDefaultAbbreviation&lt;LengthUnit&gt;(LengthUnit.Kilometer) => "km"
286+
/// </summary>
287+
/// <param name="unit">The unit enum value.</param>
288+
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
289+
/// <returns>The default unit abbreviation string.</returns>
284290
[PublicAPI]
285291
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
286292
#if WINDOWS_UWP
@@ -294,12 +300,28 @@ string GetDefaultAbbreviation<TUnitType>(TUnitType unit)
294300
return GetAllAbbreviations(unit).First();
295301
}
296302

303+
/// <summary>
304+
/// Gets the default abbreviation for a given unit type and its numeric enum value.
305+
/// If a unit has more than one abbreviation defined, then it returns the first one.
306+
/// Example: GetDefaultAbbreviation&lt;LengthUnit&gt;(typeof(LengthUnit), 1) => "cm"
307+
/// </summary>
308+
/// <param name="unitType">The unit enum type.</param>
309+
/// <param name="unitValue">The unit enum value.</param>
310+
/// <returns>The default unit abbreviation string.</returns>
297311
[PublicAPI]
298312
public string GetDefaultAbbreviation(Type unitType, int unitValue)
299313
{
300314
return GetAllAbbreviations(unitType, unitValue).First();
301315
}
302316

317+
/// <summary>
318+
/// Adds one or more unit abbreviation for the given unit enum value.
319+
/// This is used to dynamically add abbreviations for existing unit enums such as <see cref="LengthUnit"/> or to extend with third-party unit enums
320+
/// in order to <see cref="Parse{TUnitType}"/> or <see cref="GetDefaultAbbreviation{TUnitType}"/> on them later.
321+
/// </summary>
322+
/// <param name="unit">The unit enum value.</param>
323+
/// <param name="abbreviations">Unit abbreviations to add.</param>
324+
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
303325
[PublicAPI]
304326
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
305327
#if WINDOWS_UWP
@@ -318,6 +340,14 @@ void MapUnitToAbbreviation<TUnitType>(TUnitType unit, params string[] abbreviati
318340
MapUnitToAbbreviation(unitType, unitValue, abbreviations);
319341
}
320342

343+
/// <summary>
344+
/// Adds one or more unit abbreviation for the given unit enum value.
345+
/// This is used to dynamically add abbreviations for existing unit enums such as <see cref="LengthUnit"/> or to extend with third-party unit enums
346+
/// in order to <see cref="Parse{TUnitType}"/> or <see cref="GetDefaultAbbreviation{TUnitType}"/> on them later.
347+
/// </summary>
348+
/// <param name="unitType">The unit enum type.</param>
349+
/// <param name="unitValue">The unit enum value.</param>
350+
/// <param name="abbreviations">Unit abbreviations to add.</param>
321351
[PublicAPI]
322352
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
323353
#if WINDOWS_UWP
@@ -364,6 +394,13 @@ void MapUnitToAbbreviation(Type unitType, int unitValue, [NotNull] params string
364394
}
365395
}
366396

397+
/// <summary>
398+
/// Try to parse a unit abbreviation.
399+
/// </summary>
400+
/// <param name="unitAbbreviation">The string value.</param>
401+
/// <param name="unit">The unit enum value as out result.</param>
402+
/// <typeparam name="TUnitType">Type of unit enum.</typeparam>
403+
/// <returns>True if successful.</returns>
367404
[PublicAPI]
368405
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
369406
#if WINDOWS_UWP
@@ -386,6 +423,13 @@ bool TryParse<TUnitType>(string unitAbbreviation, out TUnitType unit)
386423
}
387424
}
388425

426+
/// <summary>
427+
/// Try to parse a unit abbreviation.
428+
/// </summary>
429+
/// <param name="unitAbbreviation">The string value.</param>
430+
/// <param name="unitType">Type of unit enum.</param>
431+
/// <param name="unit">The unit enum value as out result.</param>
432+
/// <returns>True if successful.</returns>
389433
[PublicAPI]
390434
public bool TryParse(string unitAbbreviation, Type unitType, out object unit)
391435
{
@@ -484,12 +528,11 @@ private void LoadDefaultAbbreviations([NotNull] IFormatProvider culture)
484528
foreach (CulturesForEnumValue ev in localization.EnumValues)
485529
{
486530
int unitEnumValue = ev.Value;
487-
var usCulture = new CultureInfo("en-US");
488531

489532
// Fall back to US English if localization not found
490-
AbbreviationsForCulture matchingCulture =
533+
var matchingCulture =
491534
ev.Cultures.FirstOrDefault(a => a.Cult.Equals(culture)) ??
492-
ev.Cultures.FirstOrDefault(a => a.Cult.Equals(usCulture));
535+
ev.Cultures.FirstOrDefault(a => a.Cult.Equals(FallbackCulture));
493536

494537
if (matchingCulture == null)
495538
continue;

0 commit comments

Comments
 (0)