Skip to content

Commit bc5eb72

Browse files
committed
Merge pull request #141 from eriove/feature/nullable-constructors
Add nullable constructor methods
2 parents f45a432 + 421a8eb commit bc5eb72

38 files changed

+7043
-221
lines changed

UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
9393
object unit = Enum.Parse(reflectedUnitEnumType, unitEnumValue);
9494

9595
// Mass.From() method, assume no overloads exist
96-
MethodInfo fromMethod = reflectedUnitType.GetMethod("From");
96+
MethodInfo fromMethod = (from m in reflectedUnitType.GetMethods()
97+
where m.Name.Equals("From", StringComparison.InvariantCulture) &&
98+
!m.ReturnType.IsGenericType // we want the non nullable type
99+
select m).Single();
97100

98101
// Ex: Mass.From(55, MassUnit.Gram)
99102
// TODO: there is a possible loss of precision if base value requires higher precision than double can represent.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright(c) 2007 Andreas Gullberg Larsen
2+
// https://github.com/anjdreas/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
using NUnit.Framework;
24+
using UnitsNet.Units;
25+
26+
namespace UnitsNet.Tests
27+
{
28+
public class NullableConstructors
29+
{
30+
[Test]
31+
public void StaticConstructorWithNullReturnsNullWhenBackingTypeIsDouble()
32+
{
33+
Length? meter = Length.FromMeters(null);
34+
Assert.IsFalse(meter.HasValue);
35+
}
36+
37+
[Test]
38+
public void StaticConstructorWithNullAndEnumReturnsNullWhenBackingTypeIsDouble()
39+
{
40+
Length? meter = Length.From(null, LengthUnit.Meter);
41+
Assert.IsFalse(meter.HasValue);
42+
}
43+
44+
[Test]
45+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDouble()
46+
{
47+
double? value = 1.0;
48+
Length? meter = Length.From(value, LengthUnit.Meter);
49+
Assert.IsTrue(meter.HasValue);
50+
}
51+
52+
[Test]
53+
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDouble()
54+
{
55+
double? value = 1.0;
56+
Length? meter = Length.FromMeters(value);
57+
Assert.IsTrue(meter.HasValue);
58+
}
59+
60+
[Test]
61+
public void StaticConstructorWithNullReturnsNullWhenBackingTypeIsDecimal()
62+
{
63+
Information? meter = Information.FromBytes(null);
64+
Assert.IsFalse(meter.HasValue);
65+
}
66+
67+
[Test]
68+
public void StaticConstructorWithNullAndEnumReturnsNullWhenBackingTypeIsDecimal()
69+
{
70+
Information? meter = Information.From(null, InformationUnit.Byte);
71+
Assert.IsFalse(meter.HasValue);
72+
}
73+
74+
[Test]
75+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDecimal()
76+
{
77+
double? value = 1.0;
78+
Information? meter = Information.From(value, InformationUnit.Byte);
79+
Assert.IsTrue(meter.HasValue);
80+
}
81+
82+
[Test]
83+
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValueWhenBackingTypeIsDecimal()
84+
{
85+
double? value = 1.0;
86+
Information? meter = Information.FromBytes(value);
87+
Assert.IsTrue(meter.HasValue);
88+
}
89+
}
90+
}

UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,112 @@ public static Acceleration FromNanometerPerSecondSquared(double nanometerperseco
176176
}
177177

178178

