99// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
1010//
1111// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities.
12- // Add Extensions\MyQuantityExtensions.cs to decorate quantities with new behavior.
1312// Add UnitDefinitions\MyQuantity.json and run GeneratUnits.bat to generate new units or quantities.
1413//
1514// </auto-generated>
3736// THE SOFTWARE.
3837
3938using System ;
40- using System . Collections . Generic ;
41- using System . Globalization ;
42- using System . Text . RegularExpressions ;
4339using System . Linq ;
4440using JetBrains . Annotations ;
41+ using UnitsNet . InternalHelpers ;
4542using UnitsNet . Units ;
4643
4744// ReSharper disable once CheckNamespace
@@ -85,17 +82,21 @@ static Acceleration()
8582 /// <summary>
8683 /// Creates the quantity with the given numeric value and unit.
8784 /// </summary>
88- /// <param name="numericValue">Numeric value.</param>
85+ /// <param name="numericValue">The numeric value to contruct this quantity with .</param>
8986 /// <param name="unit">The unit representation to contruct this quantity with.</param>
9087 /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
88+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
9189#if WINDOWS_UWP
9290 private
9391#else
9492 public
9593#endif
9694 Acceleration ( double numericValue , AccelerationUnit unit )
9795 {
98- _value = numericValue ;
96+ if ( unit == AccelerationUnit . Undefined )
97+ throw new ArgumentException ( "The quantity can not be created with an undefined unit." , nameof ( unit ) ) ;
98+
99+ _value = Guard . EnsureValidNumber ( numericValue , nameof ( numericValue ) ) ;
99100 _unit = unit ;
100101 }
101102
@@ -201,6 +202,7 @@ public static BaseDimensions BaseDimensions
201202 /// <summary>
202203 /// Get Acceleration from CentimetersPerSecondSquared.
203204 /// </summary>
205+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
204206#if WINDOWS_UWP
205207 [ Windows . Foundation . Metadata . DefaultOverload ]
206208 public static Acceleration FromCentimetersPerSecondSquared ( double centimeterspersecondsquared )
@@ -215,6 +217,7 @@ public static Acceleration FromCentimetersPerSecondSquared(QuantityValue centime
215217 /// <summary>
216218 /// Get Acceleration from DecimetersPerSecondSquared.
217219 /// </summary>
220+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
218221#if WINDOWS_UWP
219222 [ Windows . Foundation . Metadata . DefaultOverload ]
220223 public static Acceleration FromDecimetersPerSecondSquared ( double decimeterspersecondsquared )
@@ -229,6 +232,7 @@ public static Acceleration FromDecimetersPerSecondSquared(QuantityValue decimete
229232 /// <summary>
230233 /// Get Acceleration from FeetPerSecondSquared.
231234 /// </summary>
235+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
232236#if WINDOWS_UWP
233237 [ Windows . Foundation . Metadata . DefaultOverload ]
234238 public static Acceleration FromFeetPerSecondSquared ( double feetpersecondsquared )
@@ -243,6 +247,7 @@ public static Acceleration FromFeetPerSecondSquared(QuantityValue feetperseconds
243247 /// <summary>
244248 /// Get Acceleration from InchesPerSecondSquared.
245249 /// </summary>
250+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
246251#if WINDOWS_UWP
247252 [ Windows . Foundation . Metadata . DefaultOverload ]
248253 public static Acceleration FromInchesPerSecondSquared ( double inchespersecondsquared )
@@ -257,6 +262,7 @@ public static Acceleration FromInchesPerSecondSquared(QuantityValue inchespersec
257262 /// <summary>
258263 /// Get Acceleration from KilometersPerSecondSquared.
259264 /// </summary>
265+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
260266#if WINDOWS_UWP
261267 [ Windows . Foundation . Metadata . DefaultOverload ]
262268 public static Acceleration FromKilometersPerSecondSquared ( double kilometerspersecondsquared )
@@ -271,6 +277,7 @@ public static Acceleration FromKilometersPerSecondSquared(QuantityValue kilomete
271277 /// <summary>
272278 /// Get Acceleration from KnotsPerHour.
273279 /// </summary>
280+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
274281#if WINDOWS_UWP
275282 [ Windows . Foundation . Metadata . DefaultOverload ]
276283 public static Acceleration FromKnotsPerHour ( double knotsperhour )
@@ -285,6 +292,7 @@ public static Acceleration FromKnotsPerHour(QuantityValue knotsperhour)
285292 /// <summary>
286293 /// Get Acceleration from KnotsPerMinute.
287294 /// </summary>
295+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
288296#if WINDOWS_UWP
289297 [ Windows . Foundation . Metadata . DefaultOverload ]
290298 public static Acceleration FromKnotsPerMinute ( double knotsperminute )
@@ -299,6 +307,7 @@ public static Acceleration FromKnotsPerMinute(QuantityValue knotsperminute)
299307 /// <summary>
300308 /// Get Acceleration from KnotsPerSecond.
301309 /// </summary>
310+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
302311#if WINDOWS_UWP
303312 [ Windows . Foundation . Metadata . DefaultOverload ]
304313 public static Acceleration FromKnotsPerSecond ( double knotspersecond )
@@ -313,6 +322,7 @@ public static Acceleration FromKnotsPerSecond(QuantityValue knotspersecond)
313322 /// <summary>
314323 /// Get Acceleration from MetersPerSecondSquared.
315324 /// </summary>
325+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
316326#if WINDOWS_UWP
317327 [ Windows . Foundation . Metadata . DefaultOverload ]
318328 public static Acceleration FromMetersPerSecondSquared ( double meterspersecondsquared )
@@ -327,6 +337,7 @@ public static Acceleration FromMetersPerSecondSquared(QuantityValue meterspersec
327337 /// <summary>
328338 /// Get Acceleration from MicrometersPerSecondSquared.
329339 /// </summary>
340+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
330341#if WINDOWS_UWP
331342 [ Windows . Foundation . Metadata . DefaultOverload ]
332343 public static Acceleration FromMicrometersPerSecondSquared ( double micrometerspersecondsquared )
@@ -341,6 +352,7 @@ public static Acceleration FromMicrometersPerSecondSquared(QuantityValue microme
341352 /// <summary>
342353 /// Get Acceleration from MillimetersPerSecondSquared.
343354 /// </summary>
355+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
344356#if WINDOWS_UWP
345357 [ Windows . Foundation . Metadata . DefaultOverload ]
346358 public static Acceleration FromMillimetersPerSecondSquared ( double millimeterspersecondsquared )
@@ -355,6 +367,7 @@ public static Acceleration FromMillimetersPerSecondSquared(QuantityValue millime
355367 /// <summary>
356368 /// Get Acceleration from NanometersPerSecondSquared.
357369 /// </summary>
370+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
358371#if WINDOWS_UWP
359372 [ Windows . Foundation . Metadata . DefaultOverload ]
360373 public static Acceleration FromNanometersPerSecondSquared ( double nanometerspersecondsquared )
@@ -369,6 +382,7 @@ public static Acceleration FromNanometersPerSecondSquared(QuantityValue nanomete
369382 /// <summary>
370383 /// Get Acceleration from StandardGravity.
371384 /// </summary>
385+ /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
372386#if WINDOWS_UWP
373387 [ Windows . Foundation . Metadata . DefaultOverload ]
374388 public static Acceleration FromStandardGravity ( double standardgravity )
@@ -601,7 +615,7 @@ private double AsBaseNumericType(AccelerationUnit unit)
601615 /// </exception>
602616 public static Acceleration Parse ( string str )
603617 {
604- return Parse ( str , null ) ;
618+ return ParseInternal ( str , null ) ;
605619 }
606620
607621 /// <summary>
@@ -614,7 +628,7 @@ public static Acceleration Parse(string str)
614628 /// </example>
615629 public static bool TryParse ( [ CanBeNull ] string str , out Acceleration result )
616630 {
617- return TryParse ( str , null , out result ) ;
631+ return TryParseInternal ( str , null , out result ) ;
618632 }
619633
620634 /// <summary>
@@ -628,7 +642,121 @@ public static bool TryParse([CanBeNull] string str, out Acceleration result)
628642 /// <exception cref="UnitsNetException">Error parsing string.</exception>
629643 public static AccelerationUnit ParseUnit ( string str )
630644 {
631- return ParseUnit ( str , ( IFormatProvider ) null ) ;
645+ return ParseUnitInternal ( str , null ) ;
646+ }
647+
648+ public static bool TryParseUnit ( string str , out AccelerationUnit unit )
649+ {
650+ return TryParseUnitInternal ( str , null , out unit ) ;
651+ }
652+
653+ /// <summary>
654+ /// Parse a string with one or two quantities of the format "<quantity> <unit>".
655+ /// </summary>
656+ /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
657+ /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
658+ /// <example>
659+ /// Length.Parse("5.5 m", new CultureInfo("en-US"));
660+ /// </example>
661+ /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
662+ /// <exception cref="ArgumentException">
663+ /// Expected string to have one or two pairs of quantity and unit in the format
664+ /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in"
665+ /// </exception>
666+ /// <exception cref="AmbiguousUnitParseException">
667+ /// More than one unit is represented by the specified unit abbreviation.
668+ /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
669+ /// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
670+ /// </exception>
671+ /// <exception cref="UnitsNetException">
672+ /// If anything else goes wrong, typically due to a bug or unhandled case.
673+ /// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
674+ /// Units.NET exceptions from other exceptions.
675+ /// </exception>
676+ private static Acceleration ParseInternal ( string str , [ CanBeNull ] IFormatProvider provider )
677+ {
678+ if ( str == null ) throw new ArgumentNullException ( nameof ( str ) ) ;
679+
680+ provider = provider ?? GlobalConfiguration . DefaultCulture ;
681+
682+ return QuantityParser . Default . Parse < Acceleration , AccelerationUnit > ( str , provider , ParseUnitInternal , From ,
683+ ( x , y ) => From ( x . MetersPerSecondSquared + y . MetersPerSecondSquared , BaseUnit ) ) ;
684+ }
685+
686+ /// <summary>
687+ /// Try to parse a string with one or two quantities of the format "<quantity> <unit>".
688+ /// </summary>
689+ /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
690+ /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
691+ /// <param name="result">Resulting unit quantity if successful.</param>
692+ /// <returns>True if successful, otherwise false.</returns>
693+ /// <example>
694+ /// Length.Parse("5.5 m", new CultureInfo("en-US"));
695+ /// </example>
696+ private static bool TryParseInternal ( [ CanBeNull ] string str , [ CanBeNull ] IFormatProvider provider , out Acceleration result )
697+ {
698+ result = default ( Acceleration ) ;
699+
700+ if ( string . IsNullOrWhiteSpace ( str ) )
701+ return false ;
702+
703+ provider = provider ?? GlobalConfiguration . DefaultCulture ;
704+
705+ return QuantityParser . Default . TryParse < Acceleration , AccelerationUnit > ( str , provider , TryParseUnitInternal , From ,
706+ ( x , y ) => From ( x . MetersPerSecondSquared + y . MetersPerSecondSquared , BaseUnit ) , out result ) ;
707+ }
708+
709+ /// <summary>
710+ /// Parse a unit string.
711+ /// </summary>
712+ /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
713+ /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
714+ /// <example>
715+ /// Length.ParseUnit("m", new CultureInfo("en-US"));
716+ /// </example>
717+ /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
718+ /// <exception cref="UnitsNetException">Error parsing string.</exception>
719+ private static AccelerationUnit ParseUnitInternal ( string str , IFormatProvider provider = null )
720+ {
721+ if ( str == null ) throw new ArgumentNullException ( nameof ( str ) ) ;
722+
723+ var unit = UnitParser . Default . Parse < AccelerationUnit > ( str . Trim ( ) , provider ) ;
724+
725+ if ( unit == AccelerationUnit . Undefined )
726+ {
727+ var newEx = new UnitsNetException ( "Error parsing string. The unit is not a recognized AccelerationUnit." ) ;
728+ newEx . Data [ "input" ] = str ;
729+ newEx . Data [ "provider" ] = provider ? . ToString ( ) ?? "(null)" ;
730+ throw newEx ;
731+ }
732+
733+ return unit ;
734+ }
735+
736+ /// <summary>
737+ /// Parse a unit string.
738+ /// </summary>
739+ /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
740+ /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" />.</param>
741+ /// <param name="unit">The parsed unit if successful.</param>
742+ /// <returns>True if successful, otherwise false.</returns>
743+ /// <example>
744+ /// Length.ParseUnit("m", new CultureInfo("en-US"));
745+ /// </example>
746+ private static bool TryParseUnitInternal ( string str , IFormatProvider provider , out AccelerationUnit unit )
747+ {
748+ unit = AccelerationUnit . Undefined ;
749+
750+ if ( string . IsNullOrWhiteSpace ( str ) )
751+ return false ;
752+
753+ if ( ! UnitParser . Default . TryParse < AccelerationUnit > ( str . Trim ( ) , provider , out unit ) )
754+ return false ;
755+
756+ if ( unit == AccelerationUnit . Undefined )
757+ return false ;
758+
759+ return true ;
632760 }
633761
634762 #endregion
0 commit comments