diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index 7ea943e63e..cecb54327c 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -6,6 +6,54 @@ namespace UnitsNet.Tests { public class BaseDimensionsTests { + [Fact] + public void ConstructorImplementedCorrectly() + { + var baseDimensions = new BaseDimensions(1, 2, 3, 4, 5, 6, 7); + + Assert.True(baseDimensions.Length == 1); + Assert.True(baseDimensions.Mass == 2); + Assert.True(baseDimensions.Time == 3); + Assert.True(baseDimensions.Current == 4); + Assert.True(baseDimensions.Temperature == 5); + Assert.True(baseDimensions.Amount == 6); + Assert.True(baseDimensions.LuminousIntensity == 7); + } + + [Theory] + [InlineData(1, 0, 0, 0, 0, 0, 0)] + [InlineData(0, 1, 0, 0, 0, 0, 0)] + [InlineData(0, 0, 1, 0, 0, 0, 0)] + [InlineData(0, 0, 0, 1, 0, 0, 0)] + [InlineData(0, 0, 0, 0, 1, 0, 0)] + [InlineData(0, 0, 0, 0, 0, 1, 0)] + [InlineData(0, 0, 0, 0, 0, 0, 1)] + public void IsBaseQuantityImplementedProperly(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity) + { + var baseDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity); + var derivedDimensions = new BaseDimensions(length * 2, mass * 2, time * 2, current * 2, temperature * 2, amount * 2, luminousIntensity * 2); + + Assert.True(baseDimensions.IsBaseQuantity()); + Assert.False(derivedDimensions.IsBaseQuantity()); + } + + [Theory] + [InlineData(2, 0, 0, 0, 0, 0, 0)] + [InlineData(0, 2, 0, 0, 0, 0, 0)] + [InlineData(0, 0, 2, 0, 0, 0, 0)] + [InlineData(0, 0, 0, 2, 0, 0, 0)] + [InlineData(0, 0, 0, 0, 2, 0, 0)] + [InlineData(0, 0, 0, 0, 0, 2, 0)] + [InlineData(0, 0, 0, 0, 0, 0, 2)] + public void IsDerivedQuantityImplementedProperly(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity) + { + var baseDimensions = new BaseDimensions(length / 2, mass / 2, time / 2, current / 2, temperature / 2, amount / 2, luminousIntensity / 2); + var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity); + + Assert.False(baseDimensions.IsDerivedQuantity()); + Assert.True(derivedDimensions.IsDerivedQuantity()); + } + [Fact] public void EqualsWorksAsExpected() { @@ -32,6 +80,11 @@ public void EqualityOperatorsWorkAsExpected() Assert.False(baseDimensions2 == null); Assert.False(null == baseDimensions2); + + BaseDimensions nullBaseDimensions1 = null; + BaseDimensions nullBaseDimensions2 = null; + + Assert.True(nullBaseDimensions1 == nullBaseDimensions2); } [Fact] @@ -48,6 +101,11 @@ public void InequalityOperatorsWorkAsExpected() Assert.True(baseDimensions2 != null); Assert.True(null != baseDimensions2); + + BaseDimensions nullBaseDimensions1 = null; + BaseDimensions nullBaseDimensions2 = null; + + Assert.False(nullBaseDimensions1 != nullBaseDimensions2); } [Fact] diff --git a/UnitsNet.Tests/BaseUnitsTests.cs b/UnitsNet.Tests/BaseUnitsTests.cs new file mode 100644 index 0000000000..ad3a31370d --- /dev/null +++ b/UnitsNet.Tests/BaseUnitsTests.cs @@ -0,0 +1,121 @@ +// 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; + +namespace UnitsNet.Tests +{ + public class BaseUnitsTests + { + private static BaseUnits siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + private static BaseUnits siBaseUnitsCopy = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + private static BaseUnits nonSiBaseUnits = new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + [Fact] + public void ConstructorSetsUnitsProperly() + { + var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Equal(LengthUnit.Meter, baseUnits.Length); + Assert.Equal(MassUnit.Kilogram, baseUnits.Mass); + Assert.Equal(DurationUnit.Second, baseUnits.Time); + Assert.Equal(ElectricCurrentUnit.Ampere, baseUnits.Current); + Assert.Equal(TemperatureUnit.Kelvin, baseUnits.Temperature); + Assert.Equal(AmountOfSubstanceUnit.Mole, baseUnits.Amount); + Assert.Equal(LuminousIntensityUnit.Candela, baseUnits.LuminousIntensity); + } + + [Fact] + public void EqualsObjectIsImplementedCorrectly() + { + Assert.True(siBaseUnits.Equals((object)siBaseUnitsCopy)); + Assert.False(siBaseUnits.Equals((object)nonSiBaseUnits)); + + Assert.False(siBaseUnits.Equals("Some object.")); + Assert.False(siBaseUnits.Equals((IFormatProvider)null)); + } + + [Fact] + public void EqualsBaseUnitsIsImplementedCorrectly() + { + Assert.True(siBaseUnits.Equals(siBaseUnitsCopy)); + Assert.True(siBaseUnitsCopy.Equals(siBaseUnits)); + + Assert.False(siBaseUnits.Equals(nonSiBaseUnits)); + Assert.False(nonSiBaseUnits.Equals(siBaseUnits)); + + Assert.False(siBaseUnits.Equals(null)); + } + + [Fact] + public void EqualityOperatorIsImplementedCorrectly() + { + Assert.True(siBaseUnits == siBaseUnitsCopy); + Assert.True(siBaseUnitsCopy == siBaseUnits); + + Assert.False(siBaseUnits == nonSiBaseUnits); + Assert.False(nonSiBaseUnits == siBaseUnits); + + Assert.False(siBaseUnits == null); + Assert.False(null == siBaseUnits); + + BaseUnits nullBaseUnits1 = null; + BaseUnits nullBaseUnits2 = null; + + Assert.True(nullBaseUnits1 == nullBaseUnits2); + } + + [Fact] + public void InequalityOperatorIsImplementedCorrectly() + { + Assert.False(siBaseUnits != siBaseUnitsCopy); + Assert.False(siBaseUnitsCopy != siBaseUnits); + + Assert.True(siBaseUnits != nonSiBaseUnits); + Assert.True(nonSiBaseUnits != siBaseUnits); + + Assert.True(siBaseUnits != null); + Assert.True(null != siBaseUnits); + + BaseUnits nullBaseUnits1 = null; + BaseUnits nullBaseUnits2 = null; + + Assert.False(nullBaseUnits1 != nullBaseUnits2); + } + + [Fact] + public void ToStringGivesExpectedResult() + { + var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", siBaseUnits.ToString()); + } + } +} diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs new file mode 100644 index 0000000000..2a52e693c5 --- /dev/null +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -0,0 +1,171 @@ +// 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; + +namespace UnitsNet.Tests +{ + public class UnitSystemTests + { + [Fact] + public void ConstructorImplementedProperly() + { + var baseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + var unitSystem = new UnitSystem(baseUnits); + + Assert.Equal(unitSystem.BaseUnits, baseUnits); + } + + [Fact] + public void ConstructorThrowsArgumentNullExceptionForNullBaseUnits() + { + Assert.Throws(() => new UnitSystem(null)); + } + + [Theory] + [InlineData(LengthUnit.Undefined, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Undefined, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Undefined, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Undefined, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)] + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Undefined)] + public void ConstructorThrowsArgumentExceptionWithUndefinedUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) + { + var baseUnits = new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity); + Assert.Throws(() => new UnitSystem(baseUnits)); + } + + [Fact] + public void EqualsObjectIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.True(unitSystem1.Equals((object)unitSystem2)); + Assert.False(unitSystem1.Equals((object)unitSystem3)); + + Assert.False(unitSystem1.Equals("Some object.")); + Assert.False(unitSystem1.Equals((IFormatProvider)null)); + } + + [Fact] + public void EqualsUnitSystemIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.True(unitSystem1.Equals(unitSystem2)); + Assert.True(unitSystem2.Equals(unitSystem1)); + + Assert.False(unitSystem1.Equals(unitSystem3)); + Assert.False(unitSystem3.Equals(unitSystem1)); + + Assert.False(unitSystem1.Equals(null)); + } + + [Fact] + public void EqualityOperatorIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.True(unitSystem1 == unitSystem2); + Assert.True(unitSystem2 == unitSystem1); + + Assert.False(unitSystem1 == unitSystem3); + Assert.False(unitSystem3 == unitSystem1); + + Assert.False(unitSystem1 == null); + Assert.False(null == unitSystem1); + + UnitSystem nullUnitSystem1 = null; + UnitSystem nullUnitSystem2 = null; + + Assert.True(nullUnitSystem1 == nullUnitSystem2); + } + + [Fact] + public void InequalityOperatorIsImplementedCorrectly() + { + var unitSystem1 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem2 = new UnitSystem(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + var unitSystem3 = new UnitSystem(new BaseUnits(LengthUnit.Foot, MassUnit.Pound, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.DegreeFahrenheit, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)); + + Assert.False(unitSystem1 != unitSystem2); + Assert.False(unitSystem2 != unitSystem1); + + Assert.True(unitSystem1 != unitSystem3); + Assert.True(unitSystem3 != unitSystem1); + + Assert.True(unitSystem1 != null); + Assert.True(null != unitSystem1); + + UnitSystem nullUnitSystem1 = null; + UnitSystem nullUnitSystem2 = null; + + Assert.False(nullUnitSystem1 != nullUnitSystem2); + } + + [Fact] + public void SIUnitSystemHasCorrectBaseUnits() + { + var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + Assert.Equal(LengthUnit.Meter, UnitSystem.SI.BaseUnits.Length); + Assert.Equal(MassUnit.Kilogram, UnitSystem.SI.BaseUnits.Mass); + Assert.Equal(DurationUnit.Second, UnitSystem.SI.BaseUnits.Time); + Assert.Equal(ElectricCurrentUnit.Ampere, UnitSystem.SI.BaseUnits.Current); + Assert.Equal(TemperatureUnit.Kelvin, UnitSystem.SI.BaseUnits.Temperature); + Assert.Equal(AmountOfSubstanceUnit.Mole, UnitSystem.SI.BaseUnits.Amount); + Assert.Equal(LuminousIntensityUnit.Candela, UnitSystem.SI.BaseUnits.LuminousIntensity); + } + } +} diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index 8f1f0ea7d2..6e82d94ab1 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -21,6 +21,7 @@ using System; using System.Text; +using System.Linq; namespace UnitsNet { @@ -41,6 +42,26 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu LuminousIntensity = luminousIntensity; } + /// + /// Checks if the dimensions represent a base quantity. + /// + /// True if the dimensions represent a base quantity, otherwise false. + public bool IsBaseQuantity() + { + var dimensionsArray = new int[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}; + bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1; + return onlyOneEqualsOne; + } + + /// + /// Checks the dimensions represent a derived quantity. + /// + /// True if the dimensions represent a derived quantity, otherwise false. + public bool IsDerivedQuantity() + { + return !IsBaseQuantity(); + } + /// public override bool Equals(object obj) { @@ -105,6 +126,7 @@ public BaseDimensions Divide(BaseDimensions right) } #if !WINDOWS_UWP + /// /// Check if two dimensions are equal. /// @@ -113,7 +135,7 @@ public BaseDimensions Divide(BaseDimensions right) /// True if equal. public static bool operator ==(BaseDimensions left, BaseDimensions right) { - return left?.Equals(right) == true; + return left is null ? right is null : left.Equals(right); } /// @@ -158,6 +180,7 @@ public BaseDimensions Divide(BaseDimensions right) return left.Divide(right); } + #endif /// diff --git a/UnitsNet/BaseUnits.cs b/UnitsNet/BaseUnits.cs new file mode 100644 index 0000000000..16949c0ff6 --- /dev/null +++ b/UnitsNet/BaseUnits.cs @@ -0,0 +1,177 @@ +// 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.Text; +using UnitsNet.Units; + +namespace UnitsNet +{ +#if !WINDOWS_UWP + public sealed partial class BaseUnits : IEquatable { } +#endif + + /// + /// Represents the base units for a quantity. All quantities, both base and derived, can be + /// represented by a combination of these seven base units. + /// + public sealed partial class BaseUnits + { + /// + /// Creates an instance of if the base units class that represents the base units for a quantity. + /// All quantities, both base and derived, can be represented by a combination of these seven base units. + /// + /// The length unit (L). + /// The mass unit (M). + /// The time unit (T). + /// The electric current unit (I). + /// The temperature unit (Θ). + /// The amount of substance unit (N). + /// The luminous intensity unit (J). + public BaseUnits(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) + { + Length = length; + Mass = mass; + Time = time; + Current = current; + Temperature = temperature; + Amount = amount; + LuminousIntensity = luminousIntensity; + } + + /// + public override bool Equals(object obj) + { + if(obj is null || !(obj is BaseUnits)) + return false; + + return Equals((BaseUnits)obj); + } + + /// + /// Checks if all of the base units are equal to another instance's. + /// + /// The other instance to check if equal to. + /// True if equal, otherwise false. +#if WINDOWS_UWP + [Windows.Foundation.Metadata.DefaultOverload] +#endif + public bool Equals(BaseUnits other) + { + if(other is null) + return false; + + return Length == other.Length && + Mass == other.Mass && + Time == other.Time && + Current == other.Current && + Temperature == other.Temperature && + Amount == other.Amount && + LuminousIntensity == other.LuminousIntensity; + } + + /// + public override int GetHashCode() + { + return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode(); + } + +#if !WINDOWS_UWP + + /// + /// Checks if this instance is equal to another. + /// + /// The left instance. + /// The right instance. + /// True if equal, otherwise false. + /// + public static bool operator ==(BaseUnits left, BaseUnits right) + { + return left is null ? right is null : left.Equals(right); + } + + /// + /// Checks if this instance is not equal to another. + /// + /// The left instance. + /// The right instance. + /// True if not equal, otherwise false. + /// + public static bool operator !=(BaseUnits left, BaseUnits right) + { + return !(left == right); + } + +#endif + + /// + public override string ToString() + { + var sb = new StringBuilder(); + + sb.AppendFormat("[Length]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Length)); + sb.AppendFormat("[Mass]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Mass)); + sb.AppendFormat("[Time]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Time)); + sb.AppendFormat("[Current]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Current)); + sb.AppendFormat("[Temperature]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Temperature)); + sb.AppendFormat("[Amount]: {0}, ", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(Amount)); + sb.AppendFormat("[LuminousIntensity]: {0}", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensity)); + + return sb.ToString(); + } + + /// + /// Gets the length unit (L). + /// + public LengthUnit Length { get; } + + /// + /// Gets the mass unit (M). + /// + public MassUnit Mass{ get; } + + /// + /// Gets the time unit (T). + /// + public DurationUnit Time{ get; } + + /// + /// Gets the electric current unit (I). + /// + public ElectricCurrentUnit Current{ get; } + + /// + /// Gets the temperature unit (Θ). + /// + public TemperatureUnit Temperature{ get; } + + /// + /// Gets the amount of substance unit (N). + /// + public AmountOfSubstanceUnit Amount{ get; } + + /// + /// Gets the luminous intensity unit (J). + /// + public LuminousIntensityUnit LuminousIntensity{ get; } + } +} diff --git a/UnitsNet/UnitSystem.cs b/UnitsNet/UnitSystem.cs new file mode 100644 index 0000000000..695626b763 --- /dev/null +++ b/UnitsNet/UnitSystem.cs @@ -0,0 +1,125 @@ +// 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.Text; +using UnitsNet.Units; + +namespace UnitsNet +{ +#if !WINDOWS_UWP + public sealed partial class UnitSystem : IEquatable { } +#endif + + public sealed partial class UnitSystem + { + /// + /// Creates an instance of a unit system with the specified base units. + /// + /// The base units for the unit system. + public UnitSystem(BaseUnits baseUnits) + { + if(baseUnits is null) + throw new ArgumentNullException(nameof(baseUnits)); + + if(baseUnits.Length == LengthUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Mass == MassUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Time == DurationUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Current == ElectricCurrentUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Temperature == TemperatureUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.Amount == AmountOfSubstanceUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + if(baseUnits.LuminousIntensity == LuminousIntensityUnit.Undefined) + throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits)); + + BaseUnits = baseUnits; + } + + /// + public override bool Equals(object obj) + { + if(obj is null || !(obj is UnitSystem)) + return false; + + return Equals((UnitSystem)obj); + } + +#if WINDOWS_UWP + [Windows.Foundation.Metadata.DefaultOverload] +#endif + public bool Equals(UnitSystem other) + { + if(other is null) + return false; + + return BaseUnits.Equals(other.BaseUnits); + } + +#if !WINDOWS_UWP + + /// + /// Checks if this instance is equal to another. + /// + /// The left instance. + /// The right instance. + /// True if equal, otherwise false. + /// + public static bool operator ==(UnitSystem left, UnitSystem right) + { + return left is null ? right is null : left.Equals(right); + } + + /// + /// Checks if this instance is equal to another. + /// + /// The left instance. + /// The right instance. + /// True if equal, otherwise false. + /// + public static bool operator !=(UnitSystem left, UnitSystem right) + { + return !(left == right); + } + +#endif + + /// + public override int GetHashCode() + { + return new {BaseUnits}.GetHashCode(); + } + + public BaseUnits BaseUnits{ get; } + + private static readonly BaseUnits SIBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, + ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela); + + /// + /// Gets the SI unit system. + /// + public static UnitSystem SI{ get; } = new UnitSystem(SIBaseUnits); + } +}