179+
/// <summary>
180+
/// Get nullable Acceleration from nullable CentimeterPerSecondSquared.
181+
/// </summary>
182+
public static Acceleration? FromCentimeterPerSecondSquared(double? centimeterpersecondsquared)
183+
{
184+
if (centimeterpersecondsquared.HasValue)
185+
{
186+
return FromCentimeterPerSecondSquared(centimeterpersecondsquared.Value);
187+
}
188+
else
189+
{
190+
return null;
191+
}
192+
}
193+
194+
/// <summary>
195+
/// Get nullable Acceleration from nullable DecimeterPerSecondSquared.
196+
/// </summary>
197+
public static Acceleration? FromDecimeterPerSecondSquared(double? decimeterpersecondsquared)
198+
{
199+
if (decimeterpersecondsquared.HasValue)
200+
{
201+
return FromDecimeterPerSecondSquared(decimeterpersecondsquared.Value);
202+
}
203+
else
204+
{
205+
return null;
206+
}
207+
}
208+
209+
/// <summary>
210+
/// Get nullable Acceleration from nullable KilometerPerSecondSquared.
211+
/// </summary>
212+
public static Acceleration? FromKilometerPerSecondSquared(double? kilometerpersecondsquared)
213+
{
214+
if (kilometerpersecondsquared.HasValue)
215+
{
216+
return FromKilometerPerSecondSquared(kilometerpersecondsquared.Value);
217+
}
218+
else
219+
{
220+
return null;
221+
}
222+
}
223+
224+
/// <summary>
225+
/// Get nullable Acceleration from nullable MeterPerSecondSquared.
226+
/// </summary>
227+
public static Acceleration? FromMeterPerSecondSquared(double? meterpersecondsquared)
228+
{
229+
if (meterpersecondsquared.HasValue)
230+
{
231+
return FromMeterPerSecondSquared(meterpersecondsquared.Value);
232+
}
233+
else
234+
{
235+
return null;
236+
}
237+
}
238+
239+
/// <summary>
240+
/// Get nullable Acceleration from nullable MicrometerPerSecondSquared.
241+
/// </summary>
242+
public static Acceleration? FromMicrometerPerSecondSquared(double? micrometerpersecondsquared)
243+
{
244+
if (micrometerpersecondsquared.HasValue)
245+
{
246+
return FromMicrometerPerSecondSquared(micrometerpersecondsquared.Value);
247+
}
248+
else
249+
{
250+
return null;
251+
}
252+
}
253+
254+
/// <summary>
255+
/// Get nullable Acceleration from nullable MillimeterPerSecondSquared.
256+
/// </summary>
257+
public static Acceleration? FromMillimeterPerSecondSquared(double? millimeterpersecondsquared)
258+
{
259+
if (millimeterpersecondsquared.HasValue)
260+
{
261+
return FromMillimeterPerSecondSquared(millimeterpersecondsquared.Value);
262+
}
263+
else
264+
{
265+
return null;
266+
}
267+
}
268+
269+
/// <summary>
270+
/// Get nullable Acceleration from nullable NanometerPerSecondSquared.
271+
/// </summary>
272+
public static Acceleration? FromNanometerPerSecondSquared(double? nanometerpersecondsquared)
273+
{
274+
if (nanometerpersecondsquared.HasValue)
275+
{
276+
return FromNanometerPerSecondSquared(nanometerpersecondsquared.Value);
277+
}
278+
else
279+
{
280+
return null;
281+
}
282+
}
283+
284+
179285
/// <summary>
180286
/// Dynamically convert from value and unit enum <see cref="AccelerationUnit" /> to <see cref="Acceleration" />.
181287
/// </summary>
@@ -206,6 +312,40 @@ public static Acceleration From(double value, AccelerationUnit fromUnit)
206312
}
207313
}
208314

315+
/// <summary>
316+
/// Dynamically convert from value and unit enum <see cref="AccelerationUnit" /> to <see cref="Acceleration" />.
317+
/// </summary>
318+
/// <param name="value">Value to convert from.</param>
319+
/// <param name="fromUnit">Unit to convert from.</param>
320+
/// <returns>Acceleration unit value.</returns>
321+
public static Acceleration? From(double? value, AccelerationUnit fromUnit)
322+
{
323+
if (!value.HasValue)
324+
{
325+
return null;
326+
}
327+
switch (fromUnit)
328+
{
329+
case AccelerationUnit.CentimeterPerSecondSquared:
330+
return FromCentimeterPerSecondSquared(value.Value);
331+
case AccelerationUnit.DecimeterPerSecondSquared:
332+
return FromDecimeterPerSecondSquared(value.Value);
333+
case AccelerationUnit.KilometerPerSecondSquared:
334+
return FromKilometerPerSecondSquared(value.Value);
335+
case AccelerationUnit.MeterPerSecondSquared:
336+
return FromMeterPerSecondSquared(value.Value);
337+
case AccelerationUnit.MicrometerPerSecondSquared:
338+
return FromMicrometerPerSecondSquared(value.Value);
339+
case AccelerationUnit.MillimeterPerSecondSquared:
340+
return FromMillimeterPerSecondSquared(value.Value);
341+
case AccelerationUnit.NanometerPerSecondSquared:
342+
return FromNanometerPerSecondSquared(value.Value);
343+
344+
default:
345+
throw new NotImplementedException("fromUnit: " + fromUnit);
346+
}
347+
}
348+
209349
/// <summary>
210350
/// Get unit abbreviation string.
211351
/// </summary>
@@ -371,14 +511,14 @@ public double As(AccelerationUnit unit)
371511
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
372512
/// </exception>
373513
/// <exception cref="AmbiguousUnitParseException">
374-
/// More than one unit is represented by the specified unit abbreviation.
375-
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
376-
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
514+
/// More than one unit is represented by the specified unit abbreviation.
515+
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
516+
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
377517
/// </exception>
378518
/// <exception cref="UnitsNetException">
379-
/// If anything else goes wrong, typically due to a bug or unhandled case.
380-
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
381-
/// Units.NET exceptions from other exceptions.
519+
/// If anything else goes wrong, typically due to a bug or unhandled case.
520+
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
521+
/// Units.NET exceptions from other exceptions.
382522
/// </exception>
383523
public static Acceleration Parse(string str, IFormatProvider formatProvider = null)
384524
{

UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,52 @@ public static AmplitudeRatio FromDecibelVolts(double decibelvolts)
112112
}
113113

