From dac867a07e0ef1f8fefa72945afc56b07cf8a12c Mon Sep 17 00:00:00 2001 From: Geoff Sokoll Date: Fri, 26 Feb 2016 09:00:16 +1000 Subject: [PATCH] Added DynamicViscosity to base units --- .../CustomCode/DynamicViscosityTests.cs | 37 ++ .../DynamicViscosityTestsBase.g.cs | 191 +++++++ .../Enums/DynamicViscosityUnit.g.cs | 34 ++ .../UnitClasses/DynamicViscosity.g.cs | 493 ++++++++++++++++++ .../GeneratedCode/UnitSystem.Default.g.cs | 29 ++ .../UnitDefinitions/DynamicViscosity.json | 46 ++ 6 files changed, 830 insertions(+) create mode 100644 UnitsNet.Tests/CustomCode/DynamicViscosityTests.cs create mode 100644 UnitsNet.Tests/GeneratedCode/DynamicViscosityTestsBase.g.cs create mode 100644 UnitsNet/GeneratedCode/Enums/DynamicViscosityUnit.g.cs create mode 100644 UnitsNet/GeneratedCode/UnitClasses/DynamicViscosity.g.cs create mode 100644 UnitsNet/Scripts/UnitDefinitions/DynamicViscosity.json diff --git a/UnitsNet.Tests/CustomCode/DynamicViscosityTests.cs b/UnitsNet.Tests/CustomCode/DynamicViscosityTests.cs new file mode 100644 index 0000000000..ed0b201ab1 --- /dev/null +++ b/UnitsNet.Tests/CustomCode/DynamicViscosityTests.cs @@ -0,0 +1,37 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/anjdreas/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 NUnit.Framework; + +namespace UnitsNet.Tests.CustomCode +{ + public class DynamicViscosityTests : DynamicViscosityTestsBase + { + protected override double CentipoiseInOneNewtonSecondPerMeterSquared => 1e3; + protected override double MillipascalSecondsInOneNewtonSecondPerMeterSquared => 1e3; + protected override double NewtonSecondsPerMeterSquaredInOneNewtonSecondPerMeterSquared => 1; + protected override double PascalSecondsInOneNewtonSecondPerMeterSquared => 1; + protected override double PoiseInOneNewtonSecondPerMeterSquared => 10; + + } +} diff --git a/UnitsNet.Tests/GeneratedCode/DynamicViscosityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/DynamicViscosityTestsBase.g.cs new file mode 100644 index 0000000000..88281b91b8 --- /dev/null +++ b/UnitsNet.Tests/GeneratedCode/DynamicViscosityTestsBase.g.cs @@ -0,0 +1,191 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/anjdreas/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 NUnit.Framework; +using UnitsNet.Units; + +// 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 DynamicViscosity. + /// + [TestFixture] +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class DynamicViscosityTestsBase + { + protected abstract double CentipoiseInOneNewtonSecondPerMeterSquared { get; } + protected abstract double MillipascalSecondsInOneNewtonSecondPerMeterSquared { get; } + protected abstract double NewtonSecondsPerMeterSquaredInOneNewtonSecondPerMeterSquared { get; } + protected abstract double PascalSecondsInOneNewtonSecondPerMeterSquared { get; } + protected abstract double PoiseInOneNewtonSecondPerMeterSquared { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double CentipoiseTolerance { get { return 1e-5; } } + protected virtual double MillipascalSecondsTolerance { get { return 1e-5; } } + protected virtual double NewtonSecondsPerMeterSquaredTolerance { get { return 1e-5; } } + protected virtual double PascalSecondsTolerance { get { return 1e-5; } } + protected virtual double PoiseTolerance { get { return 1e-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + [Test] + public void NewtonSecondPerMeterSquaredToDynamicViscosityUnits() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.AreEqual(CentipoiseInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.Centipoise, CentipoiseTolerance); + Assert.AreEqual(MillipascalSecondsInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.MillipascalSeconds, MillipascalSecondsTolerance); + Assert.AreEqual(NewtonSecondsPerMeterSquaredInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(PascalSecondsInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.PascalSeconds, PascalSecondsTolerance); + Assert.AreEqual(PoiseInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.Poise, PoiseTolerance); + } + + [Test] + public void FromValueAndUnit() + { + Assert.AreEqual(1, DynamicViscosity.From(1, DynamicViscosityUnit.Centipoise).Centipoise, CentipoiseTolerance); + Assert.AreEqual(1, DynamicViscosity.From(1, DynamicViscosityUnit.MillipascalSecond).MillipascalSeconds, MillipascalSecondsTolerance); + Assert.AreEqual(1, DynamicViscosity.From(1, DynamicViscosityUnit.NewtonSecondPerMeterSquared).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(1, DynamicViscosity.From(1, DynamicViscosityUnit.PascalSecond).PascalSeconds, PascalSecondsTolerance); + Assert.AreEqual(1, DynamicViscosity.From(1, DynamicViscosityUnit.Poise).Poise, PoiseTolerance); + } + + [Test] + public void As() + { + var newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.AreEqual(CentipoiseInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.As(DynamicViscosityUnit.Centipoise), CentipoiseTolerance); + Assert.AreEqual(MillipascalSecondsInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.As(DynamicViscosityUnit.MillipascalSecond), MillipascalSecondsTolerance); + Assert.AreEqual(NewtonSecondsPerMeterSquaredInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.As(DynamicViscosityUnit.NewtonSecondPerMeterSquared), NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(PascalSecondsInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.As(DynamicViscosityUnit.PascalSecond), PascalSecondsTolerance); + Assert.AreEqual(PoiseInOneNewtonSecondPerMeterSquared, newtonsecondpermetersquared.As(DynamicViscosityUnit.Poise), PoiseTolerance); + } + + [Test] + public void ConversionRoundTrip() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.AreEqual(1, DynamicViscosity.FromCentipoise(newtonsecondpermetersquared.Centipoise).NewtonSecondsPerMeterSquared, CentipoiseTolerance); + Assert.AreEqual(1, DynamicViscosity.FromMillipascalSeconds(newtonsecondpermetersquared.MillipascalSeconds).NewtonSecondsPerMeterSquared, MillipascalSecondsTolerance); + Assert.AreEqual(1, DynamicViscosity.FromNewtonSecondsPerMeterSquared(newtonsecondpermetersquared.NewtonSecondsPerMeterSquared).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(1, DynamicViscosity.FromPascalSeconds(newtonsecondpermetersquared.PascalSeconds).NewtonSecondsPerMeterSquared, PascalSecondsTolerance); + Assert.AreEqual(1, DynamicViscosity.FromPoise(newtonsecondpermetersquared.Poise).NewtonSecondsPerMeterSquared, PoiseTolerance); + } + + [Test] + public void ArithmeticOperators() + { + DynamicViscosity v = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.AreEqual(-1, -v.NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(2, (DynamicViscosity.FromNewtonSecondsPerMeterSquared(3)-v).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(2, (v + v).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(10, (v*10).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(10, (10*v).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(2, (DynamicViscosity.FromNewtonSecondsPerMeterSquared(10)/5).NewtonSecondsPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance); + Assert.AreEqual(2, DynamicViscosity.FromNewtonSecondsPerMeterSquared(10)/DynamicViscosity.FromNewtonSecondsPerMeterSquared(5), NewtonSecondsPerMeterSquaredTolerance); + } + + [Test] + public void ComparisonOperators() + { + DynamicViscosity oneNewtonSecondPerMeterSquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + DynamicViscosity twoNewtonSecondsPerMeterSquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(2); + + Assert.True(oneNewtonSecondPerMeterSquared < twoNewtonSecondsPerMeterSquared); + Assert.True(oneNewtonSecondPerMeterSquared <= twoNewtonSecondsPerMeterSquared); + Assert.True(twoNewtonSecondsPerMeterSquared > oneNewtonSecondPerMeterSquared); + Assert.True(twoNewtonSecondsPerMeterSquared >= oneNewtonSecondPerMeterSquared); + + Assert.False(oneNewtonSecondPerMeterSquared > twoNewtonSecondsPerMeterSquared); + Assert.False(oneNewtonSecondPerMeterSquared >= twoNewtonSecondsPerMeterSquared); + Assert.False(twoNewtonSecondsPerMeterSquared < oneNewtonSecondPerMeterSquared); + Assert.False(twoNewtonSecondsPerMeterSquared <= oneNewtonSecondPerMeterSquared); + } + + [Test] + public void CompareToIsImplemented() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.AreEqual(0, newtonsecondpermetersquared.CompareTo(newtonsecondpermetersquared)); + Assert.Greater(newtonsecondpermetersquared.CompareTo(DynamicViscosity.Zero), 0); + Assert.Less(DynamicViscosity.Zero.CompareTo(newtonsecondpermetersquared), 0); + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void CompareToThrowsOnTypeMismatch() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); +// ReSharper disable once ReturnValueOfPureMethodIsNotUsed + newtonsecondpermetersquared.CompareTo(new object()); + } + + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void CompareToThrowsOnNull() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); +// ReSharper disable once ReturnValueOfPureMethodIsNotUsed + newtonsecondpermetersquared.CompareTo(null); + } + + + [Test] + public void EqualityOperators() + { + DynamicViscosity a = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + DynamicViscosity b = DynamicViscosity.FromNewtonSecondsPerMeterSquared(2); + +// ReSharper disable EqualExpressionComparison + Assert.True(a == a); + Assert.True(a != b); + + Assert.False(a == b); + Assert.False(a != a); +// ReSharper restore EqualExpressionComparison + } + + [Test] + public void EqualsIsImplemented() + { + DynamicViscosity v = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.IsTrue(v.Equals(DynamicViscosity.FromNewtonSecondsPerMeterSquared(1))); + Assert.IsFalse(v.Equals(DynamicViscosity.Zero)); + } + + [Test] + public void EqualsReturnsFalseOnTypeMismatch() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.IsFalse(newtonsecondpermetersquared.Equals(new object())); + } + + [Test] + public void EqualsReturnsFalseOnNull() + { + DynamicViscosity newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1); + Assert.IsFalse(newtonsecondpermetersquared.Equals(null)); + } + } +} diff --git a/UnitsNet/GeneratedCode/Enums/DynamicViscosityUnit.g.cs b/UnitsNet/GeneratedCode/Enums/DynamicViscosityUnit.g.cs new file mode 100644 index 0000000000..32a98e44de --- /dev/null +++ b/UnitsNet/GeneratedCode/Enums/DynamicViscosityUnit.g.cs @@ -0,0 +1,34 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/anjdreas/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 DynamicViscosityUnit + { + Undefined = 0, + Centipoise, + MillipascalSecond, + NewtonSecondPerMeterSquared, + PascalSecond, + Poise, + } +} diff --git a/UnitsNet/GeneratedCode/UnitClasses/DynamicViscosity.g.cs b/UnitsNet/GeneratedCode/UnitClasses/DynamicViscosity.g.cs new file mode 100644 index 0000000000..e64dcbc49b --- /dev/null +++ b/UnitsNet/GeneratedCode/UnitClasses/DynamicViscosity.g.cs @@ -0,0 +1,493 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/anjdreas/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; + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// The dynamic (shear) viscosity of a fluid expresses its resistance to shearing flows, where adjacent layers move parallel to each other with different speeds + /// + // ReSharper disable once PartialTypeWithSinglePart + public partial struct DynamicViscosity : IComparable, IComparable + { + /// + /// Base unit of DynamicViscosity. + /// + private readonly double _newtonSecondsPerMeterSquared; + + public DynamicViscosity(double newtonsecondspermetersquared) : this() + { + _newtonSecondsPerMeterSquared = newtonsecondspermetersquared; + } + + #region Properties + + public static DynamicViscosityUnit BaseUnit + { + get { return DynamicViscosityUnit.NewtonSecondPerMeterSquared; } + } + + /// + /// Get DynamicViscosity in Centipoise. + /// + public double Centipoise + { + get { return (_newtonSecondsPerMeterSquared*10) / 1e-2d; } + } + + /// + /// Get DynamicViscosity in MillipascalSeconds. + /// + public double MillipascalSeconds + { + get { return (_newtonSecondsPerMeterSquared) / 1e-3d; } + } + + /// + /// Get DynamicViscosity in NewtonSecondsPerMeterSquared. + /// + public double NewtonSecondsPerMeterSquared + { + get { return _newtonSecondsPerMeterSquared; } + } + + /// + /// Get DynamicViscosity in PascalSeconds. + /// + public double PascalSeconds + { + get { return _newtonSecondsPerMeterSquared; } + } + + /// + /// Get DynamicViscosity in Poise. + /// + public double Poise + { + get { return _newtonSecondsPerMeterSquared*10; } + } + + #endregion + + #region Static + + public static DynamicViscosity Zero + { + get { return new DynamicViscosity(); } + } + + /// + /// Get DynamicViscosity from Centipoise. + /// + public static DynamicViscosity FromCentipoise(double centipoise) + { + return new DynamicViscosity((centipoise/10) * 1e-2d); + } + + /// + /// Get DynamicViscosity from MillipascalSeconds. + /// + public static DynamicViscosity FromMillipascalSeconds(double millipascalseconds) + { + return new DynamicViscosity((millipascalseconds) * 1e-3d); + } + + /// + /// Get DynamicViscosity from NewtonSecondsPerMeterSquared. + /// + public static DynamicViscosity FromNewtonSecondsPerMeterSquared(double newtonsecondspermetersquared) + { + return new DynamicViscosity(newtonsecondspermetersquared); + } + + /// + /// Get DynamicViscosity from PascalSeconds. + /// + public static DynamicViscosity FromPascalSeconds(double pascalseconds) + { + return new DynamicViscosity(pascalseconds); + } + + /// + /// Get DynamicViscosity from Poise. + /// + public static DynamicViscosity FromPoise(double poise) + { + return new DynamicViscosity(poise/10); + } + + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// DynamicViscosity unit value. + public static DynamicViscosity From(double value, DynamicViscosityUnit fromUnit) + { + switch (fromUnit) + { + case DynamicViscosityUnit.Centipoise: + return FromCentipoise(value); + case DynamicViscosityUnit.MillipascalSecond: + return FromMillipascalSeconds(value); + case DynamicViscosityUnit.NewtonSecondPerMeterSquared: + return FromNewtonSecondsPerMeterSquared(value); + case DynamicViscosityUnit.PascalSecond: + return FromPascalSeconds(value); + case DynamicViscosityUnit.Poise: + return FromPoise(value); + + default: + throw new NotImplementedException("fromUnit: " + fromUnit); + } + } + + /// + /// 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(DynamicViscosityUnit unit, CultureInfo culture = null) + { + return UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit); + } + + #endregion + + #region Arithmetic Operators + + public static DynamicViscosity operator -(DynamicViscosity right) + { + return new DynamicViscosity(-right._newtonSecondsPerMeterSquared); + } + + public static DynamicViscosity operator +(DynamicViscosity left, DynamicViscosity right) + { + return new DynamicViscosity(left._newtonSecondsPerMeterSquared + right._newtonSecondsPerMeterSquared); + } + + public static DynamicViscosity operator -(DynamicViscosity left, DynamicViscosity right) + { + return new DynamicViscosity(left._newtonSecondsPerMeterSquared - right._newtonSecondsPerMeterSquared); + } + + public static DynamicViscosity operator *(double left, DynamicViscosity right) + { + return new DynamicViscosity(left*right._newtonSecondsPerMeterSquared); + } + + public static DynamicViscosity operator *(DynamicViscosity left, double right) + { + return new DynamicViscosity(left._newtonSecondsPerMeterSquared*(double)right); + } + + public static DynamicViscosity operator /(DynamicViscosity left, double right) + { + return new DynamicViscosity(left._newtonSecondsPerMeterSquared/(double)right); + } + + public static double operator /(DynamicViscosity left, DynamicViscosity right) + { + return Convert.ToDouble(left._newtonSecondsPerMeterSquared/right._newtonSecondsPerMeterSquared); + } + + #endregion + + #region Equality / IComparable + + public int CompareTo(object obj) + { + if (obj == null) throw new ArgumentNullException("obj"); + if (!(obj is DynamicViscosity)) throw new ArgumentException("Expected type DynamicViscosity.", "obj"); + return CompareTo((DynamicViscosity) obj); + } + + public int CompareTo(DynamicViscosity other) + { + return _newtonSecondsPerMeterSquared.CompareTo(other._newtonSecondsPerMeterSquared); + } + + public static bool operator <=(DynamicViscosity left, DynamicViscosity right) + { + return left._newtonSecondsPerMeterSquared <= right._newtonSecondsPerMeterSquared; + } + + public static bool operator >=(DynamicViscosity left, DynamicViscosity right) + { + return left._newtonSecondsPerMeterSquared >= right._newtonSecondsPerMeterSquared; + } + + public static bool operator <(DynamicViscosity left, DynamicViscosity right) + { + return left._newtonSecondsPerMeterSquared < right._newtonSecondsPerMeterSquared; + } + + public static bool operator >(DynamicViscosity left, DynamicViscosity right) + { + return left._newtonSecondsPerMeterSquared > right._newtonSecondsPerMeterSquared; + } + + public static bool operator ==(DynamicViscosity left, DynamicViscosity right) + { + // ReSharper disable once CompareOfFloatsByEqualityOperator + return left._newtonSecondsPerMeterSquared == right._newtonSecondsPerMeterSquared; + } + + public static bool operator !=(DynamicViscosity left, DynamicViscosity right) + { + // ReSharper disable once CompareOfFloatsByEqualityOperator + return left._newtonSecondsPerMeterSquared != right._newtonSecondsPerMeterSquared; + } + + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + return _newtonSecondsPerMeterSquared.Equals(((DynamicViscosity) obj)._newtonSecondsPerMeterSquared); + } + + public override int GetHashCode() + { + return _newtonSecondsPerMeterSquared.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(DynamicViscosityUnit unit) + { + switch (unit) + { + case DynamicViscosityUnit.Centipoise: + return Centipoise; + case DynamicViscosityUnit.MillipascalSecond: + return MillipascalSeconds; + case DynamicViscosityUnit.NewtonSecondPerMeterSquared: + return NewtonSecondsPerMeterSquared; + case DynamicViscosityUnit.PascalSecond: + return PascalSeconds; + case DynamicViscosityUnit.Poise: + return Poise; + + 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} + /// 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 DynamicViscosity Parse(string str, IFormatProvider formatProvider = null) + { + if (str == null) throw new ArgumentNullException("str"); + + var numFormat = formatProvider != null ? + (NumberFormatInfo) formatProvider.GetFormat(typeof (NumberFormatInfo)) : + NumberFormatInfo.CurrentInfo; + + var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) + numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator + numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator + var exponentialRegex = @"(?:[eE][-+]?\d+)?)"; + var regexString = string.Format(@"(?:\s*(?[-+]?{0}{1}{2}{3})?{4}{5}", + numRegex, // capture base (integral) Quantity value + exponentialRegex, // capture exponential (if any), end of Quantity capturing + @"\s?", // ignore whitespace (allows both "1kg", "1 kg") + @"(?[^\s\d,]+)", // capture Unit (non-whitespace) input + @"(and)?,?", // allow "and" & "," separators between quantities + @"(?[a-z]*)?"); // capture invalid input + + var quantities = ParseWithRegex(regexString, str, formatProvider); + if (quantities.Count == 0) + { + throw new ArgumentException( + "Expected string to have at least one pair of quantity and unit in the format" + + " \"<quantity> <unit>\". Eg. \"5.5 m\" or \"1ft 2in\""); + } + return quantities.Aggregate((x, y) => x + y); + } + + /// + /// Parse a string given a particular regular expression. + /// + /// Error parsing string. + private static List ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) + { + var regex = new Regex(regexString); + MatchCollection matches = regex.Matches(str.Trim()); + var converted = new List(); + + foreach (Match match in matches) + { + GroupCollection groups = match.Groups; + + var valueString = groups["value"].Value; + var unitString = groups["unit"].Value; + if (groups["invalid"].Value != "") + { + var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + if (valueString == "" && unitString == "") continue; + + try + { + DynamicViscosityUnit unit = ParseUnit(unitString, formatProvider); + double value = double.Parse(valueString, formatProvider); + + converted.Add(From(value, unit)); + } + catch(AmbiguousUnitParseException ambiguousException) + { + throw; + } + catch(Exception ex) + { + var newEx = new UnitsNetException("Error parsing string.", ex); + newEx.Data["input"] = str; + newEx.Data["matched value"] = valueString; + newEx.Data["matched unit"] = unitString; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + } + return converted; + } + + /// + /// Parse a unit string. + /// + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static DynamicViscosityUnit 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 == DynamicViscosityUnit.Undefined) + { + var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DynamicViscosityUnit."); + newEx.Data["input"] = str; + newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); + throw newEx; + } + + return unit; + } + + #endregion + + /// + /// Set the default unit used by ToString(). Default is NewtonSecondPerMeterSquared + /// + public static DynamicViscosityUnit ToStringDefaultUnit { get; set; } = DynamicViscosityUnit.NewtonSecondPerMeterSquared; + + /// + /// Get default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString(ToStringDefaultUnit); + } + + /// + /// 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(DynamicViscosityUnit unit, CultureInfo culture = null, int significantDigitsAfterRadix = 2) + { + return ToString(unit, culture, UnitFormatter.GetFormat(As(unit), significantDigitsAfterRadix)); + } + + /// + /// 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(DynamicViscosityUnit unit, CultureInfo culture, string format, params object[] args) + { + return string.Format(culture, format, UnitFormatter.GetFormatArgs(unit, As(unit), culture, args)); + } + } +} diff --git a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs index 52c32909d3..93920aace2 100644 --- a/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs +++ b/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs @@ -337,6 +337,35 @@ private static readonly ReadOnlyCollection DefaultLocalization new AbbreviationsForCulture("ru-RU", "год"), }), }), + new UnitLocalization(typeof (DynamicViscosityUnit), + new[] + { + new CulturesForEnumValue((int) DynamicViscosityUnit.Centipoise, + new[] + { + new AbbreviationsForCulture("en-US", "cP"), + }), + new CulturesForEnumValue((int) DynamicViscosityUnit.MillipascalSecond, + new[] + { + new AbbreviationsForCulture("en-US", "mPaS"), + }), + new CulturesForEnumValue((int) DynamicViscosityUnit.NewtonSecondPerMeterSquared, + new[] + { + new AbbreviationsForCulture("en-US", "Ns/m²"), + }), + new CulturesForEnumValue((int) DynamicViscosityUnit.PascalSecond, + new[] + { + new AbbreviationsForCulture("en-US", "PaS"), + }), + new CulturesForEnumValue((int) DynamicViscosityUnit.Poise, + new[] + { + new AbbreviationsForCulture("en-US", "P"), + }), + }), new UnitLocalization(typeof (ElectricCurrentUnit), new[] { diff --git a/UnitsNet/Scripts/UnitDefinitions/DynamicViscosity.json b/UnitsNet/Scripts/UnitDefinitions/DynamicViscosity.json new file mode 100644 index 0000000000..6f3438b97b --- /dev/null +++ b/UnitsNet/Scripts/UnitDefinitions/DynamicViscosity.json @@ -0,0 +1,46 @@ +{ + "Name": "DynamicViscosity", + "BaseUnit": "NewtonSecondPerMeterSquared", + "XmlDoc": "The dynamic (shear) viscosity of a fluid expresses its resistance to shearing flows, where adjacent layers move parallel to each other with different speeds", + "XmlDocRemarks": "https://en.wikipedia.org/wiki/Viscosity#Dynamic_.28shear.29_viscosity", + "Units": [ + { + "SingularName": "NewtonSecondPerMeterSquared", + "PluralName": "NewtonSecondsPerMeterSquared", + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "Ns/m²" ] + } + ] + }, + { + "SingularName": "PascalSecond", + "PluralName": "PascalSeconds", + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Prefixes": [ "Milli" ], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "PaS" ] + } + ] + }, + { + "SingularName": "Poise", + "PluralName": "Poise", + "FromUnitToBaseFunc": "x/10", + "FromBaseToUnitFunc": "x*10", + "Prefixes": [ "Centi" ], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "P" ] + } + ] + } + ] +} \ No newline at end of file