diff --git a/UnitsNet.Tests/CustomCode/IrradianceTests.cs b/UnitsNet.Tests/CustomCode/IrradianceTests.cs new file mode 100644 index 0000000000..0fe2c89ebc --- /dev/null +++ b/UnitsNet.Tests/CustomCode/IrradianceTests.cs @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// 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\MyUnit.extra.cs files to add code to generated quantities. +// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior. +// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + + +using System; + +namespace UnitsNet.Tests.CustomCode +{ + public class IrradianceTests : IrradianceTestsBase + { + /// + /// Gets the kilowatt per square meter in one watt per square meter. + /// + protected override double KilowattsPerSquareMeterInOneWattPerSquareMeter => 1E-3; + + /// + /// Gets the watt per square meter in one watt per square meter. + /// + protected override double WattsPerSquareMeterInOneWattPerSquareMeter => 1; + } +} diff --git a/UnitsNet.Tests/GeneratedCode/IrradianceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/IrradianceTestsBase.g.cs new file mode 100644 index 0000000000..a3cefccac8 --- /dev/null +++ b/UnitsNet.Tests/GeneratedCode/IrradianceTestsBase.g.cs @@ -0,0 +1,185 @@ +//------------------------------------------------------------------------------ +// +// 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\MyUnit.extra.cs files to add code to generated quantities. +// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior. +// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using UnitsNet.Units; +using Xunit; + +// Disable build warning CS1718: Comparison made to same variable; did you mean to compare something else? +#pragma warning disable 1718 + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Tests +{ + /// + /// Test of Irradiance. + /// +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class IrradianceTestsBase + { + protected abstract double KilowattsPerSquareMeterInOneWattPerSquareMeter { get; } + protected abstract double WattsPerSquareMeterInOneWattPerSquareMeter { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double KilowattsPerSquareMeterTolerance { get { return 1e-5; } } + protected virtual double WattsPerSquareMeterTolerance { get { return 1e-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + [Fact] + public void WattPerSquareMeterToIrradianceUnits() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + AssertEx.EqualTolerance(KilowattsPerSquareMeterInOneWattPerSquareMeter, wattpersquaremeter.KilowattsPerSquareMeter, KilowattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(WattsPerSquareMeterInOneWattPerSquareMeter, wattpersquaremeter.WattsPerSquareMeter, WattsPerSquareMeterTolerance); + } + + [Fact] + public void FromValueAndUnit() + { + AssertEx.EqualTolerance(1, Irradiance.From(1, IrradianceUnit.KilowattPerSquareMeter).KilowattsPerSquareMeter, KilowattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(1, Irradiance.From(1, IrradianceUnit.WattPerSquareMeter).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + } + + [Fact] + public void As() + { + var wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + AssertEx.EqualTolerance(KilowattsPerSquareMeterInOneWattPerSquareMeter, wattpersquaremeter.As(IrradianceUnit.KilowattPerSquareMeter), KilowattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(WattsPerSquareMeterInOneWattPerSquareMeter, wattpersquaremeter.As(IrradianceUnit.WattPerSquareMeter), WattsPerSquareMeterTolerance); + } + + [Fact] + public void ConversionRoundTrip() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + AssertEx.EqualTolerance(1, Irradiance.FromKilowattsPerSquareMeter(wattpersquaremeter.KilowattsPerSquareMeter).WattsPerSquareMeter, KilowattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(1, Irradiance.FromWattsPerSquareMeter(wattpersquaremeter.WattsPerSquareMeter).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + } + + [Fact] + public void ArithmeticOperators() + { + Irradiance v = Irradiance.FromWattsPerSquareMeter(1); + AssertEx.EqualTolerance(-1, -v.WattsPerSquareMeter, WattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(2, (Irradiance.FromWattsPerSquareMeter(3)-v).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(2, (v + v).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(10, (v*10).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(10, (10*v).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(2, (Irradiance.FromWattsPerSquareMeter(10)/5).WattsPerSquareMeter, WattsPerSquareMeterTolerance); + AssertEx.EqualTolerance(2, Irradiance.FromWattsPerSquareMeter(10)/Irradiance.FromWattsPerSquareMeter(5), WattsPerSquareMeterTolerance); + } + + [Fact] + public void ComparisonOperators() + { + Irradiance oneWattPerSquareMeter = Irradiance.FromWattsPerSquareMeter(1); + Irradiance twoWattsPerSquareMeter = Irradiance.FromWattsPerSquareMeter(2); + + Assert.True(oneWattPerSquareMeter < twoWattsPerSquareMeter); + Assert.True(oneWattPerSquareMeter <= twoWattsPerSquareMeter); + Assert.True(twoWattsPerSquareMeter > oneWattPerSquareMeter); + Assert.True(twoWattsPerSquareMeter >= oneWattPerSquareMeter); + + Assert.False(oneWattPerSquareMeter > twoWattsPerSquareMeter); + Assert.False(oneWattPerSquareMeter >= twoWattsPerSquareMeter); + Assert.False(twoWattsPerSquareMeter < oneWattPerSquareMeter); + Assert.False(twoWattsPerSquareMeter <= oneWattPerSquareMeter); + } + + [Fact] + public void CompareToIsImplemented() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + Assert.Equal(0, wattpersquaremeter.CompareTo(wattpersquaremeter)); + Assert.True(wattpersquaremeter.CompareTo(Irradiance.Zero) > 0); + Assert.True(Irradiance.Zero.CompareTo(wattpersquaremeter) < 0); + } + + [Fact] + public void CompareToThrowsOnTypeMismatch() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + Assert.Throws(() => wattpersquaremeter.CompareTo(new object())); + } + + [Fact] + public void CompareToThrowsOnNull() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + Assert.Throws(() => wattpersquaremeter.CompareTo(null)); + } + + + [Fact] + public void EqualityOperators() + { + Irradiance a = Irradiance.FromWattsPerSquareMeter(1); + Irradiance b = Irradiance.FromWattsPerSquareMeter(2); + +// ReSharper disable EqualExpressionComparison + Assert.True(a == a); + Assert.True(a != b); + + Assert.False(a == b); + Assert.False(a != a); +// ReSharper restore EqualExpressionComparison + } + + [Fact] + public void EqualsIsImplemented() + { + Irradiance v = Irradiance.FromWattsPerSquareMeter(1); + Assert.True(v.Equals(Irradiance.FromWattsPerSquareMeter(1), Irradiance.FromWattsPerSquareMeter(WattsPerSquareMeterTolerance))); + Assert.False(v.Equals(Irradiance.Zero, Irradiance.FromWattsPerSquareMeter(WattsPerSquareMeterTolerance))); + } + + [Fact] + public void EqualsReturnsFalseOnTypeMismatch() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + Assert.False(wattpersquaremeter.Equals(new object())); + } + + [Fact] + public void EqualsReturnsFalseOnNull() + { + Irradiance wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1); + Assert.False(wattpersquaremeter.Equals(null)); + } + } +} diff --git a/UnitsNet/GeneratedCode/Extensions/Number/NumberToIrradianceExtensions.g.cs b/UnitsNet/GeneratedCode/Extensions/Number/NumberToIrradianceExtensions.g.cs new file mode 100644 index 0000000000..e71dbfe4a0 --- /dev/null +++ b/UnitsNet/GeneratedCode/Extensions/Number/NumberToIrradianceExtensions.g.cs @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// 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\MyUnit.extra.cs files to add code to generated quantities. +// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior. +// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; + +// Windows Runtime Component does not support extension methods and method overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx +#if !WINDOWS_UWP +namespace UnitsNet.Extensions.NumberToIrradiance +{ + public static class NumberToIrradianceExtensions + { + #region KilowattPerSquareMeter + + /// + public static Irradiance KilowattsPerSquareMeter(this int value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance? KilowattsPerSquareMeter(this int? value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance KilowattsPerSquareMeter(this long value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance? KilowattsPerSquareMeter(this long? value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance KilowattsPerSquareMeter(this double value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance? KilowattsPerSquareMeter(this double? value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance KilowattsPerSquareMeter(this float value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance? KilowattsPerSquareMeter(this float? value) => Irradiance.FromKilowattsPerSquareMeter(value); + + /// + public static Irradiance KilowattsPerSquareMeter(this decimal value) => Irradiance.FromKilowattsPerSquareMeter(Convert.ToDouble(value)); + + /// + public static Irradiance? KilowattsPerSquareMeter(this decimal? value) => Irradiance.FromKilowattsPerSquareMeter(value == null ? (double?)null : Convert.ToDouble(value.Value)); + + #endregion + + #region WattPerSquareMeter + + /// + public static Irradiance WattsPerSquareMeter(this int value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance? WattsPerSquareMeter(this int? value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance WattsPerSquareMeter(this long value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance? WattsPerSquareMeter(this long? value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance WattsPerSquareMeter(this double value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance? WattsPerSquareMeter(this double? value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance WattsPerSquareMeter(this float value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance? WattsPerSquareMeter(this float? value) => Irradiance.FromWattsPerSquareMeter(value); + + /// + public static Irradiance WattsPerSquareMeter(this decimal value) => Irradiance.FromWattsPerSquareMeter(Convert.ToDouble(value)); + + /// + public static Irradiance? WattsPerSquareMeter(this decimal? value) => Irradiance.FromWattsPerSquareMeter(value == null ? (double?)null : Convert.ToDouble(value.Value)); + + #endregion + + } +} +#endif diff --git a/UnitsNet/GeneratedCode/Quantities/Irradiance.g.cs b/UnitsNet/GeneratedCode/Quantities/Irradiance.g.cs new file mode 100644 index 0000000000..2567d73493 --- /dev/null +++ b/UnitsNet/GeneratedCode/Quantities/Irradiance.g.cs @@ -0,0 +1,724 @@ +//------------------------------------------------------------------------------ +// +// 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\MyUnit.extra.cs files to add code to generated quantities. +// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior. +// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text.RegularExpressions; +using System.Linq; +using JetBrains.Annotations; +using UnitsNet.Units; + +// Windows Runtime Component does not support CultureInfo type, so use culture name string instead for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx +#if WINDOWS_UWP +using Culture = System.String; +#else +using Culture = System.IFormatProvider; +#endif + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// Irradiance is the intensity of ultraviolet (UV) or visible light incident on a surface. + /// + // ReSharper disable once PartialTypeWithSinglePart + + // 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. +#if WINDOWS_UWP + public sealed partial class Irradiance +#else + public partial struct Irradiance : IComparable, IComparable +#endif + { + /// + /// Base unit of Irradiance. + /// + private readonly double _wattsPerSquareMeter; + + // Windows Runtime Component requires a default constructor +#if WINDOWS_UWP + public Irradiance() : this(0) + { + } +#endif + + public Irradiance(double wattspersquaremeter) + { + _wattsPerSquareMeter = Convert.ToDouble(wattspersquaremeter); + } + + // 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 +#if WINDOWS_UWP + private +#else + public +#endif + Irradiance(long wattspersquaremeter) + { + _wattsPerSquareMeter = Convert.ToDouble(wattspersquaremeter); + } + + // 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 + // Windows Runtime Component does not support decimal type +#if WINDOWS_UWP + private +#else + public +#endif + Irradiance(decimal wattspersquaremeter) + { + _wattsPerSquareMeter = Convert.ToDouble(wattspersquaremeter); + } + + #region Properties + + /// + /// The of this quantity. + /// + public static QuantityType QuantityType => QuantityType.Irradiance; + + /// + /// The base unit representation of this quantity for the numeric value stored internally. All conversions go via this value. + /// + public static IrradianceUnit BaseUnit + { + get { return IrradianceUnit.WattPerSquareMeter; } + } + + /// + /// All units of measurement for the Irradiance quantity. + /// + public static IrradianceUnit[] Units { get; } = Enum.GetValues(typeof(IrradianceUnit)).Cast().ToArray(); + + /// + /// Get Irradiance in KilowattsPerSquareMeter. + /// + public double KilowattsPerSquareMeter + { + get { return (_wattsPerSquareMeter) / 1e3d; } + } + + /// + /// Get Irradiance in WattsPerSquareMeter. + /// + public double WattsPerSquareMeter + { + get { return _wattsPerSquareMeter; } + } + + #endregion + + #region Static + + public static Irradiance Zero + { + get { return new Irradiance(); } + } + + /// + /// Get Irradiance from KilowattsPerSquareMeter. + /// +#if WINDOWS_UWP + [Windows.Foundation.Metadata.DefaultOverload] + public static Irradiance FromKilowattsPerSquareMeter(double kilowattspersquaremeter) + { + double value = (double) kilowattspersquaremeter; + return new Irradiance((value) * 1e3d); + } +#else + public static Irradiance FromKilowattsPerSquareMeter(QuantityValue kilowattspersquaremeter) + { + double value = (double) kilowattspersquaremeter; + return new Irradiance(((value) * 1e3d)); + } +#endif + + /// + /// Get Irradiance from WattsPerSquareMeter. + /// +#if WINDOWS_UWP + [Windows.Foundation.Metadata.DefaultOverload] + public static Irradiance FromWattsPerSquareMeter(double wattspersquaremeter) + { + double value = (double) wattspersquaremeter; + return new Irradiance(value); + } +#else + public static Irradiance FromWattsPerSquareMeter(QuantityValue wattspersquaremeter) + { + double value = (double) wattspersquaremeter; + return new Irradiance((value)); + } +#endif + + // Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx +#if !WINDOWS_UWP + /// + /// Get nullable Irradiance from nullable KilowattsPerSquareMeter. + /// + public static Irradiance? FromKilowattsPerSquareMeter(QuantityValue? kilowattspersquaremeter) + { + if (kilowattspersquaremeter.HasValue) + { + return FromKilowattsPerSquareMeter(kilowattspersquaremeter.Value); + } + else + { + return null; + } + } + + /// + /// Get nullable Irradiance from nullable WattsPerSquareMeter. + /// + public static Irradiance? FromWattsPerSquareMeter(QuantityValue? wattspersquaremeter) + { + if (wattspersquaremeter.HasValue) + { + return FromWattsPerSquareMeter(wattspersquaremeter.Value); + } + else + { + return null; + } + } + +#endif + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// Irradiance unit value. +#if WINDOWS_UWP + // Fix name conflict with parameter "value" + [return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("returnValue")] + public static Irradiance From(double value, IrradianceUnit fromUnit) +#else + public static Irradiance From(QuantityValue value, IrradianceUnit fromUnit) +#endif + { + switch (fromUnit) + { + case IrradianceUnit.KilowattPerSquareMeter: + return FromKilowattsPerSquareMeter(value); + case IrradianceUnit.WattPerSquareMeter: + return FromWattsPerSquareMeter(value); + + default: + throw new NotImplementedException("fromUnit: " + fromUnit); + } + } + + // Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx +#if !WINDOWS_UWP + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// Irradiance unit value. + public static Irradiance? From(QuantityValue? value, IrradianceUnit fromUnit) + { + if (!value.HasValue) + { + return null; + } + switch (fromUnit) + { + case IrradianceUnit.KilowattPerSquareMeter: + return FromKilowattsPerSquareMeter(value.Value); + case IrradianceUnit.WattPerSquareMeter: + return FromWattsPerSquareMeter(value.Value); + + default: + throw new NotImplementedException("fromUnit: " + fromUnit); + } + } +#endif + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + [UsedImplicitly] + public static string GetAbbreviation(IrradianceUnit unit) + { + return GetAbbreviation(unit, null); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Culture to use for localization. Defaults to Thread.CurrentUICulture. + /// Unit abbreviation string. + [UsedImplicitly] + public static string GetAbbreviation(IrradianceUnit unit, [CanBeNull] Culture culture) + { + return UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit); + } + + #endregion + + #region Arithmetic Operators + + // Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx +#if !WINDOWS_UWP + public static Irradiance operator -(Irradiance right) + { + return new Irradiance(-right._wattsPerSquareMeter); + } + + public static Irradiance operator +(Irradiance left, Irradiance right) + { + return new Irradiance(left._wattsPerSquareMeter + right._wattsPerSquareMeter); + } + + public static Irradiance operator -(Irradiance left, Irradiance right) + { + return new Irradiance(left._wattsPerSquareMeter - right._wattsPerSquareMeter); + } + + public static Irradiance operator *(double left, Irradiance right) + { + return new Irradiance(left*right._wattsPerSquareMeter); + } + + public static Irradiance operator *(Irradiance left, double right) + { + return new Irradiance(left._wattsPerSquareMeter*(double)right); + } + + public static Irradiance operator /(Irradiance left, double right) + { + return new Irradiance(left._wattsPerSquareMeter/(double)right); + } + + public static double operator /(Irradiance left, Irradiance right) + { + return Convert.ToDouble(left._wattsPerSquareMeter/right._wattsPerSquareMeter); + } +#endif + + #endregion + + #region Equality / IComparable + + public int CompareTo(object obj) + { + if (obj == null) throw new ArgumentNullException("obj"); + if (!(obj is Irradiance)) throw new ArgumentException("Expected type Irradiance.", "obj"); + return CompareTo((Irradiance) obj); + } + + // 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 +#if WINDOWS_UWP + internal +#else + public +#endif + int CompareTo(Irradiance other) + { + return _wattsPerSquareMeter.CompareTo(other._wattsPerSquareMeter); + } + + // Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx +#if !WINDOWS_UWP + public static bool operator <=(Irradiance left, Irradiance right) + { + return left._wattsPerSquareMeter <= right._wattsPerSquareMeter; + } + + public static bool operator >=(Irradiance left, Irradiance right) + { + return left._wattsPerSquareMeter >= right._wattsPerSquareMeter; + } + + public static bool operator <(Irradiance left, Irradiance right) + { + return left._wattsPerSquareMeter < right._wattsPerSquareMeter; + } + + public static bool operator >(Irradiance left, Irradiance right) + { + return left._wattsPerSquareMeter > right._wattsPerSquareMeter; + } + + [Obsolete("It is not safe to compare equality due to using System.Double as the internal representation. It is very easy to get slightly different values due to floating point operations. Instead use Equals(other, maxError) to provide the max allowed error.")] + public static bool operator ==(Irradiance left, Irradiance right) + { + // ReSharper disable once CompareOfFloatsByEqualityOperator + return left._wattsPerSquareMeter == right._wattsPerSquareMeter; + } + + [Obsolete("It is not safe to compare equality due to using System.Double as the internal representation. It is very easy to get slightly different values due to floating point operations. Instead use Equals(other, maxError) to provide the max allowed error.")] + public static bool operator !=(Irradiance left, Irradiance right) + { + // ReSharper disable once CompareOfFloatsByEqualityOperator + return left._wattsPerSquareMeter != right._wattsPerSquareMeter; + } +#endif + + [Obsolete("It is not safe to compare equality due to using System.Double as the internal representation. It is very easy to get slightly different values due to floating point operations. Instead use Equals(other, maxError) to provide the max allowed error.")] + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + return _wattsPerSquareMeter.Equals(((Irradiance) obj)._wattsPerSquareMeter); + } + + /// + /// Compare equality to another Irradiance by specifying a max allowed difference. + /// Note that it is advised against specifying zero difference, due to the nature + /// of floating point operations and using System.Double internally. + /// + /// Other quantity to compare to. + /// Max error allowed. + /// True if the difference between the two values is not greater than the specified max. + public bool Equals(Irradiance other, Irradiance maxError) + { + return Math.Abs(_wattsPerSquareMeter - other._wattsPerSquareMeter) <= maxError._wattsPerSquareMeter; + } + + public override int GetHashCode() + { + return _wattsPerSquareMeter.GetHashCode(); + } + + #endregion + + #region Conversion + + /// + /// Convert to the unit representation . + /// + /// Value in new unit if successful, exception otherwise. + /// If conversion was not successful. + public double As(IrradianceUnit unit) + { + switch (unit) + { + case IrradianceUnit.KilowattPerSquareMeter: + return KilowattsPerSquareMeter; + case IrradianceUnit.WattPerSquareMeter: + return WattsPerSquareMeter; + + default: + throw new NotImplementedException("unit: " + unit); + } + } + + #endregion + + #region Parsing + + /// + /// 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 Irradiance 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} + /// Format to use when parsing number and unit. If it is null, it defaults to for parsing the number and for parsing the unit abbreviation by culture/language. + /// + /// 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 Irradiance Parse(string str, [CanBeNull] Culture culture) + { + if (str == null) throw new ArgumentNullException("str"); + + // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx +#if WINDOWS_UWP + IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); +#else + IFormatProvider formatProvider = culture; +#endif + return QuantityParser.Parse(str, formatProvider, + delegate(string value, string unit, IFormatProvider formatProvider2) + { + double parsedValue = double.Parse(value, formatProvider2); + IrradianceUnit parsedUnit = ParseUnit(unit, formatProvider2); + return From(parsedValue, parsedUnit); + }, (x, y) => FromWattsPerSquareMeter(x.WattsPerSquareMeter + y.WattsPerSquareMeter)); + } + + /// + /// 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 Irradiance 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} + /// Format to use when parsing number and unit. If it is null, it defaults to for parsing the number and for parsing the unit abbreviation by culture/language. + /// Resulting unit quantity if successful. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out Irradiance result) + { + try + { + result = Parse(str, culture); + return true; + } + catch + { + result = default(Irradiance); + return false; + } + } + + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static IrradianceUnit ParseUnit(string str) + { + return ParseUnit(str, (IFormatProvider)null); + } + + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static IrradianceUnit ParseUnit(string str, [CanBeNull] string cultureName) + { + return ParseUnit(str, cultureName == null ? null : new CultureInfo(cultureName)); + } + + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + + // 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 +#if WINDOWS_UWP + internal +#else + public +#endif + static IrradianceUnit ParseUnit(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + + var unitSystem = UnitSystem.GetCached(formatProvider); + var unit = unitSystem.Parse(str.Trim()); + + if (unit == IrradianceUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized IrradianceUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider?.ToString() ?? "(null)"; + throw newEx; + } + + return unit; + } + + #endregion + + /// + /// Set the default unit used by ToString(). Default is WattPerSquareMeter + /// + public static IrradianceUnit ToStringDefaultUnit { get; set; } = IrradianceUnit.WattPerSquareMeter; + + /// + /// Get default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString(ToStringDefaultUnit); + } + + /// + /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. + /// + /// Unit representation to use. + /// String representation. + public string ToString(IrradianceUnit unit) + { + return ToString(unit, null, 2); + } + + /// + /// Get string representation of value and unit. Using two significant digits after radix. + /// + /// Unit representation to use. + /// Culture to use for localization and number formatting. + /// String representation. + public string ToString(IrradianceUnit unit, [CanBeNull] Culture culture) + { + return ToString(unit, culture, 2); + } + + /// + /// Get string representation of value and unit. + /// + /// Unit representation to use. + /// Culture to use for localization and number formatting. + /// The number of significant digits after the radix point. + /// String representation. + [UsedImplicitly] + public string ToString(IrradianceUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix) + { + double value = As(unit); + string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(unit, culture, format); + } + + /// + /// Get string representation of value and unit. + /// + /// Culture to use for localization and number formatting. + /// Unit representation to use. + /// 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. + [UsedImplicitly] + public string ToString(IrradianceUnit unit, [CanBeNull] Culture culture, [NotNull] string format, + [NotNull] params object[] args) + { + if (format == null) throw new ArgumentNullException(nameof(format)); + if (args == null) throw new ArgumentNullException(nameof(args)); + + // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx +#if WINDOWS_UWP + IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); +#else + IFormatProvider formatProvider = culture; +#endif + double value = As(unit); + object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args); + return string.Format(formatProvider, format, formatArgs); + } + + /// + /// Represents the largest possible value of Irradiance + /// + public static Irradiance MaxValue + { + get + { + return new Irradiance(double.MaxValue); + } + } + + /// + /// Represents the smallest possible value of Irradiance + /// + public static Irradiance MinValue + { + get + { + return new Irradiance(double.MinValue); + } + } + } +} diff --git a/UnitsNet/GeneratedCode/QuantityType.g.cs b/UnitsNet/GeneratedCode/QuantityType.g.cs index 06688fd25d..508d5e663a 100644 --- a/UnitsNet/GeneratedCode/QuantityType.g.cs +++ b/UnitsNet/GeneratedCode/QuantityType.g.cs @@ -74,6 +74,7 @@ public enum QuantityType ForcePerLength, Frequency, Information, + Irradiance, KinematicViscosity, LapseRate, Length, diff --git a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs index 1a0537c3df..f0024b5084 100644 --- a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs +++ b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs @@ -1489,6 +1489,20 @@ private static readonly ReadOnlyCollection DefaultLocalization new AbbreviationsForCulture("en-US", "TB"), }), }), + new UnitLocalization(typeof (IrradianceUnit), + new[] + { + new CulturesForEnumValue((int) IrradianceUnit.KilowattPerSquareMeter, + new[] + { + new AbbreviationsForCulture("en-US", "kW/m²"), + }), + new CulturesForEnumValue((int) IrradianceUnit.WattPerSquareMeter, + new[] + { + new AbbreviationsForCulture("en-US", "W/m²"), + }), + }), new UnitLocalization(typeof (KinematicViscosityUnit), new[] { diff --git a/UnitsNet/GeneratedCode/Units/IrradianceUnit.g.cs b/UnitsNet/GeneratedCode/Units/IrradianceUnit.g.cs new file mode 100644 index 0000000000..a91760489c --- /dev/null +++ b/UnitsNet/GeneratedCode/Units/IrradianceUnit.g.cs @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// 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\MyUnit.extra.cs files to add code to generated quantities. +// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior. +// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// https://github.com/angularsen/UnitsNet +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + public enum IrradianceUnit + { + Undefined = 0, + KilowattPerSquareMeter, + WattPerSquareMeter, + } +} diff --git a/UnitsNet/UnitDefinitions/Irradiance.json b/UnitsNet/UnitDefinitions/Irradiance.json new file mode 100644 index 0000000000..456c924027 --- /dev/null +++ b/UnitsNet/UnitDefinitions/Irradiance.json @@ -0,0 +1,20 @@ +{ + "Name": "Irradiance", + "BaseUnit": "WattPerSquareMeter", + "XmlDoc": "Irradiance is the intensity of ultraviolet (UV) or visible light incident on a surface.", + "Units": [ + { + "SingularName": "WattPerSquareMeter", + "PluralName": "WattsPerSquareMeter", + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Prefixes": [ "Kilo" ], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "W/m²" ] + } + ] + } + ] +} \ No newline at end of file