Skip to content

Commit 4665eb7

Browse files
committed
Remove static methods of UnitSystem
Where an instance method also exists. Add some xmldoc while at it too.
1 parent 5e8ac05 commit 4665eb7

File tree

1 file changed

+77
-32
lines changed

1 file changed

+77
-32
lines changed

UnitsNet/CustomCode/UnitSystem.cs

Lines changed: 77 additions & 32 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,16 @@ public UnitSystem() : this(DefaultCulture)
147155
#endif
148156
static IFormatProvider DefaultCulture { get; set; } = CultureInfo.CurrentUICulture;
149157

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

164+
/// <summary>
165+
/// Clear the cached singleton instances.
166+
/// Calling <see cref="Default"/> or <see cref="GetCached(string)"/> afterwards will create a new instance.
167+
/// </summary>
152168
[PublicAPI]
153169
public static void ClearCache()
154170
{
@@ -185,6 +201,11 @@ public static UnitSystem GetCached([CanBeNull] string cultureName)
185201
}
186202

187203
// 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
204+
/// <summary>
205+
/// Gets or creates the singleton instance configured with localized unit abbreviations and number formatting for the given culture.
206+
/// </summary>
207+
/// <param name="cultureInfo">The culture.</param>
208+
/// <returns>The instance.</returns>
188209
#if WINDOWS_UWP
189210
internal
190211
#else
@@ -205,19 +226,13 @@ static UnitSystem GetCached([CanBeNull] IFormatProvider cultureInfo)
205226
}
206227
}
207228

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-
229+
/// <summary>
230+
/// Parses a unit abbreviation for a given unit enumeration type.
231+
/// Example: Parse&lt;LengthUnit&gt;("km") => LengthUnit.Kilometer
232+
/// </summary>
233+
/// <param name="unitAbbreviation"></param>
234+
/// <typeparam name="TUnitType"></typeparam>
235+
/// <returns></returns>
221236
[PublicAPI]
222237
// 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
223238
#if WINDOWS_UWP
@@ -246,12 +261,10 @@ TUnitType Parse<TUnitType>(string unitAbbreviation)
246261
[PublicAPI]
247262
public object Parse(string unitAbbreviation, Type unitType)
248263
{
249-
AbbreviationMap abbrevToUnitValue;
250-
if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out abbrevToUnitValue))
264+
if (!_unitTypeToAbbrevToUnitValue.TryGetValue(unitType, out var abbrevToUnitValue))
251265
throw new UnitNotFoundException($"No abbreviations defined for unit type [{unitType}] for culture [{Culture}].");
252266

253-
List<int> unitIntValues;
254-
List<object> unitValues = abbrevToUnitValue.TryGetValue(unitAbbreviation, out unitIntValues)
267+
List<object> unitValues = abbrevToUnitValue.TryGetValue(unitAbbreviation, out var unitIntValues)
255268
? unitIntValues.Distinct().Cast<object>().ToList()
256269
: new List<object>();
257270

@@ -268,19 +281,13 @@ public object Parse(string unitAbbreviation, Type unitType)
268281
}
269282
}
270283

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-
284+
/// <summary>
285+
/// Gets the default abbreviation for a given unit. If a unit has more than one abbreviation defined, then it returns the first one.
286+
/// Example: GetDefaultAbbreviation&lt;LengthUnit&gt;(LengthUnit.Kilometer) => "km"
287+
/// </summary>
288+
/// <param name="unit">The unit enum value.</param>
289+
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
290+
/// <returns>The default unit abbreviation string.</returns>
284291
[PublicAPI]
285292
// 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
286293
#if WINDOWS_UWP
@@ -294,12 +301,28 @@ string GetDefaultAbbreviation<TUnitType>(TUnitType unit)
294301
return GetAllAbbreviations(unit).First();
295302
}
296303

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

318+
/// <summary>
319+
/// Adds one or more unit abbreviation for the given unit enum value.
320+
/// This is used to dynamically add abbreviations for existing unit enums such as <see cref="LengthUnit"/> or to extend with third-party unit enums
321+
/// in order to <see cref="Parse{TUnitType}"/> or <see cref="GetDefaultAbbreviation{TUnitType}"/> on them later.
322+
/// </summary>
323+
/// <param name="unit">The unit enum value.</param>
324+
/// <param name="abbreviations">Unit abbreviations to add.</param>
325+
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
303326
[PublicAPI]
304327
// 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
305328
#if WINDOWS_UWP
@@ -318,6 +341,14 @@ void MapUnitToAbbreviation<TUnitType>(TUnitType unit, params string[] abbreviati
318341
MapUnitToAbbreviation(unitType, unitValue, abbreviations);
319342
}
320343

344+
/// <summary>
345+
/// Adds one or more unit abbreviation for the given unit enum value.
346+
/// This is used to dynamically add abbreviations for existing unit enums such as <see cref="LengthUnit"/> or to extend with third-party unit enums
347+
/// in order to <see cref="Parse{TUnitType}"/> or <see cref="GetDefaultAbbreviation{TUnitType}"/> on them later.
348+
/// </summary>
349+
/// <param name="unitType">The unit enum type.</param>
350+
/// <param name="unitValue">The unit enum value.</param>
351+
/// <param name="abbreviations">Unit abbreviations to add.</param>
321352
[PublicAPI]
322353
// 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
323354
#if WINDOWS_UWP
@@ -364,6 +395,13 @@ void MapUnitToAbbreviation(Type unitType, int unitValue, [NotNull] params string
364395
}
365396
}
366397

398+
/// <summary>
399+
/// Try to parse a unit abbreviation.
400+
/// </summary>
401+
/// <param name="unitAbbreviation">The string value.</param>
402+
/// <param name="unit">The unit enum value as out result.</param>
403+
/// <typeparam name="TUnitType">Type of unit enum.</typeparam>
404+
/// <returns>True if successful.</returns>
367405
[PublicAPI]
368406
// 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
369407
#if WINDOWS_UWP
@@ -386,6 +424,13 @@ bool TryParse<TUnitType>(string unitAbbreviation, out TUnitType unit)
386424
}
387425
}
388426

427+
/// <summary>
428+
/// Try to parse a unit abbreviation.
429+
/// </summary>
430+
/// <param name="unitAbbreviation">The string value.</param>
431+
/// <param name="unitType">Type of unit enum.</param>
432+
/// <param name="unit">The unit enum value as out result.</param>
433+
/// <returns>True if successful.</returns>
389434
[PublicAPI]
390435
public bool TryParse(string unitAbbreviation, Type unitType, out object unit)
391436
{

0 commit comments

Comments
 (0)