Skip to content

Commit 5dc3dd5

Browse files
committed
Remove QuantityValue
1 parent aeb0cd7 commit 5dc3dd5

File tree

16 files changed

+36
-424
lines changed

16 files changed

+36
-424
lines changed

CodeGen/Generators/UnitsNetGen/NumberExtensionsGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static class NumberTo{_quantityName}Extensions
4545
continue;
4646

4747
Writer.WL(2, $@"
48-
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(UnitsNet.QuantityValue)"" />");
48+
/// <inheritdoc cref=""{_quantityName}.From{unit.PluralName}(double)"" />");
4949

5050
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
5151

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void GenerateProperties()
299299
public {_valueType} Value => _value;
300300
301301
/// <inheritdoc />
302-
QuantityValue IQuantity.Value => _value;
302+
double IQuantity.Value => _value;
303303
304304
Enum IQuantity.Unit => Unit;
305305
@@ -429,7 +429,7 @@ private void GenerateStaticFactoryMethods()
429429
/// <exception cref=""ArgumentException"">If value is NaN or Infinity.</exception>");
430430
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit));
431431
Writer.WL($@"
432-
public static {_quantity.Name} From{unit.PluralName}(QuantityValue {valueParamName})
432+
public static {_quantity.Name} From{unit.PluralName}(double {valueParamName})
433433
{{
434434
{_valueType} value = ({_valueType}) {valueParamName};
435435
return new {_quantity.Name}(value, {_unitEnumName}.{unit.SingularName});
@@ -444,7 +444,7 @@ private void GenerateStaticFactoryMethods()
444444
/// <param name=""value"">Value to convert from.</param>
445445
/// <param name=""fromUnit"">Unit to convert from.</param>
446446
/// <returns>{_quantity.Name} unit value.</returns>
447-
public static {_quantity.Name} From(QuantityValue value, {_unitEnumName} fromUnit)
447+
public static {_quantity.Name} From(double value, {_unitEnumName} fromUnit)
448448
{{
449449
return new {_quantity.Name}(({_valueType})value, fromUnit);
450450
}}

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public partial class Quantity
4949
/// <param name=""quantityInfo"">The <see cref=""QuantityInfo""/> of the quantity to create.</param>
5050
/// <param name=""value"">The value to construct the quantity with.</param>
5151
/// <returns>The created quantity.</returns>
52-
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValue value)
52+
public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, double value)
5353
{
5454
return quantityInfo.Name switch
5555
{");
@@ -72,7 +72,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu
7272
/// <param name=""unit"">Unit enum value.</param>
7373
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
7474
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
75-
public static bool TryFrom(QuantityValue value, Enum? unit, [NotNullWhen(true)] out IQuantity? quantity)
75+
public static bool TryFrom(double value, Enum? unit, [NotNullWhen(true)] out IQuantity? quantity)
7676
{
7777
quantity = unit switch
7878
{");

UnitsNet.Serialization.JsonNet.Tests/AbbreviatedUnitsConverterTests.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,6 @@ class PlainOldDoubleQuantity
342342
public string Unit { get; set; }
343343
}
344344

345-
[Fact]
346-
public void LargeDecimalQuantity_DeserializedTo_PlainOldDoubleQuantity()
347-
{
348-
const string json = """{"Value":18446744073709551614,"Unit":"EB","Type":"Information"}""";
349-
var plainOldQuantity = JsonConvert.DeserializeObject<PlainOldDoubleQuantity>(json);
350-
351-
Assert.Equal(18446744073709551614d, plainOldQuantity.Value);
352-
Assert.Equal("EB", plainOldQuantity.Unit);
353-
}
354-
355345
#endregion
356346

357347
}

UnitsNet.Serialization.JsonNet/AbbreviatedUnitsConverter.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,11 @@ protected string GetQuantityType(IQuantity quantity)
172172
unit = GetUnitOrDefault(unitAbbreviation, quantityInfo);
173173
}
174174

175-
QuantityValue value;
175+
double value;
176176
if (valueToken is null)
177177
{
178178
value = default;
179179
}
180-
else if (quantityInfo.Zero is IValueQuantity<decimal>)
181-
{
182-
value = decimal.Parse(valueToken, CultureInfo.InvariantCulture);
183-
}
184180
else
185181
{
186182
value = double.Parse(valueToken, CultureInfo.InvariantCulture);

UnitsNet.Serialization.JsonNet/UnitsNetBaseJsonConverter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract class UnitsNetBaseJsonConverter<T> : JsonConverter<T>
2121

2222
/// <summary>
2323
/// Register custom types so that the converter can instantiate these quantities.
24-
/// Instead of calling <see cref="Quantity.From(UnitsNet.QuantityValue,System.Enum)"/>, the <see cref="Activator"/> will be used to instantiate the object.
24+
/// Instead of calling <see cref="Quantity.From(double,System.Enum)"/>, the <see cref="Activator"/> will be used to instantiate the object.
2525
/// It is therefore assumed that the constructor of <paramref name="quantity"/> is specified with <c>new T(double value, typeof(<paramref name="unit"/>) unit)</c>.
2626
/// Registering the same <paramref name="unit"/> multiple times, it will overwrite the one registered.
2727
/// </summary>
@@ -113,11 +113,7 @@ protected IQuantity ConvertValueUnit(ValueUnit valueUnit)
113113
return (IQuantity)Activator.CreateInstance(registeredQuantity, valueUnit.Value, unit);
114114
}
115115

116-
return valueUnit switch
117-
{
118-
ExtendedValueUnit {ValueType: "decimal", ValueString: {}} extendedValueUnit => Quantity.From(decimal.Parse(extendedValueUnit.ValueString, CultureInfo.InvariantCulture), unit),
119-
_ => Quantity.From(valueUnit.Value, unit)
120-
};
116+
return Quantity.From(valueUnit.Value, unit);
121117
}
122118

123119
private (Type? Quantity, Type? Unit) GetRegisteredType(string unit)

UnitsNet.Tests/CustomQuantities/HowMuch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public HowMuch(double value, HowMuchUnit unit)
1919
Enum IQuantity.Unit => Unit;
2020
public HowMuchUnit Unit { get; }
2121

22-
public QuantityValue Value { get; }
22+
public double Value { get; }
2323

2424
#region IQuantity
2525

UnitsNet.Tests/DummyIQuantity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class DummyIQuantity : IQuantity
1313

1414
public Enum Unit => throw new NotImplementedException();
1515

16-
public QuantityValue Value => throw new NotImplementedException();
16+
public double Value => throw new NotImplementedException();
1717

1818
public double As(Enum unit ) => throw new NotImplementedException();
1919

UnitsNet/CustomCode/Quantity.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static bool TryGetUnitInfo(Enum unitEnum, [NotNullWhen(true)] out UnitInf
4646
/// <param name="unit">Unit enum value.</param>
4747
/// <returns>An <see cref="IQuantity"/> object.</returns>
4848
/// <exception cref="UnitNotFoundException">Unit value is not a known unit enum type.</exception>
49-
public static IQuantity From(QuantityValue value, Enum unit)
49+
public static IQuantity From(double value, Enum unit)
5050
{
5151
return TryFrom(value, unit, out IQuantity? quantity)
5252
? quantity
@@ -61,7 +61,7 @@ public static IQuantity From(QuantityValue value, Enum unit)
6161
/// <param name="unitName">The invariant unit enum name, such as "Meter". Does not support localization.</param>
6262
/// <returns>An <see cref="IQuantity"/> object.</returns>
6363
/// <exception cref="ArgumentException">Unit value is not a known unit enum type.</exception>
64-
public static IQuantity From(QuantityValue value, string quantityName, string unitName)
64+
public static IQuantity From(double value, string quantityName, string unitName)
6565
{
6666
// Get enum value for this unit, f.ex. LengthUnit.Meter for unit name "Meter".
6767
return UnitConverter.TryParseUnit(quantityName, unitName, out Enum? unitValue) &&
@@ -78,14 +78,14 @@ public static IQuantity From(QuantityValue value, string quantityName, string un
7878
/// Unit abbreviation matching is case-insensitive.<br/>
7979
/// <br/>
8080
/// This will fail if more than one unit across all quantities share the same unit abbreviation.<br/>
81-
/// Prefer <see cref="From(UnitsNet.QuantityValue,System.Enum)"/> or <see cref="From(UnitsNet.QuantityValue,string,string)"/> instead.
81+
/// Prefer <see cref="From(double,System.Enum)"/> or <see cref="From(double,string,string)"/> instead.
8282
/// </remarks>
8383
/// <param name="value">Numeric value.</param>
8484
/// <param name="unitAbbreviation">Unit abbreviation, such as "kg" for <see cref="MassUnit.Kilogram"/>.</param>
8585
/// <returns>An <see cref="IQuantity"/> object.</returns>
8686
/// <exception cref="UnitNotFoundException">Unit abbreviation is not known.</exception>
8787
/// <exception cref="AmbiguousUnitParseException">Multiple units found matching the given unit abbreviation.</exception>
88-
public static IQuantity FromUnitAbbreviation(QuantityValue value, string unitAbbreviation) => FromUnitAbbreviation(null, value, unitAbbreviation);
88+
public static IQuantity FromUnitAbbreviation(double value, string unitAbbreviation) => FromUnitAbbreviation(null, value, unitAbbreviation);
8989

9090
/// <summary>
9191
/// Dynamically construct a quantity from a numeric value and a unit abbreviation.
@@ -95,15 +95,15 @@ public static IQuantity From(QuantityValue value, string quantityName, string un
9595
/// Unit abbreviation matching is case-insensitive.<br/>
9696
/// <br/>
9797
/// This will fail if more than one unit across all quantities share the same unit abbreviation.<br/>
98-
/// Prefer <see cref="From(UnitsNet.QuantityValue,System.Enum)"/> or <see cref="From(UnitsNet.QuantityValue,string,string)"/> instead.
98+
/// Prefer <see cref="From(double,System.Enum)"/> or <see cref="From(double,string,string)"/> instead.
9999
/// </remarks>
100100
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
101101
/// <param name="value">Numeric value.</param>
102102
/// <param name="unitAbbreviation">Unit abbreviation, such as "kg" for <see cref="MassUnit.Kilogram"/>.</param>
103103
/// <returns>An <see cref="IQuantity"/> object.</returns>
104104
/// <exception cref="UnitNotFoundException">Unit abbreviation is not known.</exception>
105105
/// <exception cref="AmbiguousUnitParseException">Multiple units found matching the given unit abbreviation.</exception>
106-
public static IQuantity FromUnitAbbreviation(IFormatProvider? formatProvider, QuantityValue value, string unitAbbreviation)
106+
public static IQuantity FromUnitAbbreviation(IFormatProvider? formatProvider, double value, string unitAbbreviation)
107107
{
108108
// TODO Optimize this with UnitValueAbbreviationLookup via UnitAbbreviationsCache.TryGetUnitValueAbbreviationLookup.
109109
List<Enum> units = GetUnitsForAbbreviation(formatProvider, unitAbbreviation);
@@ -121,16 +121,6 @@ public static IQuantity FromUnitAbbreviation(IFormatProvider? formatProvider, Qu
121121
return From(value, unit);
122122
}
123123

124-
/// <inheritdoc cref="TryFrom(QuantityValue,System.Enum,out UnitsNet.IQuantity)"/>
125-
public static bool TryFrom(double value, Enum unit, [NotNullWhen(true)] out IQuantity? quantity)
126-
{
127-
quantity = default;
128-
129-
// Implicit cast to QuantityValue would prevent TryFrom from being called,
130-
// so we need to explicitly check this here for double arguments.
131-
return TryFrom((QuantityValue)value, unit, out quantity);
132-
}
133-
134124
/// <summary>
135125
/// Try to dynamically construct a quantity from a value, the quantity name and the unit name.
136126
/// </summary>
@@ -155,14 +145,14 @@ public static bool TryFrom(double value, string quantityName, string unitName, [
155145
/// Unit abbreviation matching is case-insensitive.<br/>
156146
/// <br/>
157147
/// This will fail if more than one unit across all quantities share the same unit abbreviation.<br/>
158-
/// Prefer <see cref="From(UnitsNet.QuantityValue,System.Enum)"/> or <see cref="From(UnitsNet.QuantityValue,string,string)"/> instead.
148+
/// Prefer <see cref="From(double,System.Enum)"/> or <see cref="From(double,string,string)"/> instead.
159149
/// </remarks>
160150
/// <param name="value">Numeric value.</param>
161151
/// <param name="unitAbbreviation">Unit abbreviation, such as "kg" for <see cref="MassUnit.Kilogram"/>.</param>
162152
/// <param name="quantity">The quantity if successful, otherwise null.</param>
163153
/// <returns>True if successful.</returns>
164154
/// <exception cref="ArgumentException">Unit value is not a known unit enum type.</exception>
165-
public static bool TryFromUnitAbbreviation(QuantityValue value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity) =>
155+
public static bool TryFromUnitAbbreviation(double value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity) =>
166156
TryFromUnitAbbreviation(null, value, unitAbbreviation, out quantity);
167157

168158
/// <summary>
@@ -173,15 +163,15 @@ public static bool TryFromUnitAbbreviation(QuantityValue value, string unitAbbre
173163
/// Unit abbreviation matching is case-insensitive.<br/>
174164
/// <br/>
175165
/// This will fail if more than one unit across all quantities share the same unit abbreviation.<br/>
176-
/// Prefer <see cref="From(UnitsNet.QuantityValue,System.Enum)"/> or <see cref="From(UnitsNet.QuantityValue,string,string)"/> instead.
166+
/// Prefer <see cref="From(double,System.Enum)"/> or <see cref="From(double,string,string)"/> instead.
177167
/// </remarks>
178168
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
179169
/// <param name="value">Numeric value.</param>
180170
/// <param name="unitAbbreviation">Unit abbreviation, such as "kg" for <see cref="MassUnit.Kilogram"/>.</param>
181171
/// <param name="quantity">The quantity if successful, otherwise null.</param>
182172
/// <returns>True if successful.</returns>
183173
/// <exception cref="ArgumentException">Unit value is not a known unit enum type.</exception>
184-
public static bool TryFromUnitAbbreviation(IFormatProvider? formatProvider, QuantityValue value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity)
174+
public static bool TryFromUnitAbbreviation(IFormatProvider? formatProvider, double value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity)
185175
{
186176
// TODO Optimize this with UnitValueAbbreviationLookup via UnitAbbreviationsCache.TryGetUnitValueAbbreviationLookup.
187177
List<Enum> units = GetUnitsForAbbreviation(formatProvider, unitAbbreviation);

UnitsNet/CustomCode/QuantityParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace UnitsNet
1717
/// </summary>
1818
/// <typeparam name="TQuantity">The type of quantity to create, such as <see cref="Length"/>.</typeparam>
1919
/// <typeparam name="TUnitType">The type of unit enum that belongs to this quantity, such as <see cref="LengthUnit"/> for <see cref="Length"/>.</typeparam>
20-
public delegate TQuantity QuantityFromDelegate<out TQuantity, in TUnitType>(QuantityValue value, TUnitType fromUnit)
20+
public delegate TQuantity QuantityFromDelegate<out TQuantity, in TUnitType>(double value, TUnitType fromUnit)
2121
where TQuantity : IQuantity
2222
where TUnitType : Enum;
2323

0 commit comments

Comments
 (0)