diff --git a/Common/UnitDefinitions/VolumePerLength.json b/Common/UnitDefinitions/VolumePerLength.json new file mode 100644 index 0000000000..2098ba6030 --- /dev/null +++ b/Common/UnitDefinitions/VolumePerLength.json @@ -0,0 +1,55 @@ +{ + "Name": "VolumePerLength", + "BaseUnit": "CubicMeterPerMeter", + "XmlDoc": "Volume, typically of fluid, that a container can hold within a unit of length.", + "BaseDimensions": { + "L": 3 + }, + "Units": [ + { + "SingularName": "CubicMeterPerMeter", + "PluralName": "CubicMetersPerMeter", + "BaseUnits": { + "L": "Meter" + }, + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Prefixes": [], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "m³/m" ] + } + ] + }, + { + "SingularName": "LiterPerMeter", + "PluralName": "LitersPerMeter", + "BaseUnits": { + "L": "Decimeter" + }, + "FromUnitToBaseFunc": "x/1000", + "FromBaseToUnitFunc": "x*1000", + "Prefixes": [], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "l/m" ] + } + ] + }, + { + "SingularName": "OilBarrelPerFoot", + "PluralName": "OilBarrelsPerFoot", + "FromUnitToBaseFunc": "x/1.91713408", + "FromBaseToUnitFunc": "x*1.91713408", + "Prefixes": [], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "bbl/ft" ] + } + ] + } + ] +} diff --git a/UnitsNet.Tests/CustomCode/VolumePerLengthTests.cs b/UnitsNet.Tests/CustomCode/VolumePerLengthTests.cs new file mode 100644 index 0000000000..8be2e9cfed --- /dev/null +++ b/UnitsNet.Tests/CustomCode/VolumePerLengthTests.cs @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated (once) by \generate-code.bat, but will not be +// regenerated when it already exists. The purpose of creating this file is to make +// it easier to remember to implement all the unit conversion test cases. +// +// Whenever a new unit is added to this quantity and \generate-code.bat is run, +// the base test class will get a new abstract property and cause a compile error +// in this derived class, reminding the developer to implement the test case +// for the new unit. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; + +namespace UnitsNet.Tests.CustomCode +{ + public class VolumePerLengthTests : VolumePerLengthTestsBase + { + // Override properties in base class here + protected override double CubicMetersPerMeterInOneCubicMeterPerMeter => 1; + + protected override double LitersPerMeterInOneCubicMeterPerMeter => 1000; + + protected override double OilBarrelsPerFootInOneCubicMeterPerMeter => 1.917134088; + } +} diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumePerLength.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumePerLength.WindowsRuntimeComponent.g.cs new file mode 100644 index 0000000000..8e56b9d497 --- /dev/null +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/VolumePerLength.WindowsRuntimeComponent.g.cs @@ -0,0 +1,614 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Globalization; +using System.Linq; +using JetBrains.Annotations; +using UnitsNet.Units; +using UnitsNet.InternalHelpers; + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// Volume, typically of fluid, that a container can hold within a unit of length. + /// + // Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components + // Public structures can't have any members other than public fields, and those fields must be value types or strings. + // Public classes must be sealed (NotInheritable in Visual Basic). If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic. + public sealed partial class VolumePerLength : IQuantity + { + /// + /// The numeric value this quantity was constructed with. + /// + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + private readonly VolumePerLengthUnit? _unit; + + static VolumePerLength() + { + BaseDimensions = new BaseDimensions(3, 0, 0, 0, 0, 0, 0); + Info = new QuantityInfo(QuantityType.VolumePerLength, Units.Cast().ToArray(), BaseUnit, Zero, BaseDimensions); + } + + /// + /// Creates the quantity with a value of 0 in the base unit CubicMeterPerMeter. + /// + /// + /// Windows Runtime Component requires a default constructor. + /// + public VolumePerLength() + { + _value = 0; + _unit = BaseUnit; + } + + /// + /// Creates the quantity with the given numeric value and unit. + /// + /// The numeric value to contruct this quantity with. + /// The unit representation to contruct this quantity with. + /// Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component. + /// If value is NaN or Infinity. + private VolumePerLength(double numericValue, VolumePerLengthUnit unit) + { + if(unit == VolumePerLengthUnit.Undefined) + throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); + + _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue)); + _unit = unit; + } + + #region Static Properties + + /// + /// Information about the quantity type, such as unit values and names. + /// + internal static QuantityInfo Info { get; } + + /// + /// The of this quantity. + /// + public static BaseDimensions BaseDimensions { get; } + + /// + /// The base unit of VolumePerLength, which is CubicMeterPerMeter. All conversions go via this value. + /// + public static VolumePerLengthUnit BaseUnit { get; } = VolumePerLengthUnit.CubicMeterPerMeter; + + /// + /// Represents the largest possible value of VolumePerLength + /// + public static VolumePerLength MaxValue { get; } = new VolumePerLength(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of VolumePerLength + /// + public static VolumePerLength MinValue { get; } = new VolumePerLength(double.MinValue, BaseUnit); + + /// + /// The of this quantity. + /// + public static QuantityType QuantityType { get; } = QuantityType.VolumePerLength; + + /// + /// All units of measurement for the VolumePerLength quantity. + /// + public static VolumePerLengthUnit[] Units { get; } = Enum.GetValues(typeof(VolumePerLengthUnit)).Cast().Except(new VolumePerLengthUnit[]{ VolumePerLengthUnit.Undefined }).ToArray(); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit CubicMeterPerMeter. + /// + public static VolumePerLength Zero { get; } = new VolumePerLength(0, BaseUnit); + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => Convert.ToDouble(_value); + + /// + object IQuantity.Unit => Unit; + + /// + /// The unit this quantity was constructed with -or- if default ctor was used. + /// + public VolumePerLengthUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + internal QuantityInfo QuantityInfo => Info; + + /// + /// The of this quantity. + /// + public QuantityType Type => VolumePerLength.QuantityType; + + /// + /// The of this quantity. + /// + public BaseDimensions Dimensions => VolumePerLength.BaseDimensions; + + #endregion + + #region Conversion Properties + + /// + /// Get VolumePerLength in CubicMetersPerMeter. + /// + public double CubicMetersPerMeter => As(VolumePerLengthUnit.CubicMeterPerMeter); + + /// + /// Get VolumePerLength in LitersPerMeter. + /// + public double LitersPerMeter => As(VolumePerLengthUnit.LiterPerMeter); + + /// + /// Get VolumePerLength in OilBarrelsPerFoot. + /// + public double OilBarrelsPerFoot => As(VolumePerLengthUnit.OilBarrelPerFoot); + + #endregion + + #region Static Methods + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(VolumePerLengthUnit unit) + { + return GetAbbreviation(unit, null); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static string GetAbbreviation(VolumePerLengthUnit unit, [CanBeNull] string cultureName) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Get VolumePerLength from CubicMetersPerMeter. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static VolumePerLength FromCubicMetersPerMeter(double cubicmeterspermeter) + { + double value = (double) cubicmeterspermeter; + return new VolumePerLength(value, VolumePerLengthUnit.CubicMeterPerMeter); + } + /// + /// Get VolumePerLength from LitersPerMeter. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static VolumePerLength FromLitersPerMeter(double literspermeter) + { + double value = (double) literspermeter; + return new VolumePerLength(value, VolumePerLengthUnit.LiterPerMeter); + } + /// + /// Get VolumePerLength from OilBarrelsPerFoot. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static VolumePerLength FromOilBarrelsPerFoot(double oilbarrelsperfoot) + { + double value = (double) oilbarrelsperfoot; + return new VolumePerLength(value, VolumePerLengthUnit.OilBarrelPerFoot); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// VolumePerLength unit value. + // Fix name conflict with parameter "value" + [return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("returnValue")] + public static VolumePerLength From(double value, VolumePerLengthUnit fromUnit) + { + return new VolumePerLength((double)value, fromUnit); + } + + #endregion + + #region Static Parse Methods + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + public static VolumePerLength Parse(string str) + { + return Parse(str, null); + } + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static VolumePerLength Parse(string str, [CanBeNull] string cultureName) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return QuantityParser.Default.Parse( + str, + provider, + From); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + public static bool TryParse([CanBeNull] string str, out VolumePerLength result) + { + return TryParse(str, null, out result); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// True if successful, otherwise false. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out VolumePerLength result) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static VolumePerLengthUnit ParseUnit(string str) + { + return ParseUnit(str, null); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static VolumePerLengthUnit ParseUnit(string str, [CanBeNull] string cultureName) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return UnitParser.Default.Parse(str, provider); + } + + public static bool TryParseUnit(string str, out VolumePerLengthUnit unit) + { + return TryParseUnit(str, null, out unit); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// The parsed unit if successful. + /// True if successful, otherwise false. + /// + /// Length.TryParseUnit("m", new CultureInfo("en-US")); + /// + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out VolumePerLengthUnit unit) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return UnitParser.Default.TryParse(str, provider, out unit); + } + + #endregion + + #region Equality / IComparable + + public int CompareTo(object obj) + { + if(obj is null) throw new ArgumentNullException(nameof(obj)); + if(!(obj is VolumePerLength objVolumePerLength)) throw new ArgumentException("Expected type VolumePerLength.", nameof(obj)); + + return CompareTo(objVolumePerLength); + } + + // 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 + internal int CompareTo(VolumePerLength other) + { + return _value.CompareTo(other.AsBaseNumericType(this.Unit)); + } + + [Windows.Foundation.Metadata.DefaultOverload] + public override bool Equals(object obj) + { + if(obj is null || !(obj is VolumePerLength objVolumePerLength)) + return false; + + return Equals(objVolumePerLength); + } + + public bool Equals(VolumePerLength other) + { + return _value.Equals(other.AsBaseNumericType(this.Unit)); + } + + /// + /// + /// Compare equality to another VolumePerLength within the given absolute or relative tolerance. + /// + /// + /// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a percentage of this quantity's value. will be converted into + /// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of + /// this quantity's value to be considered equal. + /// + /// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Relative); + /// + /// + /// + /// + /// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a fixed number in this quantity's unit. will be converted into + /// this quantity's unit for comparison. + /// + /// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Absolute); + /// + /// + /// + /// + /// Note that it is advised against specifying zero difference, due to the nature + /// of floating point operations and using System.Double internally. + /// + /// + /// The other quantity to compare to. + /// The absolute or relative tolerance value. Must be greater than or equal to 0. + /// The comparison type: either relative or absolute. + /// True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance. + public bool Equals(VolumePerLength other, double tolerance, ComparisonType comparisonType) + { + if(tolerance < 0) + throw new ArgumentOutOfRangeException("tolerance", "Tolerance must be greater than or equal to 0."); + + double thisValue = (double)this.Value; + double otherValueInThisUnits = other.As(this.Unit); + + return UnitsNet.Comparison.Equals(thisValue, otherValueInThisUnits, tolerance, comparisonType); + } + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current VolumePerLength. + public override int GetHashCode() + { + return new { QuantityType, Value, Unit }.GetHashCode(); + } + + #endregion + + #region Conversion Methods + + double IQuantity.As(object unit) => As((VolumePerLengthUnit)unit); + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(VolumePerLengthUnit unit) + { + if(Unit == unit) + return Convert.ToDouble(Value); + + var converted = AsBaseNumericType(unit); + return Convert.ToDouble(converted); + } + + /// + /// Converts this VolumePerLength to another VolumePerLength with the unit representation . + /// + /// A VolumePerLength with the specified unit. + public VolumePerLength ToUnit(VolumePerLengthUnit unit) + { + var convertedValue = AsBaseNumericType(unit); + return new VolumePerLength(convertedValue, unit); + } + + /// + /// Converts the current value + unit to the base unit. + /// This is typically the first step in converting from one unit to another. + /// + /// The value in the base unit representation. + private double AsBaseUnit() + { + switch(Unit) + { + case VolumePerLengthUnit.CubicMeterPerMeter: return _value; + case VolumePerLengthUnit.LiterPerMeter: return _value/1000; + case VolumePerLengthUnit.OilBarrelPerFoot: return _value/1.91713408; + default: + throw new NotImplementedException($"Can not convert {Unit} to base units."); + } + } + + private double AsBaseNumericType(VolumePerLengthUnit unit) + { + if(Unit == unit) + return _value; + + var baseUnitValue = AsBaseUnit(); + + switch(unit) + { + case VolumePerLengthUnit.CubicMeterPerMeter: return baseUnitValue; + case VolumePerLengthUnit.LiterPerMeter: return baseUnitValue*1000; + case VolumePerLengthUnit.OilBarrelPerFoot: return baseUnitValue*1.91713408; + default: + throw new NotImplementedException($"Can not convert {Unit} to {unit}."); + } + } + + #endregion + + #region ToString Methods + + /// + /// Get default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString(null); + } + + /// + /// Get string representation of value and unit. Using two significant digits after radix. + /// + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + public string ToString([CanBeNull] string cultureName) + { + var provider = cultureName; + return ToString(provider, 2); + } + + /// + /// Get string representation of value and unit. + /// + /// The number of significant digits after the radix point. + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + public string ToString(string cultureName, int significantDigitsAfterRadix) + { + var provider = cultureName; + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); + } + + /// + /// Get string representation of value and unit. + /// + /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." + /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + public string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) + { + var provider = GetFormatProviderFromCultureName(cultureName); + if (format == null) throw new ArgumentNullException(nameof(format)); + if (args == null) throw new ArgumentNullException(nameof(args)); + + provider = provider ?? GlobalConfiguration.DefaultCulture; + + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); + return string.Format(provider, format, formatArgs); + } + + #endregion + + private static IFormatProvider GetFormatProviderFromCultureName([CanBeNull] string cultureName) + { + return cultureName != null ? new CultureInfo(cultureName) : (IFormatProvider)null; + } + } +} diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs index cec7b231e3..024322a3ef 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs @@ -311,6 +311,9 @@ internal static bool TryFrom(double value, Enum unit, out IQuantity quantity) case VolumeFlowUnit volumeFlowUnit: quantity = VolumeFlow.From(value, volumeFlowUnit); return true; + case VolumePerLengthUnit volumePerLengthUnit: + quantity = VolumePerLength.From(value, volumePerLengthUnit); + return true; default: { quantity = default(IQuantity); @@ -631,6 +634,9 @@ internal static bool TryParse([CanBeNull] IFormatProvider formatProvider, Type q if (quantityType == typeof(VolumeFlow)) return parser.TryParse(quantityString, formatProvider, VolumeFlow.From, out quantity); + if (quantityType == typeof(VolumePerLength)) + return parser.TryParse(quantityString, formatProvider, VolumePerLength.From, out quantity); + throw new ArgumentException( $"Type {quantityType} is not a known quantity type. Did you pass in a third-party quantity type defined outside UnitsNet library?"); } diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs index e22960d2a9..4cfaa8c6c2 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs @@ -119,5 +119,6 @@ public enum QuantityType VitaminA, Volume, VolumeFlow, + VolumePerLength, } } diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs index 8133129dc3..44bc5e1edf 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs @@ -1136,6 +1136,9 @@ private static readonly (string CultureName, Type UnitType, int UnitValue, strin ("en-US", typeof(VolumeFlowUnit), (int)VolumeFlowUnit.UsGallonPerHour, new string[]{"gal (U.S.)/h"}), ("en-US", typeof(VolumeFlowUnit), (int)VolumeFlowUnit.UsGallonPerMinute, new string[]{"gal (U.S.)/min", "GPM"}), ("en-US", typeof(VolumeFlowUnit), (int)VolumeFlowUnit.UsGallonPerSecond, new string[]{"gal (U.S.)/s"}), + ("en-US", typeof(VolumePerLengthUnit), (int)VolumePerLengthUnit.CubicMeterPerMeter, new string[]{"m³/m"}), + ("en-US", typeof(VolumePerLengthUnit), (int)VolumePerLengthUnit.LiterPerMeter, new string[]{"l/m"}), + ("en-US", typeof(VolumePerLengthUnit), (int)VolumePerLengthUnit.OilBarrelPerFoot, new string[]{"bbl/ft"}), }; } } diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/VolumePerLengthUnit.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/VolumePerLengthUnit.g.cs new file mode 100644 index 0000000000..0f1de7c933 --- /dev/null +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/VolumePerLengthUnit.g.cs @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum VolumePerLengthUnit + { + Undefined = 0, + CubicMeterPerMeter, + LiterPerMeter, + OilBarrelPerFoot, + } + + #pragma warning restore 1591 +} diff --git a/UnitsNet/GeneratedCode/Quantities/VolumePerLength.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/VolumePerLength.NetFramework.g.cs new file mode 100644 index 0000000000..1d845ae209 --- /dev/null +++ b/UnitsNet/GeneratedCode/Quantities/VolumePerLength.NetFramework.g.cs @@ -0,0 +1,860 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Globalization; +using System.Linq; +using JetBrains.Annotations; +using UnitsNet.InternalHelpers; +using UnitsNet.Units; + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// + /// Volume, typically of fluid, that a container can hold within a unit of length. + /// + public partial struct VolumePerLength : IQuantity, IEquatable, IComparable, IComparable, IConvertible, IFormattable + { + /// + /// The numeric value this quantity was constructed with. + /// + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + private readonly VolumePerLengthUnit? _unit; + + static VolumePerLength() + { + BaseDimensions = new BaseDimensions(3, 0, 0, 0, 0, 0, 0); + + Info = new QuantityInfo(QuantityType.VolumePerLength, + new UnitInfo[] { + new UnitInfo(VolumePerLengthUnit.CubicMeterPerMeter, BaseUnits.Undefined), + new UnitInfo(VolumePerLengthUnit.LiterPerMeter, BaseUnits.Undefined), + new UnitInfo(VolumePerLengthUnit.OilBarrelPerFoot, BaseUnits.Undefined), + }, + BaseUnit, Zero, BaseDimensions); + } + + /// + /// Creates the quantity with the given numeric value and unit. + /// + /// The numeric value to contruct this quantity with. + /// The unit representation to contruct this quantity with. + /// If value is NaN or Infinity. + public VolumePerLength(double numericValue, VolumePerLengthUnit unit) + { + if(unit == VolumePerLengthUnit.Undefined) + throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); + + _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue)); + _unit = unit; + } + + /// + /// Creates an instance of the quantity with the given numeric value in units compatible with the given . + /// + /// The numeric value to contruct this quantity with. + /// The unit system to create the quantity with. + /// The given is null. + /// No unit was found for the given . + /// More than one unit was found for the given . + public VolumePerLength(double numericValue, UnitSystem unitSystem) + { + if(unitSystem == null) throw new ArgumentNullException(nameof(unitSystem)); + + _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue)); + _unit = Info.GetUnitInfoFor(unitSystem.BaseUnits).Value; + } + + #region Static Properties + + /// + public static QuantityInfo Info { get; } + + /// + /// The of this quantity. + /// + public static BaseDimensions BaseDimensions { get; } + + /// + /// The base unit of VolumePerLength, which is CubicMeterPerMeter. All conversions go via this value. + /// + public static VolumePerLengthUnit BaseUnit { get; } = VolumePerLengthUnit.CubicMeterPerMeter; + + /// + /// Represents the largest possible value of VolumePerLength + /// + public static VolumePerLength MaxValue { get; } = new VolumePerLength(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of VolumePerLength + /// + public static VolumePerLength MinValue { get; } = new VolumePerLength(double.MinValue, BaseUnit); + + /// + /// The of this quantity. + /// + public static QuantityType QuantityType { get; } = QuantityType.VolumePerLength; + + /// + /// All units of measurement for the VolumePerLength quantity. + /// + public static VolumePerLengthUnit[] Units { get; } = Enum.GetValues(typeof(VolumePerLengthUnit)).Cast().Except(new VolumePerLengthUnit[]{ VolumePerLengthUnit.Undefined }).ToArray(); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit CubicMeterPerMeter. + /// + public static VolumePerLength Zero { get; } = new VolumePerLength(0, BaseUnit); + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + Enum IQuantity.Unit => Unit; + + /// + public VolumePerLengthUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + /// + public QuantityInfo QuantityInfo => Info; + + /// + QuantityInfo IQuantity.QuantityInfo => Info; + + /// + /// The of this quantity. + /// + public QuantityType Type => VolumePerLength.QuantityType; + + /// + /// The of this quantity. + /// + public BaseDimensions Dimensions => VolumePerLength.BaseDimensions; + + #endregion + + #region Conversion Properties + + /// + /// Get VolumePerLength in CubicMetersPerMeter. + /// + public double CubicMetersPerMeter => As(VolumePerLengthUnit.CubicMeterPerMeter); + + /// + /// Get VolumePerLength in LitersPerMeter. + /// + public double LitersPerMeter => As(VolumePerLengthUnit.LiterPerMeter); + + /// + /// Get VolumePerLength in OilBarrelsPerFoot. + /// + public double OilBarrelsPerFoot => As(VolumePerLengthUnit.OilBarrelPerFoot); + + #endregion + + #region Static Methods + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(VolumePerLengthUnit unit) + { + return GetAbbreviation(unit, null); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + /// Format to use for localization. Defaults to if null. + public static string GetAbbreviation(VolumePerLengthUnit unit, [CanBeNull] IFormatProvider provider) + { + return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Get VolumePerLength from CubicMetersPerMeter. + /// + /// If value is NaN or Infinity. + public static VolumePerLength FromCubicMetersPerMeter(QuantityValue cubicmeterspermeter) + { + double value = (double) cubicmeterspermeter; + return new VolumePerLength(value, VolumePerLengthUnit.CubicMeterPerMeter); + } + /// + /// Get VolumePerLength from LitersPerMeter. + /// + /// If value is NaN or Infinity. + public static VolumePerLength FromLitersPerMeter(QuantityValue literspermeter) + { + double value = (double) literspermeter; + return new VolumePerLength(value, VolumePerLengthUnit.LiterPerMeter); + } + /// + /// Get VolumePerLength from OilBarrelsPerFoot. + /// + /// If value is NaN or Infinity. + public static VolumePerLength FromOilBarrelsPerFoot(QuantityValue oilbarrelsperfoot) + { + double value = (double) oilbarrelsperfoot; + return new VolumePerLength(value, VolumePerLengthUnit.OilBarrelPerFoot); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// VolumePerLength unit value. + public static VolumePerLength From(QuantityValue value, VolumePerLengthUnit fromUnit) + { + return new VolumePerLength((double)value, fromUnit); + } + + #endregion + + #region Static Parse Methods + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + public static VolumePerLength Parse(string str) + { + return Parse(str, null); + } + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static VolumePerLength Parse(string str, [CanBeNull] IFormatProvider provider) + { + return QuantityParser.Default.Parse( + str, + provider, + From); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + public static bool TryParse([CanBeNull] string str, out VolumePerLength result) + { + return TryParse(str, null, out result); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// True if successful, otherwise false. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out VolumePerLength result) + { + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static VolumePerLengthUnit ParseUnit(string str) + { + return ParseUnit(str, null); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + /// Format to use when parsing number and unit. Defaults to if null. + public static VolumePerLengthUnit ParseUnit(string str, IFormatProvider provider = null) + { + return UnitParser.Default.Parse(str, provider); + } + + /// + public static bool TryParseUnit(string str, out VolumePerLengthUnit unit) + { + return TryParseUnit(str, null, out unit); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// The parsed unit if successful. + /// True if successful, otherwise false. + /// + /// Length.TryParseUnit("m", new CultureInfo("en-US")); + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static bool TryParseUnit(string str, IFormatProvider provider, out VolumePerLengthUnit unit) + { + return UnitParser.Default.TryParse(str, provider, out unit); + } + + #endregion + + #region Arithmetic Operators + + /// Negate the value. + public static VolumePerLength operator -(VolumePerLength right) + { + return new VolumePerLength(-right.Value, right.Unit); + } + + /// Get from adding two . + public static VolumePerLength operator +(VolumePerLength left, VolumePerLength right) + { + return new VolumePerLength(left.Value + right.GetValueAs(left.Unit), left.Unit); + } + + /// Get from subtracting two . + public static VolumePerLength operator -(VolumePerLength left, VolumePerLength right) + { + return new VolumePerLength(left.Value - right.GetValueAs(left.Unit), left.Unit); + } + + /// Get from multiplying value and . + public static VolumePerLength operator *(double left, VolumePerLength right) + { + return new VolumePerLength(left * right.Value, right.Unit); + } + + /// Get from multiplying value and . + public static VolumePerLength operator *(VolumePerLength left, double right) + { + return new VolumePerLength(left.Value * right, left.Unit); + } + + /// Get from dividing by value. + public static VolumePerLength operator /(VolumePerLength left, double right) + { + return new VolumePerLength(left.Value / right, left.Unit); + } + + /// Get ratio value from dividing by . + public static double operator /(VolumePerLength left, VolumePerLength right) + { + return left.CubicMetersPerMeter / right.CubicMetersPerMeter; + } + + #endregion + + #region Equality / IComparable + + /// Returns true if less or equal to. + public static bool operator <=(VolumePerLength left, VolumePerLength right) + { + return left.Value <= right.GetValueAs(left.Unit); + } + + /// Returns true if greater than or equal to. + public static bool operator >=(VolumePerLength left, VolumePerLength right) + { + return left.Value >= right.GetValueAs(left.Unit); + } + + /// Returns true if less than. + public static bool operator <(VolumePerLength left, VolumePerLength right) + { + return left.Value < right.GetValueAs(left.Unit); + } + + /// Returns true if greater than. + public static bool operator >(VolumePerLength left, VolumePerLength right) + { + return left.Value > right.GetValueAs(left.Unit); + } + + /// Returns true if exactly equal. + /// Consider using for safely comparing floating point values. + public static bool operator ==(VolumePerLength left, VolumePerLength right) + { + return left.Equals(right); + } + + /// Returns true if not exactly equal. + /// Consider using for safely comparing floating point values. + public static bool operator !=(VolumePerLength left, VolumePerLength right) + { + return !(left == right); + } + + /// + public int CompareTo(object obj) + { + if(obj is null) throw new ArgumentNullException(nameof(obj)); + if(!(obj is VolumePerLength objVolumePerLength)) throw new ArgumentException("Expected type VolumePerLength.", nameof(obj)); + + return CompareTo(objVolumePerLength); + } + + /// + public int CompareTo(VolumePerLength other) + { + return _value.CompareTo(other.GetValueAs(this.Unit)); + } + + /// + /// Consider using for safely comparing floating point values. + public override bool Equals(object obj) + { + if(obj is null || !(obj is VolumePerLength objVolumePerLength)) + return false; + + return Equals(objVolumePerLength); + } + + /// + /// Consider using for safely comparing floating point values. + public bool Equals(VolumePerLength other) + { + return _value.Equals(other.GetValueAs(this.Unit)); + } + + /// + /// + /// Compare equality to another VolumePerLength within the given absolute or relative tolerance. + /// + /// + /// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a percentage of this quantity's value. will be converted into + /// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of + /// this quantity's value to be considered equal. + /// + /// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Relative); + /// + /// + /// + /// + /// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a fixed number in this quantity's unit. will be converted into + /// this quantity's unit for comparison. + /// + /// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Absolute); + /// + /// + /// + /// + /// Note that it is advised against specifying zero difference, due to the nature + /// of floating point operations and using System.Double internally. + /// + /// + /// The other quantity to compare to. + /// The absolute or relative tolerance value. Must be greater than or equal to 0. + /// The comparison type: either relative or absolute. + /// True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance. + public bool Equals(VolumePerLength other, double tolerance, ComparisonType comparisonType) + { + if(tolerance < 0) + throw new ArgumentOutOfRangeException("tolerance", "Tolerance must be greater than or equal to 0."); + + double thisValue = (double)this.Value; + double otherValueInThisUnits = other.As(this.Unit); + + return UnitsNet.Comparison.Equals(thisValue, otherValueInThisUnits, tolerance, comparisonType); + } + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current VolumePerLength. + public override int GetHashCode() + { + return new { QuantityType, Value, Unit }.GetHashCode(); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(VolumePerLengthUnit unit) + { + if(Unit == unit) + return Convert.ToDouble(Value); + + var converted = GetValueAs(unit); + return Convert.ToDouble(converted); + } + + /// + public double As(UnitSystem unitSystem) + { + if(unitSystem == null) + throw new ArgumentNullException(nameof(unitSystem)); + + var unitForUnitSystem = Info.GetUnitInfoFor(unitSystem.BaseUnits).Value; + return As(unitForUnitSystem); + } + + /// + double IQuantity.As(Enum unit) + { + if(!(unit is VolumePerLengthUnit unitAsVolumePerLengthUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(VolumePerLengthUnit)} is supported.", nameof(unit)); + + return As(unitAsVolumePerLengthUnit); + } + + /// + /// Converts this VolumePerLength to another VolumePerLength with the unit representation . + /// + /// A VolumePerLength with the specified unit. + public VolumePerLength ToUnit(VolumePerLengthUnit unit) + { + var convertedValue = GetValueAs(unit); + return new VolumePerLength(convertedValue, unit); + } + + /// + IQuantity IQuantity.ToUnit(Enum unit) + { + if(!(unit is VolumePerLengthUnit unitAsVolumePerLengthUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(VolumePerLengthUnit)} is supported.", nameof(unit)); + + return ToUnit(unitAsVolumePerLengthUnit); + } + + /// + public VolumePerLength ToUnit(UnitSystem unitSystem) + { + if(unitSystem == null) + throw new ArgumentNullException(nameof(unitSystem)); + + var unitForUnitSystem = Info.GetUnitInfoFor(unitSystem.BaseUnits).Value; + return ToUnit(unitForUnitSystem); + } + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + IQuantity IQuantity.ToUnit(VolumePerLengthUnit unit) => ToUnit(unit); + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + /// Converts the current value + unit to the base unit. + /// This is typically the first step in converting from one unit to another. + /// + /// The value in the base unit representation. + private double GetValueInBaseUnit() + { + switch(Unit) + { + case VolumePerLengthUnit.CubicMeterPerMeter: return _value; + case VolumePerLengthUnit.LiterPerMeter: return _value/1000; + case VolumePerLengthUnit.OilBarrelPerFoot: return _value/1.91713408; + default: + throw new NotImplementedException($"Can not convert {Unit} to base units."); + } + } + + private double GetValueAs(VolumePerLengthUnit unit) + { + if(Unit == unit) + return _value; + + var baseUnitValue = GetValueInBaseUnit(); + + switch(unit) + { + case VolumePerLengthUnit.CubicMeterPerMeter: return baseUnitValue; + case VolumePerLengthUnit.LiterPerMeter: return baseUnitValue*1000; + case VolumePerLengthUnit.OilBarrelPerFoot: return baseUnitValue*1.91713408; + default: + throw new NotImplementedException($"Can not convert {Unit} to {unit}."); + } + } + + #endregion + + #region ToString Methods + + /// + /// Gets the default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString("g"); + } + + /// + /// Gets the default string representation of value and unit using the given format provider. + /// + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + public string ToString([CanBeNull] IFormatProvider provider) + { + return ToString("g", provider); + } + + /// + /// Get string representation of value and unit. + /// + /// The number of significant digits after the radix point. + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + [Obsolete(@"This method is deprecated and will be removed at a future release. Please use ToString(""s2"") or ToString(""s2"", provider) where 2 is an example of the number passed to significantDigitsAfterRadix.")] + public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) + { + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); + } + + /// + /// Get string representation of value and unit. + /// + /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." + /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + [Obsolete("This method is deprecated and will be removed at a future release. Please use string.Format().")] + public string ToString([CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) + { + if (format == null) throw new ArgumentNullException(nameof(format)); + if (args == null) throw new ArgumentNullException(nameof(args)); + + provider = provider ?? CultureInfo.CurrentUICulture; + + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); + return string.Format(provider, format, formatArgs); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using . + /// + /// The format string. + /// The string representation. + public string ToString(string format) + { + return ToString(format, CultureInfo.CurrentUICulture); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using the specified format provider, or if null. + /// + /// The format string. + /// Format to use for localization and number formatting. Defaults to if null. + /// The string representation. + public string ToString(string format, IFormatProvider formatProvider) + { + return QuantityFormatter.Format(this, format, formatProvider); + } + + #endregion + + #region IConvertible Methods + + TypeCode IConvertible.GetTypeCode() + { + return TypeCode.Object; + } + + bool IConvertible.ToBoolean(IFormatProvider provider) + { + throw new InvalidCastException($"Converting {typeof(VolumePerLength)} to bool is not supported."); + } + + byte IConvertible.ToByte(IFormatProvider provider) + { + return Convert.ToByte(_value); + } + + char IConvertible.ToChar(IFormatProvider provider) + { + throw new InvalidCastException($"Converting {typeof(VolumePerLength)} to char is not supported."); + } + + DateTime IConvertible.ToDateTime(IFormatProvider provider) + { + throw new InvalidCastException($"Converting {typeof(VolumePerLength)} to DateTime is not supported."); + } + + decimal IConvertible.ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_value); + } + + double IConvertible.ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_value); + } + + short IConvertible.ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_value); + } + + int IConvertible.ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_value); + } + + long IConvertible.ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_value); + } + + sbyte IConvertible.ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_value); + } + + float IConvertible.ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_value); + } + + string IConvertible.ToString(IFormatProvider provider) + { + return ToString("g", provider); + } + + object IConvertible.ToType(Type conversionType, IFormatProvider provider) + { + if(conversionType == typeof(VolumePerLength)) + return this; + else if(conversionType == typeof(VolumePerLengthUnit)) + return Unit; + else if(conversionType == typeof(QuantityType)) + return VolumePerLength.QuantityType; + else if(conversionType == typeof(BaseDimensions)) + return VolumePerLength.BaseDimensions; + else + throw new InvalidCastException($"Converting {typeof(VolumePerLength)} to {conversionType} is not supported."); + } + + ushort IConvertible.ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_value); + } + + uint IConvertible.ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_value); + } + + ulong IConvertible.ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_value); + } + + #endregion + } +} diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index 25f1546c97..2e33636a80 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -220,6 +220,8 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu return Volume.From(value, Volume.BaseUnit); case QuantityType.VolumeFlow: return VolumeFlow.From(value, VolumeFlow.BaseUnit); + case QuantityType.VolumePerLength: + return VolumePerLength.From(value, VolumePerLength.BaseUnit); default: throw new ArgumentException($"{quantityType} is not a supported quantity type."); } @@ -506,6 +508,9 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity quantit case VolumeFlowUnit volumeFlowUnit: quantity = VolumeFlow.From(value, volumeFlowUnit); return true; + case VolumePerLengthUnit volumePerLengthUnit: + quantity = VolumePerLength.From(value, volumePerLengthUnit); + return true; default: { quantity = default(IQuantity); @@ -713,6 +718,8 @@ public static bool TryParse([CanBeNull] IFormatProvider formatProvider, Type qua return parser.TryParse(quantityString, formatProvider, Volume.From, out quantity); case Type _ when quantityType == typeof(VolumeFlow): return parser.TryParse(quantityString, formatProvider, VolumeFlow.From, out quantity); + case Type _ when quantityType == typeof(VolumePerLength): + return parser.TryParse(quantityString, formatProvider, VolumePerLength.From, out quantity); default: return false; } diff --git a/UnitsNet/GeneratedCode/QuantityType.g.cs b/UnitsNet/GeneratedCode/QuantityType.g.cs index a6ae6556ca..f694839353 100644 --- a/UnitsNet/GeneratedCode/QuantityType.g.cs +++ b/UnitsNet/GeneratedCode/QuantityType.g.cs @@ -121,6 +121,7 @@ public enum QuantityType VitaminA, Volume, VolumeFlow, + VolumePerLength, // Missing XML comment for public type or member #pragma warning restore CS1591 } diff --git a/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs b/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs index 9018fb5310..c0dda6bcd5 100644 --- a/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs +++ b/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs @@ -1136,6 +1136,9 @@ private static readonly (string CultureName, Type UnitType, int UnitValue, strin ("en-US", typeof(VolumeFlowUnit), (int)VolumeFlowUnit.UsGallonPerHour, new string[]{"gal (U.S.)/h"}), ("en-US", typeof(VolumeFlowUnit), (int)VolumeFlowUnit.UsGallonPerMinute, new string[]{"gal (U.S.)/min", "GPM"}), ("en-US", typeof(VolumeFlowUnit), (int)VolumeFlowUnit.UsGallonPerSecond, new string[]{"gal (U.S.)/s"}), + ("en-US", typeof(VolumePerLengthUnit), (int)VolumePerLengthUnit.CubicMeterPerMeter, new string[]{"m³/m"}), + ("en-US", typeof(VolumePerLengthUnit), (int)VolumePerLengthUnit.LiterPerMeter, new string[]{"l/m"}), + ("en-US", typeof(VolumePerLengthUnit), (int)VolumePerLengthUnit.OilBarrelPerFoot, new string[]{"bbl/ft"}), }; } } diff --git a/UnitsNet/GeneratedCode/Units/VolumePerLengthUnit.g.cs b/UnitsNet/GeneratedCode/Units/VolumePerLengthUnit.g.cs new file mode 100644 index 0000000000..0f1de7c933 --- /dev/null +++ b/UnitsNet/GeneratedCode/Units/VolumePerLengthUnit.g.cs @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum VolumePerLengthUnit + { + Undefined = 0, + CubicMeterPerMeter, + LiterPerMeter, + OilBarrelPerFoot, + } + + #pragma warning restore 1591 +}