114114

115+
/// <summary>
116+
/// Get nullable AmplitudeRatio from nullable DecibelMicrovolts.
117+
/// </summary>
118+
public static AmplitudeRatio? FromDecibelMicrovolts(double? decibelmicrovolts)
119+
{
120+
if (decibelmicrovolts.HasValue)
121+
{
122+
return FromDecibelMicrovolts(decibelmicrovolts.Value);
123+
}
124+
else
125+
{
126+
return null;
127+
}
128+
}
129+
130+
/// <summary>
131+
/// Get nullable AmplitudeRatio from nullable DecibelMillivolts.
132+
/// </summary>
133+
public static AmplitudeRatio? FromDecibelMillivolts(double? decibelmillivolts)
134+
{
135+
if (decibelmillivolts.HasValue)
136+
{
137+
return FromDecibelMillivolts(decibelmillivolts.Value);
138+
}
139+
else
140+
{
141+
return null;
142+
}
143+
}
144+
145+
/// <summary>
146+
/// Get nullable AmplitudeRatio from nullable DecibelVolts.
147+
/// </summary>
148+
public static AmplitudeRatio? FromDecibelVolts(double? decibelvolts)
149+
{
150+
if (decibelvolts.HasValue)
151+
{
152+
return FromDecibelVolts(decibelvolts.Value);
153+
}
154+
else
155+
{
156+
return null;
157+
}
158+
}
159+
160+
115161
/// <summary>
116162
/// Dynamically convert from value and unit enum <see cref="AmplitudeRatioUnit" /> to <see cref="AmplitudeRatio" />.
117163
/// </summary>
@@ -134,6 +180,32 @@ public static AmplitudeRatio From(double value, AmplitudeRatioUnit fromUnit)
134180
}
135181
}
136182

183+
/// <summary>
184+
/// Dynamically convert from value and unit enum <see cref="AmplitudeRatioUnit" /> to <see cref="AmplitudeRatio" />.
185+
/// </summary>
186+
/// <param name="value">Value to convert from.</param>
187+
/// <param name="fromUnit">Unit to convert from.</param>
188+
/// <returns>AmplitudeRatio unit value.</returns>
189+
public static AmplitudeRatio? From(double? value, AmplitudeRatioUnit fromUnit)
190+
{
191+
if (!value.HasValue)
192+
{
193+
return null;
194+
}
195+
switch (fromUnit)
196+
{
197+
case AmplitudeRatioUnit.DecibelMicrovolt:
198+
return FromDecibelMicrovolts(value.Value);
199+
case AmplitudeRatioUnit.DecibelMillivolt:
200+
return FromDecibelMillivolts(value.Value);
201+
case AmplitudeRatioUnit.DecibelVolt:
202+
return FromDecibelVolts(value.Value);
203+
204+
default:
205+
throw new NotImplementedException("fromUnit: " + fromUnit);
206+
}
207+
}
208+
137209
/// <summary>
138210
/// Get unit abbreviation string.
139211
/// </summary>
@@ -299,14 +371,14 @@ public double As(AmplitudeRatioUnit unit)
299371
/// "&lt;quantity&gt; &lt;unit&gt;". Eg. "5.5 m" or "1ft 2in"
300372
/// </exception>
301373
/// <exception cref="AmbiguousUnitParseException">
302-
/// More than one unit is represented by the specified unit abbreviation.
303-
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
304-
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
374+
/// More than one unit is represented by the specified unit abbreviation.
375+
/// Example: Volume.Parse("1 cup") will throw, because it can refer to any of
376+
/// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />.
305377
/// </exception>
306378
/// <exception cref="UnitsNetException">
307-
/// If anything else goes wrong, typically due to a bug or unhandled case.
308-
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
309-
/// Units.NET exceptions from other exceptions.
379+
/// If anything else goes wrong, typically due to a bug or unhandled case.
380+
/// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish
381+
/// Units.NET exceptions from other exceptions.
310382
/// </exception>
311383
public static AmplitudeRatio Parse(string str, IFormatProvider formatProvider = null)
312384
{

0 commit comments

Comments
 (0)