From 7198845e6f775890ac6615fdd23533b3b19ba8a6 Mon Sep 17 00:00:00 2001 From: Branden Gunn Date: Sat, 25 May 2024 22:11:41 -0400 Subject: [PATCH] normalize all the things fix comparisons, formatting, naming, typos, etc. --- Source/Meadow.Units/AbsoluteHumidity.cs | 717 ++++++++------- Source/Meadow.Units/Acceleration.cs | 779 ++++++++-------- Source/Meadow.Units/Acceleration3D.cs | 413 +++++---- Source/Meadow.Units/Angle.cs | 129 ++- Source/Meadow.Units/AngularAcceleration.cs | 706 +++++++------- Source/Meadow.Units/AngularAcceleration3D.cs | 406 ++++---- Source/Meadow.Units/AngularVelocity.cs | 102 +- Source/Meadow.Units/AngularVelocity3D.cs | 404 ++++---- Source/Meadow.Units/ApparentPower.cs | 694 +++++++------- Source/Meadow.Units/Azimuth.cs | 745 ++++++++------- .../Azimuth16PointCardinalNames.cs | 139 ++- Source/Meadow.Units/Charge.cs | 607 ++++++------ Source/Meadow.Units/Concentration.cs | 671 +++++++------- Source/Meadow.Units/ConcentrationInWater.cs | 725 +++++++-------- Source/Meadow.Units/Conductivity.cs | 706 +++++++------- .../AbsoluteHumidityConversions.cs | 29 +- .../Conversions/AccelerationConversions.cs | 39 +- .../Conversions/AngleConversions.cs | 37 +- .../AngularAccelerationConversion.cs | 37 +- .../Conversions/AngularVelocityConversion.cs | 37 +- .../Conversions/ApparentPowerConversions.cs | 35 +- .../Conversions/AzimuthConversions.cs | 145 ++- .../Conversions/ConcentrationConversions.cs | 31 +- .../ConcentrationInWaterConversions.cs | 39 +- .../Conversions/ConductivityConversions.cs | 37 +- .../Conversions/CurrentConversions.cs | 37 +- .../Conversions/DensityConversions.cs | 47 +- .../Conversions/DigitalStorageConversions.cs | 51 +- .../Conversions/EnergyConversions.cs | 43 +- .../Conversions/FrequencyConversions.cs | 33 +- .../Conversions/IlluminanceConversions.cs | 31 +- .../Conversions/LengthConversions.cs | 49 +- .../Conversions/MagneticFieldConversions.cs | 41 +- .../Conversions/MassConversions.cs | 43 +- .../Conversions/ParticleDensityConversions.cs | 31 +- .../Conversions/PowerConversions.cs | 59 +- .../Conversions/PressureConversions.cs | 59 +- .../Conversions/ReactiveEnergyConversions.cs | 35 +- .../Conversions/ReactivePowerConversions.cs | 35 +- .../Conversions/ResistanceConversions.cs | 33 +- .../Conversions/SpeedConversions.cs | 51 +- .../Conversions/TempConversions.cs | 77 +- .../Conversions/TorqueConversions.cs | 41 +- .../Conversions/VoltageConversions.cs | 43 +- .../Conversions/VolumeConversions.cs | 41 +- Source/Meadow.Units/Current.cs | 26 +- Source/Meadow.Units/Density.cs | 750 +++++++-------- Source/Meadow.Units/DigitalStorage.cs | 822 ++++++++--------- Source/Meadow.Units/Energy.cs | 731 +++++++-------- Source/Meadow.Units/Frequency.cs | 743 ++++++++------- Source/Meadow.Units/GeoLocation.cs | 183 ++-- Source/Meadow.Units/Illuminance.cs | 737 ++++++++------- Source/Meadow.Units/Length.cs | 152 ++- Source/Meadow.Units/MagneticField.cs | 799 ++++++++-------- Source/Meadow.Units/MagneticField3D.cs | 406 ++++---- Source/Meadow.Units/Mass.cs | 813 ++++++++-------- Source/Meadow.Units/ParticleDensity.cs | 678 +++++++------- Source/Meadow.Units/PotentialHydrogen.cs | 676 +++++++------- Source/Meadow.Units/Power.cs | 177 ++-- Source/Meadow.Units/Pressure.cs | 784 ++++++++-------- Source/Meadow.Units/ReactiveEnergy.cs | 694 +++++++------- Source/Meadow.Units/ReactivePower.cs | 772 ++++++++-------- Source/Meadow.Units/RelativeHumidity.cs | 611 ++++++------ Source/Meadow.Units/Resistance.cs | 122 ++- Source/Meadow.Units/Speed.cs | 869 +++++++++--------- Source/Meadow.Units/Temperature.cs | 788 ++++++++-------- Source/Meadow.Units/Torque.cs | 806 ++++++++-------- Source/Meadow.Units/Turbidity.cs | 678 +++++++------- Source/Meadow.Units/Voltage.cs | 27 +- Source/Meadow.Units/Volume.cs | 806 ++++++++-------- 70 files changed, 11883 insertions(+), 12056 deletions(-) diff --git a/Source/Meadow.Units/AbsoluteHumidity.cs b/Source/Meadow.Units/AbsoluteHumidity.cs index 424357d..7dcc427 100644 --- a/Source/Meadow.Units/AbsoluteHumidity.cs +++ b/Source/Meadow.Units/AbsoluteHumidity.cs @@ -5,377 +5,376 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents AbsoluteHumidity +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct AbsoluteHumidity : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents AbsoluteHumidity + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct AbsoluteHumidity : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The AbsoluteHumidity value. + /// kilometers meters per second by default. + public AbsoluteHumidity(double value, UnitType type = UnitType.GramsPerCubicMeter) { - /// - /// Creates a new`AbsoluteHumidity` object. - /// - /// The AbsoluteHumidity value. - /// kilometers meters per second by default. - public AbsoluteHumidity(double value, UnitType type = UnitType.GramsPerCubicMeter) - { - Value = AbsoluteHumidityConversions.Convert(value, type, UnitType.GramsPerCubicMeter); - } + Value = AbsoluteHumidityConversions.Convert(value, type, UnitType.GramsPerCubicMeter); + } - /// - /// Creates a new AbsoluteHumidity object. - /// - /// - public AbsoluteHumidity(AbsoluteHumidity absoluteHumidity) - { - Value = absoluteHumidity.Value; - } + /// + /// Creates a new AbsoluteHumidity object. + /// + /// + public AbsoluteHumidity(AbsoluteHumidity absoluteHumidity) + { + Value = absoluteHumidity.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the AbsoluteHumidity. - /// - public enum UnitType - { - /// - /// Grams per cubic meter - /// - GramsPerCubicMeter, - /// - /// Kilograms per cubic meter - /// - KilogramsPerCubicMeter, - } + /// + /// The type of units available to describe the AbsoluteHumidity. + /// + public enum UnitType + { + /// Grams per cubic meter + GramsPerCubicMeter, + /// Kilograms per cubic meter + KilogramsPerCubicMeter, + } - /// - /// Get value of object in grams per cubic meter - /// - public double GramsPerCubicMeter => From(UnitType.GramsPerCubicMeter); - - /// - /// Get value of object in kilograms per cubic meter - /// - public double KilogramsPerCubicMeter => From(UnitType.KilogramsPerCubicMeter); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return AbsoluteHumidityConversions.Convert(Value, UnitType.GramsPerCubicMeter, convertTo); - } + /// + /// Get value of object in grams per cubic meter + /// + public double GramsPerCubicMeter => From(UnitType.GramsPerCubicMeter); - /// - /// Compare to another AbsoluteHumidity object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((AbsoluteHumidity)obj); - } + /// + /// Get value of object in kilograms per cubic meter + /// + public double KilogramsPerCubicMeter => From(UnitType.KilogramsPerCubicMeter); + + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return AbsoluteHumidityConversions.Convert(Value, UnitType.GramsPerCubicMeter, convertTo); + } + + /// + /// Compare to another AbsoluteHumidity object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator AbsoluteHumidity(ushort value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(short value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(uint value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(long value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(int value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(float value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(double value) => new AbsoluteHumidity(value); + //[Pure] public static implicit operator AbsoluteHumidity(decimal value) => new AbsoluteHumidity((double)value); + + // Comparison + /// + /// Compare to another AbsoluteHumidity object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(AbsoluteHumidity other) => Value == other.Value; + + /// + /// Equals operator to compare two AbsoluteHumidity objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(AbsoluteHumidity left, AbsoluteHumidity right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two AbsoluteHumidity objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(AbsoluteHumidity left, AbsoluteHumidity right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another AbsoluteHumidity object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(AbsoluteHumidity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two AbsoluteHumidity objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two AbsoluteHumidity objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two AbsoluteHumidity objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two AbsoluteHumidity objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two AbsoluteHumidity objects + /// + /// left value + /// right value + /// A new AbsoluteHumidity object with a value of left + right + [Pure] public static AbsoluteHumidity operator +(AbsoluteHumidity left, AbsoluteHumidity right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two AbsoluteHumidity objects + /// + /// left value + /// right value + /// A new AbsoluteHumidity object with a value of left - right + [Pure] public static AbsoluteHumidity operator -(AbsoluteHumidity left, AbsoluteHumidity right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new AbsoluteHumidity object with a value of value multiplied by the operand + [Pure] public static AbsoluteHumidity operator *(AbsoluteHumidity value, double operand) => new (value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new AbsoluteHumidity object with a value of value divided by the operand + [Pure] public static AbsoluteHumidity operator /(AbsoluteHumidity value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public AbsoluteHumidity Abs() { return new AbsoluteHumidity(Math.Abs(Value)); } - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator AbsoluteHumidity(ushort value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(short value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(uint value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(long value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(int value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(float value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(double value) => new AbsoluteHumidity(value); - //[Pure] public static implicit operator AbsoluteHumidity(decimal value) => new AbsoluteHumidity((double)value); - - // Comparison - /// - /// Compare to another AbsoluteHumidity object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(AbsoluteHumidity other) => Value == other.Value; - - /// - /// Equals operator to compare two AbsoluteHumidity objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(AbsoluteHumidity left, AbsoluteHumidity right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two AbsoluteHumidity objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(AbsoluteHumidity left, AbsoluteHumidity right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another AbsoluteHumidity object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(AbsoluteHumidity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two AbsoluteHumidity objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two AbsoluteHumidity objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two AbsoluteHumidity objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two AbsoluteHumidity objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(AbsoluteHumidity left, AbsoluteHumidity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two AbsoluteHumidity objects - /// - /// left value - /// right value - /// A new AbsoluteHumidity object with a value of left + right - [Pure] public static AbsoluteHumidity operator +(AbsoluteHumidity left, AbsoluteHumidity right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two AbsoluteHumidity objects - /// - /// left value - /// right value - /// A new AbsoluteHumidity object with a value of left - right - [Pure] public static AbsoluteHumidity operator -(AbsoluteHumidity left, AbsoluteHumidity right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new AbsoluteHumidity object with a value of value multiplied by the operand - [Pure] public static AbsoluteHumidity operator *(AbsoluteHumidity value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new AbsoluteHumidity object with a value of value divided by the operand - [Pure] public static AbsoluteHumidity operator /(AbsoluteHumidity value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public AbsoluteHumidity Abs() { return new AbsoluteHumidity(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another AbsoluteHumidity object - /// - /// The other AbsoluteHumidity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// conversion type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another AbsoluteHumidity object + /// + /// The other AbsoluteHumidity cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is AbsoluteHumidity absoluteHumidity) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(absoluteHumidity.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not an AbsoluteHumidity"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// conversion type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Acceleration.cs b/Source/Meadow.Units/Acceleration.cs index ad1de4f..6f979bb 100644 --- a/Source/Meadow.Units/Acceleration.cs +++ b/Source/Meadow.Units/Acceleration.cs @@ -5,412 +5,403 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Acceleration +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Acceleration : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Acceleration + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Acceleration : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Acceleration value. + /// kilometers meters per second by default. + public Acceleration(double value, UnitType type = UnitType.MetersPerSecondSquared) { - /// - /// Creates a new `Acceleration` object. - /// - /// The Acceleration value. - /// kilometers meters per second by default. - public Acceleration(double value, UnitType type = UnitType.MetersPerSecondSquared) - { - Value = AccelerationConversions.Convert(value, type, UnitType.MetersPerSecondSquared); - } + Value = AccelerationConversions.Convert(value, type, UnitType.MetersPerSecondSquared); + } - /// - /// Creates a new Acceleration object. - /// - /// - public Acceleration(Acceleration acceleration) - { - Value = acceleration.Value; - } + /// + /// Creates a new Acceleration object. + /// + /// + public Acceleration(Acceleration acceleration) + { + Value = acceleration.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Acceleration. - /// - public enum UnitType - { - /// - /// Meters per second squared - /// - MetersPerSecondSquared, - /// - /// Centimeters per second squared - /// - CentimetersPerSecondSquared, - /// - /// Galileo - /// - Galileo, - /// - /// MilliGalileo - /// - MilliGalileo, - /// - /// MilliGravity - /// - MilliGravity, - /// - /// Gravity - /// - Gravity, - /// - /// Feet per second squared - /// - FeetPerSecondSquared, - /// - /// Inches per second squared - /// - InchesPerSecondSquared, - } + /// + /// The type of units available to describe the Acceleration. + /// + public enum UnitType + { + /// Meters per second squared + MetersPerSecondSquared, + /// Centimeters per second squared + CentimetersPerSecondSquared, + /// Galileo + Galileo, + /// MilliGalileo + MilliGalileo, + /// MilliGravity + MilliGravity, + /// Gravity + Gravity, + /// Feet per second squared + FeetPerSecondSquared, + /// Inches per second squared + InchesPerSecondSquared, + } - /// - /// Get acceleration in meters per second squared - /// - public double MetersPerSecondSquared => From(UnitType.MetersPerSecondSquared); - /// - /// Get acceleration in centimeters per second squared - /// - public double CentimetersPerSecondSquared => From(UnitType.CentimetersPerSecondSquared); - /// - /// Get acceleration in g - /// - public double Gravity => From(UnitType.Gravity); - /// - /// Get acceleration in feet per second squared - /// - public double FeetPerSecondSquared => From(UnitType.FeetPerSecondSquared); - /// - /// Get acceleration in inches per second squared - /// - public double InchesPerSecondSquared => From(UnitType.InchesPerSecondSquared); - - /// - /// Get acceleration value for a given unit - /// - /// acceleration unit to convert to - /// - [Pure] public double From(UnitType convertTo) - { - return AccelerationConversions.Convert(Value, UnitType.MetersPerSecondSquared, convertTo); - } + /// + /// Get acceleration in meters per second squared + /// + public double MetersPerSecondSquared => From(UnitType.MetersPerSecondSquared); + + /// + /// Get acceleration in centimeters per second squared + /// + public double CentimetersPerSecondSquared => From(UnitType.CentimetersPerSecondSquared); + + /// + /// Get acceleration in g + /// + public double Gravity => From(UnitType.Gravity); + + /// + /// Get acceleration in feet per second squared + /// + public double FeetPerSecondSquared => From(UnitType.FeetPerSecondSquared); + + /// + /// Get acceleration in inches per second squared + /// + public double InchesPerSecondSquared => From(UnitType.InchesPerSecondSquared); - /// - /// Compare to another Acceleration object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Acceleration)obj); - } + /// + /// Get acceleration value for a given unit + /// + /// acceleration unit to convert to + /// + [Pure] public double From(UnitType convertTo) + { + return AccelerationConversions.Convert(Value, UnitType.MetersPerSecondSquared, convertTo); + } - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Acceleration(ushort value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(short value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(uint value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(long value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(int value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(float value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(double value) => new Acceleration(value); - //[Pure] public static implicit operator Acceleration(decimal value) => new Acceleration((double)value); - - // Comparison - /// - /// Compare to another Acceleration object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Acceleration other) => Value == other.Value; - - /// - /// Equals operator to compare two Acceleration objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Acceleration left, Acceleration right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Acceleration objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Acceleration left, Acceleration right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Acceleration object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Acceleration other) => Equals(this.Value, other.Value) ? 0 : this.Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Acceleration objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Acceleration objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Acceleration objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Acceleration objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Acceleration objects - /// - /// left value - /// right value - /// A new Acceleration object with a value of left + right - [Pure] public static Acceleration operator +(Acceleration left, Acceleration right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Acceleration objects - /// - /// left value - /// right value - /// A new Acceleration object with a value of left - right - [Pure] public static Acceleration operator -(Acceleration left, Acceleration right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Acceleration object with a value of value multiplied by the operand - [Pure] public static Acceleration operator *(Acceleration value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Acceleration object with a value of value divided by the operand - [Pure] public static Acceleration operator /(Acceleration value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Acceleration Abs() { return new Acceleration(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another AbsoluteHumidity object - /// - /// The other AbsoluteHumity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// conversion type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Compare to another Acceleration object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Acceleration(ushort value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(short value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(uint value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(long value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(int value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(float value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(double value) => new Acceleration(value); + //[Pure] public static implicit operator Acceleration(decimal value) => new Acceleration((double)value); + + // Comparison + /// + /// Compare to another Acceleration object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Acceleration other) => Value == other.Value; + + /// + /// Equals operator to compare two Acceleration objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Acceleration left, Acceleration right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Acceleration objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Acceleration left, Acceleration right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Acceleration object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Acceleration other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Acceleration objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Acceleration objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Acceleration objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Acceleration objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Acceleration left, Acceleration right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Acceleration objects + /// + /// left value + /// right value + /// A new Acceleration object with a value of left + right + [Pure] public static Acceleration operator +(Acceleration left, Acceleration right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Acceleration objects + /// + /// left value + /// right value + /// A new Acceleration object with a value of left - right + [Pure] public static Acceleration operator -(Acceleration left, Acceleration right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Acceleration object with a value of value multiplied by the operand + [Pure] public static Acceleration operator *(Acceleration value, double operand) => new (value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Acceleration object with a value of value divided by the operand + [Pure] public static Acceleration operator /(Acceleration value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Acceleration Abs() { return new Acceleration(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Acceleration object + /// + /// The other Acceleration cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Acceleration acceleration) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(acceleration.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not an Acceleration"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// conversion type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Acceleration3D.cs b/Source/Meadow.Units/Acceleration3D.cs index e04224c..ca2609f 100644 --- a/Source/Meadow.Units/Acceleration3D.cs +++ b/Source/Meadow.Units/Acceleration3D.cs @@ -4,226 +4,231 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Acceleration3D +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Acceleration3D : + IFormattable, IComparable, + IEquatable<(double X, double Y, double Z)>, + IComparable<(double, double, double)> { /// - /// Represents Acceleration3d + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Acceleration3D : - IFormattable, IComparable, - IEquatable<(double X, double Y, double Z)>, - IComparable<(double, double, double)> + /// The X Acceleration value. + /// The Y Acceleration value. + /// The Z Acceleration value. + /// Acceleration unit + public Acceleration3D(double x, double y, double z, + Acceleration.UnitType type = Acceleration.UnitType.MetersPerSecondSquared) { - /// - /// Creates a new `Acceleration3d` object. - /// - /// The X Acceleration3d value. - /// The Y Acceleration3d value. - /// The Z Acceleration3d value. - /// Acceleration unit - public Acceleration3D(double x, double y, double z, - Acceleration.UnitType type = Acceleration.UnitType.MetersPerSecondSquared) - { - X = new Acceleration(x, type); - Y = new Acceleration(y, type); - Z = new Acceleration(z, type); - } + X = new Acceleration(x, type); + Y = new Acceleration(y, type); + Z = new Acceleration(z, type); + } - /// - /// Creates a new `Acceleration3d` object. - /// - /// The X Acceleration value. - /// The Y Acceleration value. - /// The Z Acceleration value. - public Acceleration3D(Acceleration x, Acceleration y, Acceleration z) - { - X = new Acceleration(x); - Y = new Acceleration(y); - Z = new Acceleration(z); - } + /// + /// Creates a new object. + /// + /// The X Acceleration value. + /// The Y Acceleration value. + /// The Z Acceleration value. + public Acceleration3D(Acceleration x, Acceleration y, Acceleration z) + { + X = new Acceleration(x); + Y = new Acceleration(y); + Z = new Acceleration(z); + } - /// - /// Creates a new `Acceleration3d` object from an existing object. - /// - /// Source object - public Acceleration3D(Acceleration3D acceleration3D) - { - X = new Acceleration(acceleration3D.X); - Y = new Acceleration(acceleration3D.Y); - Z = new Acceleration(acceleration3D.Z); - } + /// + /// Creates a new object from an existing object. + /// + /// Source object + public Acceleration3D(Acceleration3D acceleration3D) + { + X = new Acceleration(acceleration3D.X); + Y = new Acceleration(acceleration3D.Y); + Z = new Acceleration(acceleration3D.Z); + } - /// - /// X component of acceleration - /// - public Acceleration X { get; set; } - /// - /// Y component of acceleration - /// - public Acceleration Y { get; set; } - /// - /// Z component of acceleration - /// - public Acceleration Z { get; set; } - - /// - /// Compare to another acceleration object - /// - /// - /// true if equals - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Acceleration3D)obj); - } + /// + /// X component of acceleration + /// + public Acceleration X { get; set; } - /// - /// Get hash object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; - - // Comparison - /// - /// Compare to another Acceleration object - /// - /// - /// true if equal - [Pure] public bool Equals(Acceleration3D other) => - X == other.X && - Y == other.Y && - Z == other.Z; - - /// - /// Equals operator to compare two Acceleration3D objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Acceleration3D left, Acceleration3D right) => Equals(left, right); - - /// - /// Not equals operator to compare two Acceleration3D objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Acceleration3D left, Acceleration3D right) => !Equals(left, right); - - /// - /// Less than operator to compare two Acceleration3D objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) < 0; - - /// - /// Greater than operator to compare two Acceleration3D objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) > 0; - - /// - /// Less than or equal operator to compare two Acceleration3D objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) <= 0; - - /// - /// Greater than or equal operator to compare two Acceleration3D objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) >= 0; - - // Math - /// - /// Addition operator to add two Acceleration3D objects - /// - /// left value - /// right value - /// A new Acceleration3D object with a value of left + right - [Pure] public static Acceleration3D operator +(Acceleration3D left, Acceleration3D right) - { - var x = left.X + right.X; - var y = left.Y + right.Y; - var z = left.Z + right.Z; + /// + /// Y component of acceleration + /// + public Acceleration Y { get; set; } + + /// + /// Z component of acceleration + /// + public Acceleration Z { get; set; } - return new Acceleration3D(x, y, z); - } + /// + /// Compare to another Acceleration3D object + /// + /// + /// true if equals + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + /// + /// Get hash object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; - /// - /// Subtraction operator to subtract two Acceleration3D objects - /// - /// left value - /// right value - /// A new Acceleration3D object with a value of left - right - [Pure] public static Acceleration3D operator -(Acceleration3D left, Acceleration3D right) - { - var x = left.X - right.X; - var y = left.Y - right.Y; - var z = left.Z - right.Z; + // Comparison + /// + /// Compare to another Acceleration object + /// + /// + /// true if equal + [Pure] public bool Equals(Acceleration3D other) => + X == other.X && + Y == other.Y && + Z == other.Z; - return new Acceleration3D(x, y, z); - } + /// + /// Equals operator to compare two Acceleration3D objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Acceleration3D left, Acceleration3D right) => Equals(left, right); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; - - // IComparable - /// - /// Compare to another Acceleration3D object - /// - /// - /// - /// - public int CompareTo(object obj) - { - throw new NotImplementedException(); - } + /// + /// Not equals operator to compare two Acceleration3D objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Acceleration3D left, Acceleration3D right) => !Equals(left, right); - /// - /// Compare the default value to three double - /// - /// value to compare - /// 0 if equal - public bool Equals((double X, double Y, double Z) other) - { - return X.Equals(other.X) && - Y.Equals(other.Y) && - Z.Equals(other.Z); - } + /// + /// Less than operator to compare two Acceleration3D objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) < 0; - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - public int CompareTo((double, double, double) other) + /// + /// Greater than operator to compare two Acceleration3D objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) > 0; + + /// + /// Less than or equal operator to compare two Acceleration3D objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) <= 0; + + /// + /// Greater than or equal operator to compare two Acceleration3D objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Acceleration3D left, Acceleration3D right) => Comparer.Default.Compare(left, right) >= 0; + + // Math + /// + /// Addition operator to add two Acceleration3D objects + /// + /// left value + /// right value + /// A new Acceleration3D object with a value of left + right + [Pure] public static Acceleration3D operator +(Acceleration3D left, Acceleration3D right) + { + var x = left.X + right.X; + var y = left.Y + right.Y; + var z = left.Z + right.Z; + + return new Acceleration3D(x, y, z); + } + + + /// + /// Subtraction operator to subtract two Acceleration3D objects + /// + /// left value + /// right value + /// A new Acceleration3D object with a value of left - right + [Pure] public static Acceleration3D operator -(Acceleration3D left, Acceleration3D right) + { + var x = left.X - right.X; + var y = left.Y - right.Y; + var z = left.Z - right.Z; + + return new Acceleration3D(x, y, z); + } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; + + // IComparable + /// + /// Compare to another Acceleration3D object + /// + /// + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Acceleration3D acceleration3D) { - throw new NotImplementedException(); + return X.CompareTo(acceleration3D.X) | + Y.CompareTo(acceleration3D.Y) | + Z.CompareTo(acceleration3D.X); } + + throw new ArgumentException("Object is not an Acceleration3D"); + } + + /// + /// Compare the default value to three doubles + /// + /// value to compare + /// 0 if equal + public bool Equals((double X, double Y, double Z) other) + { + return X.Equals(other.X) && + Y.Equals(other.Y) && + Z.Equals(other.Z); + } + + /// + /// Compare the default value to a tuple of three doubles + /// + /// value to compare + /// 0 if equal + public int CompareTo((double, double, double) other) + { + return X.CompareTo(other.Item1) | + Y.CompareTo(other.Item2) | + Z.CompareTo(other.Item3); } } \ No newline at end of file diff --git a/Source/Meadow.Units/Angle.cs b/Source/Meadow.Units/Angle.cs index b2680d2..ff45219 100644 --- a/Source/Meadow.Units/Angle.cs +++ b/Source/Meadow.Units/Angle.cs @@ -30,57 +30,45 @@ static Angle() public static Angle Zero => _zero; /// - /// Creates a new `Angle` object. + /// Creates a new object. /// /// The Angle value. /// Degrees by default. public Angle(double value, UnitType type = UnitType.Degrees) { - _value = AngleConversions.Convert(value, type, UnitType.Degrees); + Value = AngleConversions.Convert(value, type, UnitType.Degrees); } /// - /// Creates a new `Angle` object from an existing angle object + /// Creates a new object from an existing angle object /// /// public Angle(Angle angle) { - _value = angle._value; + Value = angle.Value; } /// /// Internal canonical value. /// - private readonly double _value; + private readonly double Value; /// /// The type of units available to describe the Angle. /// public enum UnitType { - /// - /// Revolutions - /// + /// Revolutions Revolutions, - /// - /// Degrees - /// + /// Degrees Degrees, - /// - /// Radians - /// + /// Radians Radians, - /// - /// Gradians - /// + /// Gradians Gradians, - /// - /// Minutes - /// + /// Minutes Minutes, - /// - /// Seconds - /// + /// Seconds Seconds } @@ -122,7 +110,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return AngleConversions.Convert(_value, UnitType.Degrees, convertTo); + return AngleConversions.Convert(Value, UnitType.Degrees, convertTo); } /// @@ -130,19 +118,13 @@ public double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Angle)obj); - } + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object /// /// int32 hash value - [Pure] public override int GetHashCode() => _value.GetHashCode(); + [Pure] public override int GetHashCode() => Value.GetHashCode(); // implicit conversions //[Pure] public static implicit operator Angle(ushort value) => new Angle(value); @@ -160,7 +142,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(Angle other) => _value == other._value; + [Pure] public bool Equals(Angle other) => Value == other.Value; /// /// Equals operator to compare two Angle objects @@ -168,7 +150,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(Angle left, Angle right) => Equals(left._value, right._value); + [Pure] public static bool operator ==(Angle left, Angle right) => Equals(left.Value, right.Value); /// /// Not equals operator to compare two Angle objects @@ -176,14 +158,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(Angle left, Angle right) => !Equals(left._value, right._value); + [Pure] public static bool operator !=(Angle left, Angle right) => !Equals(left.Value, right.Value); /// /// Compare to another Angle object /// /// /// 0 if equal - [Pure] public int CompareTo(Angle other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); + [Pure] public int CompareTo(Angle other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); /// /// Less than operator to compare two Angle objects @@ -191,7 +173,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(Angle left, Angle right) => Comparer.Default.Compare(left._value, right._value) < 0; + [Pure] public static bool operator <(Angle left, Angle right) => Comparer.Default.Compare(left.Value, right.Value) < 0; /// /// Greater than operator to compare two Angle objects @@ -199,7 +181,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(Angle left, Angle right) => Comparer.Default.Compare(left._value, right._value) > 0; + [Pure] public static bool operator >(Angle left, Angle right) => Comparer.Default.Compare(left.Value, right.Value) > 0; /// /// Less than or equal operator to compare two Angle objects @@ -207,7 +189,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(Angle left, Angle right) => Comparer.Default.Compare(left._value, right._value) <= 0; + [Pure] public static bool operator <=(Angle left, Angle right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; /// /// Greater than or equal operator to compare two Angle objects @@ -215,7 +197,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Angle left, Angle right) => Comparer.Default.Compare(left._value, right._value) >= 0; + [Pure] public static bool operator >=(Angle left, Angle right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; // Math /// @@ -224,7 +206,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Angle object with a value of left + right - [Pure] public static Angle operator +(Angle left, Angle right) => new(left._value + right._value); + [Pure] public static Angle operator +(Angle left, Angle right) => new(left.Value + right.Value); /// /// Subtraction operator to subtract two Angle objects @@ -232,7 +214,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Angle object with a value of left - right - [Pure] public static Angle operator -(Angle left, Angle right) => new(left._value - right._value); + [Pure] public static Angle operator -(Angle left, Angle right) => new(left.Value - right.Value); /// /// Multiplication operator to multiply by a double @@ -240,7 +222,7 @@ public override bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new Angle object with a value of value multiplied by the operand - [Pure] public static Angle operator *(Angle value, double operand) => new(value._value * operand); + [Pure] public static Angle operator *(Angle value, double operand) => new(value.Value * operand); /// /// Division operator to divide by a double @@ -248,19 +230,19 @@ public override bool Equals(object obj) /// object to be divided /// operand to divide object /// A new Angle object with a value of value divided by the operand - [Pure] public static Angle operator /(Angle value, double operand) => new(value._value / operand); + [Pure] public static Angle operator /(Angle value, double operand) => new(value.Value / operand); /// /// Returns the absolute value of the /// /// - [Pure] public Angle Abs() { return new Angle(Math.Abs(this._value)); } + [Pure] public Angle Abs() { return new Angle(Math.Abs(Value)); } /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => _value.ToString(); + [Pure] public override string ToString() => Value.ToString(); /// /// Get a string representation of the object @@ -268,7 +250,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => _value.ToString(format, formatProvider); + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); // IComparable /// @@ -276,97 +258,106 @@ public override bool Equals(object obj) /// /// The other Angle cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is Angle angle) + { + return Value.CompareTo(angle.Value); + } + + throw new ArgumentException("Object is not an Angle"); + } /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => _value; + [Pure] public double ToDouble(IFormatProvider provider) => Value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); /// /// Convert to type @@ -374,28 +365,28 @@ public override bool Equals(object obj) /// conversion type /// format provider /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); /// /// Convert to uint64 /// /// format provider /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); /// /// Compare the default value to a double @@ -405,7 +396,7 @@ public override bool Equals(object obj) [Pure] public int CompareTo(double? other) { - return (other is null) ? -1 : (_value).CompareTo(other.Value); + return (other is null) ? -1 : (Value).CompareTo(other.Value); } /// @@ -413,19 +404,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => _value.Equals(other); + [Pure] public bool Equals(double? other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double other) => _value.Equals(other); + [Pure] public bool Equals(double other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public int CompareTo(double other) => _value.CompareTo(other); + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/AngularAcceleration.cs b/Source/Meadow.Units/AngularAcceleration.cs index 709c1aa..530ece7 100644 --- a/Source/Meadow.Units/AngularAcceleration.cs +++ b/Source/Meadow.Units/AngularAcceleration.cs @@ -5,418 +5,410 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents AngularAcceleration +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct AngularAcceleration : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents AngularAcceleration + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct AngularAcceleration : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The AngularAcceleration value. + /// units of angular acceleration + public AngularAcceleration(double value, UnitType type = UnitType.RevolutionsPerSecondSquared) { - /// - /// Creates a new `AngularAcceleration` object. - /// - /// The AngularAcceleration value. - /// units of angular acceleration - public AngularAcceleration(double value, UnitType type = UnitType.RevolutionsPerSecondSquared) - { - Value = AngularAccelerationConversions.Convert(value, type, UnitType.RevolutionsPerSecondSquared); - } + Value = AngularAccelerationConversions.Convert(value, type, UnitType.RevolutionsPerSecondSquared); + } - /// - /// Creates a new `AngularAcceleration` object from an existing object - /// - /// - public AngularAcceleration(AngularAcceleration acceleration) - { - Value = acceleration.Value; - } + /// + /// Creates a new object from an existing object + /// + /// + public AngularAcceleration(AngularAcceleration acceleration) + { + Value = acceleration.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the AngularAcceleration. - /// - public enum UnitType - { - /// - /// Revolutions per second squared - /// - RevolutionsPerSecondSquared, - - /// - /// Revolutions per minute squared - /// - RevolutionsPerMinuteSquared, - - /// - /// Radians per second squared - /// - RadiansPerSecondSquared, - - /// - /// Radians per minutes squared - /// - RadiansPerMinuteSquared, - - /// - /// Degrees per second squared - /// - DegreesPerSecondSquared, - - /// - /// Degrees per minute squared - /// - DegreesPerMinuteSquared - } + /// + /// The type of units available to describe the AngularAcceleration. + /// + public enum UnitType + { + /// Revolutions per second squared + RevolutionsPerSecondSquared, + /// Revolutions per minute squared + RevolutionsPerMinuteSquared, + /// Radians per second squared + RadiansPerSecondSquared, + /// Radians per minutes squared + RadiansPerMinuteSquared, /// - /// Get angular acceleration in revolutions per second squared + /// Degrees per second squared /// - public double RevolutionsPerSecondSquared => From(UnitType.RevolutionsPerSecondSquared); + DegreesPerSecondSquared, /// - /// Get angular acceleration in revolutions per minutes squared + /// Degrees per minute squared /// - public double RevolutionsPerMinuteSquared => From(UnitType.RevolutionsPerMinuteSquared); + DegreesPerMinuteSquared + } - /// - /// Get angular acceleration in radians per second squared - /// - public double RadiansPerSecondSquared => From(UnitType.RadiansPerSecondSquared); + /// + /// Get angular acceleration in revolutions per second squared + /// + public double RevolutionsPerSecondSquared => From(UnitType.RevolutionsPerSecondSquared); - /// - /// Get angular acceleration in radians per minute squared - /// - public double RadiansPerMinuteSquared => From(UnitType.RadiansPerMinuteSquared); + /// + /// Get angular acceleration in revolutions per minutes squared + /// + public double RevolutionsPerMinuteSquared => From(UnitType.RevolutionsPerMinuteSquared); - /// - /// Get angular acceleration in degrees per second squared - /// - public double DegreesPerSecondSquared => From(UnitType.DegreesPerSecondSquared); + /// + /// Get angular acceleration in radians per second squared + /// + public double RadiansPerSecondSquared => From(UnitType.RadiansPerSecondSquared); - /// - /// Get angular acceleration in degrees per minute squared - /// - public double DegreesPerMinuteSquared => From(UnitType.DegreesPerMinuteSquared); + /// + /// Get angular acceleration in radians per minute squared + /// + public double RadiansPerMinuteSquared => From(UnitType.RadiansPerMinuteSquared); - /// - /// Get angular acceleration for a specific unit - /// - /// - /// angular acceleration value - [Pure] public double From(UnitType convertTo) - { - return AngularAccelerationConversions.Convert(Value, UnitType.RevolutionsPerSecondSquared, convertTo); - } + /// + /// Get angular acceleration in degrees per second squared + /// + public double DegreesPerSecondSquared => From(UnitType.DegreesPerSecondSquared); - /// - /// Compare to another AngularAcceleration object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((AngularAcceleration)obj); - } + /// + /// Get angular acceleration in degrees per minute squared + /// + public double DegreesPerMinuteSquared => From(UnitType.DegreesPerMinuteSquared); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator AngularAcceleration(ushort value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(short value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(uint value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(long value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(int value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(float value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(double value) => new AngularAcceleration(value); - //[Pure] public static implicit operator AngularAcceleration(decimal value) => new AngularAcceleration((double)value); - - // Comparison - /// - /// Compare to another AngularAcceleration object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(AngularAcceleration other) => Value == other.Value; + /// + /// Get angular acceleration for a specific unit + /// + /// + /// angular acceleration value + [Pure] public double From(UnitType convertTo) + { + return AngularAccelerationConversions.Convert(Value, UnitType.RevolutionsPerSecondSquared, convertTo); + } - /// - /// Equals operator to compare two AngularAcceleration objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(AngularAcceleration left, AngularAcceleration right) => Equals(left.Value, right.Value); + /// + /// Compare to another AngularAcceleration object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Not equals operator to compare two AngularAcceleration objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(AngularAcceleration left, AngularAcceleration right) => !Equals(left.Value, right.Value); + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator AngularAcceleration(ushort value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(short value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(uint value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(long value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(int value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(float value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(double value) => new AngularAcceleration(value); + //[Pure] public static implicit operator AngularAcceleration(decimal value) => new AngularAcceleration((double)value); + + // Comparison + /// + /// Compare to another AngularAcceleration object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(AngularAcceleration other) => Value == other.Value; - /// - /// Compare to another AngularAcceleration object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(AngularAcceleration other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Equals operator to compare two AngularAcceleration objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(AngularAcceleration left, AngularAcceleration right) => Equals(left.Value, right.Value); - /// - /// Less than operator to compare two AngularAcceleration objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Not equals operator to compare two AngularAcceleration objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(AngularAcceleration left, AngularAcceleration right) => !Equals(left.Value, right.Value); - /// - /// Greater than operator to compare two AngularAcceleration objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Compare to another AngularAcceleration object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(AngularAcceleration other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Less than or equal operator to compare two AngularAcceleration objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Less than operator to compare two AngularAcceleration objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Greater than or equal operator to compare two AngularAcceleration objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Greater than operator to compare two AngularAcceleration objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - // Math - /// - /// Addition operator to add two AngularAcceleration objects - /// - /// left value - /// right value - /// A new AngularAcceleration object with a value of left + right - [Pure] public static AngularAcceleration operator +(AngularAcceleration left, AngularAcceleration right) => new (left.Value + right.Value); + /// + /// Less than or equal operator to compare two AngularAcceleration objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Subtraction operator to subtract two AngularAcceleration objects - /// - /// left value - /// right value - /// A new AngularAcceleration object with a value of left - right - [Pure] public static AngularAcceleration operator -(AngularAcceleration left, AngularAcceleration right) => new (left.Value - right.Value); + /// + /// Greater than or equal operator to compare two AngularAcceleration objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(AngularAcceleration left, AngularAcceleration right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new AngularAcceleration object with a value of value multiplied by the operand - [Pure] public static AngularAcceleration operator *(AngularAcceleration value, double operand) => new (value.Value * operand); + // Math + /// + /// Addition operator to add two AngularAcceleration objects + /// + /// left value + /// right value + /// A new AngularAcceleration object with a value of left + right + [Pure] public static AngularAcceleration operator +(AngularAcceleration left, AngularAcceleration right) => new (left.Value + right.Value); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new AngularAcceleration object with a value of value divided by the operand - [Pure] public static AngularAcceleration operator /(AngularAcceleration value, double operand) => new (value.Value / operand); + /// + /// Subtraction operator to subtract two AngularAcceleration objects + /// + /// left value + /// right value + /// A new AngularAcceleration object with a value of left - right + [Pure] public static AngularAcceleration operator -(AngularAcceleration left, AngularAcceleration right) => new (left.Value - right.Value); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public AngularAcceleration Abs() { return new AngularAcceleration(Math.Abs(this.Value)); } + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new AngularAcceleration object with a value of value multiplied by the operand + [Pure] public static AngularAcceleration operator *(AngularAcceleration value, double operand) => new (value.Value * operand); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new AngularAcceleration object with a value of value divided by the operand + [Pure] public static AngularAcceleration operator /(AngularAcceleration value, double operand) => new (value.Value / operand); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public AngularAcceleration Abs() { return new AngularAcceleration(Math.Abs(Value)); } - // IComparable - /// - /// Compare to another AngularAcceleration object - /// - /// The other AngularAcceleration cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + // IComparable + /// + /// Compare to another AngularAcceleration object + /// + /// The other AngularAcceleration cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is AngularAcceleration angularAcceleration) + { + return Value.CompareTo(angularAcceleration.Value); + } - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + throw new ArgumentException("Object is not an AngularAcceleration"); + } - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to type - /// - /// conversion type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to type + /// + /// conversion type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/AngularAcceleration3D.cs b/Source/Meadow.Units/AngularAcceleration3D.cs index 7a45ab9..e82235f 100644 --- a/Source/Meadow.Units/AngularAcceleration3D.cs +++ b/Source/Meadow.Units/AngularAcceleration3D.cs @@ -4,225 +4,225 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents AngularAcceleration3D +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct AngularAcceleration3D : + IFormattable, IComparable, + IEquatable<(double ValueX, double ValueY, double ValueZ)>, + IComparable<(double, double, double)> { /// - /// Represents AngularAcceleration3d + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct AngularAcceleration3D : - IFormattable, IComparable, - IEquatable<(double ValueX, double ValueY, double ValueZ)>, - IComparable<(double, double, double)> + /// The X AngularAcceleration value. + /// The Y AngularAcceleration value. + /// The Z AngularAcceleration value. + /// units of angular acceleration + public AngularAcceleration3D(double valueX, double valueY, double valueZ, + AngularAcceleration.UnitType type = AngularAcceleration.UnitType.RadiansPerSecondSquared) { - /// - /// Creates a new `AngularAcceleration3d` object. - /// - /// The X AngularAcceleration3d value. - /// The Y AngularAcceleration3d value. - /// The Z AngularAcceleration3d value. - /// units of angular acceleration - public AngularAcceleration3D(double valueX, double valueY, double valueZ, - AngularAcceleration.UnitType type = AngularAcceleration.UnitType.RadiansPerSecondSquared) - { - X = new AngularAcceleration(valueX, type); - Y = new AngularAcceleration(valueY, type); - Z = new AngularAcceleration(valueZ, type); - } + X = new AngularAcceleration(valueX, type); + Y = new AngularAcceleration(valueY, type); + Z = new AngularAcceleration(valueZ, type); + } - /// - /// Creates a new `AngularAcceleration3d` object. - /// - /// The X AngularAcceleration value. - /// The Y AngularAcceleration value. - /// The Z AngularAcceleration value. - public AngularAcceleration3D(AngularAcceleration x, AngularAcceleration y, AngularAcceleration z) - { - X = new AngularAcceleration(x); - Y = new AngularAcceleration(y); - Z = new AngularAcceleration(z); - } + /// + /// Creates a new object. + /// + /// The X AngularAcceleration value. + /// The Y AngularAcceleration value. + /// The Z AngularAcceleration value. + public AngularAcceleration3D(AngularAcceleration x, AngularAcceleration y, AngularAcceleration z) + { + X = new AngularAcceleration(x); + Y = new AngularAcceleration(y); + Z = new AngularAcceleration(z); + } - /// - /// Creates a new `AngularAcceleration3D` object from an existing object. - /// - /// Source object - public AngularAcceleration3D(AngularAcceleration3D angularAcceleration3D) - { - this.X = new AngularAcceleration(angularAcceleration3D.X); - this.Y = new AngularAcceleration(angularAcceleration3D.Y); - this.Z = new AngularAcceleration(angularAcceleration3D.Z); - } + /// + /// Creates a new object from an existing object. + /// + /// Source object + public AngularAcceleration3D(AngularAcceleration3D angularAcceleration3D) + { + X = new AngularAcceleration(angularAcceleration3D.X); + Y = new AngularAcceleration(angularAcceleration3D.Y); + Z = new AngularAcceleration(angularAcceleration3D.Z); + } - /// - /// X component of angular acceleration - /// - public AngularAcceleration X { get; set; } - /// - /// Y component of angular acceleration - /// - public AngularAcceleration Y { get; set; } - /// - /// Z component of angular acceleration - /// - public AngularAcceleration Z { get; set; } - - /// - /// Compare to another AngularAcceleration3D object - /// - /// object to compare to - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((AngularAcceleration3D)obj); - } + /// + /// X component of angular acceleration + /// + public AngularAcceleration X { get; set; } + /// + /// Y component of angular acceleration + /// + public AngularAcceleration Y { get; set; } + /// + /// Z component of angular acceleration + /// + public AngularAcceleration Z { get; set; } + + /// + /// Compare to another AngularAcceleration3D object + /// + /// object to compare to + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; + + // Comparison + /// + /// Compare to another AngularAcceleration object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(AngularAcceleration3D other) => + X == other.X && + Y == other.Y && + Z == other.Z; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; - - // Comparison - /// - /// Compare to another AngularAcceleration object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(AngularAcceleration3D other) => - X == other.X && - Y == other.Y && - Z == other.Z; - - /// - /// Equals operator to compare two AngularAcceleration3D objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(AngularAcceleration3D left, AngularAcceleration3D right) => Equals(left, right); - - /// - /// Not equals operator to compare two AngularAcceleration3D objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(AngularAcceleration3D left, AngularAcceleration3D right) => !Equals(left, right); + /// + /// Equals operator to compare two AngularAcceleration3D objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(AngularAcceleration3D left, AngularAcceleration3D right) => Equals(left, right); + + /// + /// Not equals operator to compare two AngularAcceleration3D objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(AngularAcceleration3D left, AngularAcceleration3D right) => !Equals(left, right); - /// - /// Less than operator to compare two AngularAcceleration3D objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) < 0; - - /// - /// Greater than operator to compare two AngularAcceleration3D objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) > 0; - - /// - /// Less than or equal operator to compare two AngularAcceleration3D objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) <= 0; - - /// - /// Greater than or equal operator to compare two AngularAcceleration3D objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) >= 0; - - // Math - /// - /// Addition operator to add two AngularAcceleration objects - /// - /// left value - /// right value - /// A new AngularAcceleration3D object with a value of left + right - [Pure] public static AngularAcceleration3D operator +(AngularAcceleration3D left, AngularAcceleration3D right) - { - var x = left.X + right.X; - var y = left.Y + right.Y; - var z = left.Z + right.Z; + /// + /// Less than operator to compare two AngularAcceleration3D objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) < 0; - return new AngularAcceleration3D(x, y, z); - } + /// + /// Greater than operator to compare two AngularAcceleration3D objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) > 0; - /// - /// Subtraction operator to subtract two AngularAcceleration3D objects - /// - /// left value - /// right value - /// A new AngularAcceleration3D object with a value of left - right - [Pure] public static AngularAcceleration3D operator -(AngularAcceleration3D left, AngularAcceleration3D right) - { - var x = left.X - right.X; - var y = left.Y - right.Y; - var z = left.Z - right.Z; + /// + /// Less than or equal operator to compare two AngularAcceleration3D objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) <= 0; - return new AngularAcceleration3D(x, y, z); - } + /// + /// Greater than or equal operator to compare two AngularAcceleration3D objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(AngularAcceleration3D left, AngularAcceleration3D right) => Comparer.Default.Compare(left, right) >= 0; - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; - - // IComparable - /// - /// Compare to another AngularAcceleration3D object - /// - /// The other AngularAcceleration3D cast to object - /// - /// - public int CompareTo(object obj) - { - throw new NotImplementedException(); - } + // Math + /// + /// Addition operator to add two AngularAcceleration objects + /// + /// left value + /// right value + /// A new AngularAcceleration3D object with a value of left + right + [Pure] public static AngularAcceleration3D operator +(AngularAcceleration3D left, AngularAcceleration3D right) + { + var x = left.X + right.X; + var y = left.Y + right.Y; + var z = left.Z + right.Z; - /// - /// Compare the default value to three double - /// - /// values to compare - /// true if equal - public bool Equals((double ValueX, double ValueY, double ValueZ) other) - { - return X.Equals(other.ValueX) && - Y.Equals(other.ValueY) && - Z.Equals(other.ValueZ); - } + return new AngularAcceleration3D(x, y, z); + } + + /// + /// Subtraction operator to subtract two AngularAcceleration3D objects + /// + /// left value + /// right value + /// A new AngularAcceleration3D object with a value of left - right + [Pure] public static AngularAcceleration3D operator -(AngularAcceleration3D left, AngularAcceleration3D right) + { + var x = left.X - right.X; + var y = left.Y - right.Y; + var z = left.Z - right.Z; + + return new AngularAcceleration3D(x, y, z); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - public int CompareTo((double, double, double) other) + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; + + // IComparable + /// + /// Compare to another AngularAcceleration3D object + /// + /// The other AngularAcceleration3D cast to object + /// 0 if equal + public int CompareTo(object obj) + { + if (obj is AngularAcceleration3D angularAcceleration3D) { - throw new NotImplementedException(); + return X.CompareTo(angularAcceleration3D.X) | Y.CompareTo(angularAcceleration3D.Y) | Z.CompareTo(angularAcceleration3D.Z); } + + throw new ArgumentException("Object is not an AngularAcceleration3D"); + } + + /// + /// Compare the default value to three doubles + /// + /// values to compare + /// true if equal + public bool Equals((double ValueX, double ValueY, double ValueZ) other) + { + return X.Equals(other.ValueX) && + Y.Equals(other.ValueY) && + Z.Equals(other.ValueZ); + } + + /// + /// Compare the default value to a tuple of three doubles + /// + /// value to compare + /// 0 if equal + public int CompareTo((double, double, double) other) + { + return X.CompareTo(other.Item1) | + Y.CompareTo(other.Item2) | + Z.CompareTo(other.Item3); } } \ No newline at end of file diff --git a/Source/Meadow.Units/AngularVelocity.cs b/Source/Meadow.Units/AngularVelocity.cs index 9c51ea9..8a0100f 100644 --- a/Source/Meadow.Units/AngularVelocity.cs +++ b/Source/Meadow.Units/AngularVelocity.cs @@ -30,13 +30,13 @@ static AngularVelocity() public static AngularVelocity Zero => _zero; /// - /// Creates a new `AngularVelocity` object. + /// Creates a new object. /// /// The AngularVelocity value. /// kilometers meters per second by default. public AngularVelocity(double value, UnitType type = UnitType.RevolutionsPerSecond) { - _value = AngularVelocityConversions.Convert(value, type, UnitType.RevolutionsPerSecond); + Value = AngularVelocityConversions.Convert(value, type, UnitType.RevolutionsPerSecond); } /// @@ -45,13 +45,13 @@ public AngularVelocity(double value, UnitType type = UnitType.RevolutionsPerSeco /// public AngularVelocity(AngularVelocity angularVelocity) { - _value = angularVelocity._value; + Value = angularVelocity.Value; } /// /// Internal canonical value. /// - private readonly double _value; + private readonly double Value; /// /// The type of units available to describe the AngularVelocity. @@ -122,7 +122,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return AngularVelocityConversions.Convert(_value, UnitType.RevolutionsPerSecond, convertTo); + return AngularVelocityConversions.Convert(Value, UnitType.RevolutionsPerSecond, convertTo); } /// @@ -131,18 +131,13 @@ public double From(UnitType convertTo) /// The object to compare /// true if equal [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((AngularVelocity)obj); - } + public override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object /// /// int32 hash value - [Pure] public override int GetHashCode() => _value.GetHashCode(); + [Pure] public override int GetHashCode() => Value.GetHashCode(); // implicit conversions //[Pure] public static implicit operator AngularVelocity(ushort value) => new AngularVelocity(value); @@ -160,7 +155,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(AngularVelocity other) => _value == other._value; + [Pure] public bool Equals(AngularVelocity other) => Value == other.Value; /// /// Equals operator to compare two AngularVelocity objects @@ -168,7 +163,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(AngularVelocity left, AngularVelocity right) => Equals(left._value, right._value); + [Pure] public static bool operator ==(AngularVelocity left, AngularVelocity right) => Equals(left.Value, right.Value); /// /// Not equals operator to compare two AngularVelocity objects @@ -176,14 +171,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(AngularVelocity left, AngularVelocity right) => !Equals(left._value, right._value); + [Pure] public static bool operator !=(AngularVelocity left, AngularVelocity right) => !Equals(left.Value, right.Value); /// /// Compare to another AngularVelocity object /// /// /// 0 if equal - [Pure] public int CompareTo(AngularVelocity other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); + [Pure] public int CompareTo(AngularVelocity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); /// /// Less than operator to compare two AngularVelocity objects @@ -191,7 +186,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) < 0; + [Pure] public static bool operator <(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; /// /// Greater than operator to compare two AngularVelocity objects @@ -199,7 +194,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) > 0; + [Pure] public static bool operator >(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; /// /// Less than or equal operator to compare two AngularVelocity objects @@ -207,7 +202,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) <= 0; + [Pure] public static bool operator <=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; /// /// Greater than or equal operator to compare two AngularVelocity objects @@ -215,7 +210,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left._value, right._value) >= 0; + [Pure] public static bool operator >=(AngularVelocity left, AngularVelocity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; // Math /// @@ -224,7 +219,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new AngularVelocity object with a value of left + right - [Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new(left._value + right._value); + [Pure] public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right) => new(left.Value + right.Value); /// /// Subtraction operator to subtract two AngularVelocity objects @@ -232,7 +227,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new AngularVelocity object with a value of left - right - [Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right) => new(left._value - right._value); + [Pure] public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right) => new(left.Value - right.Value); /// /// Multiplication operator to multiply by a double @@ -240,7 +235,7 @@ public override bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new AngularVelocity object with a value of value multiplied by the operand - [Pure] public static AngularVelocity operator *(AngularVelocity value, double operand) => new(value._value * operand); + [Pure] public static AngularVelocity operator *(AngularVelocity value, double operand) => new(value.Value * operand); /// /// Division operator to divide by a double @@ -248,19 +243,19 @@ public override bool Equals(object obj) /// object to be divided /// operand to divide object /// A new AngularVelocity object with a value of value divided by the operand - [Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new(value._value / operand); + [Pure] public static AngularVelocity operator /(AngularVelocity value, double operand) => new(value.Value / operand); /// /// Returns the absolute value of the /// /// - [Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(this._value)); } + [Pure] public AngularVelocity Abs() { return new AngularVelocity(Math.Abs(Value)); } /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => _value.ToString(); + [Pure] public override string ToString() => Value.ToString(); /// /// Get a string representation of the object @@ -268,7 +263,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => _value.ToString(format, formatProvider); + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); // IComparable /// @@ -276,97 +271,106 @@ public override bool Equals(object obj) /// /// The other AngularVelocity cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is AngularVelocity angularVelocity) + { + return Value.CompareTo(angularVelocity.Value); + } + + throw new ArgumentException("Object is not an AngularVelocity"); + } /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => _value; + [Pure] public double ToDouble(IFormatProvider provider) => Value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); /// /// Convert to type @@ -374,28 +378,28 @@ public override bool Equals(object obj) /// conversion type to convert to /// format provider /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); /// /// Convert to uint64 /// /// format provider /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); /// /// Compare the default value to a double @@ -405,7 +409,7 @@ public override bool Equals(object obj) [Pure] public int CompareTo(double? other) { - return (other is null) ? -1 : (_value).CompareTo(other.Value); + return (other is null) ? -1 : (Value).CompareTo(other.Value); } /// @@ -413,19 +417,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => _value.Equals(other); + [Pure] public bool Equals(double? other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double other) => _value.Equals(other); + [Pure] public bool Equals(double other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public int CompareTo(double other) => _value.CompareTo(other); + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/AngularVelocity3D.cs b/Source/Meadow.Units/AngularVelocity3D.cs index f3dfab8..2336c16 100644 --- a/Source/Meadow.Units/AngularVelocity3D.cs +++ b/Source/Meadow.Units/AngularVelocity3D.cs @@ -4,224 +4,226 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents AngularVelocity3D +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct AngularVelocity3D : + IFormattable, IComparable, + IEquatable<(double ValueX, double ValueY, double ValueZ)>, + IComparable<(double, double, double)> { /// - /// Represents AngularVelocity3d + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct AngularVelocity3D : - IFormattable, IComparable, - IEquatable<(double ValueX, double ValueY, double ValueZ)>, - IComparable<(double, double, double)> + /// The X AngularVelocity value. + /// The Y AngularVelocity value. + /// The Z AngularVelocity value. + /// + public AngularVelocity3D(double valueX, double valueY, double valueZ, + AngularVelocity.UnitType type = AngularVelocity.UnitType.RevolutionsPerSecond) { - /// - /// Creates a new `AngularVelocity3d` object. - /// - /// The X AngularVelocity value. - /// The Y AngularVelocity value. - /// The Z AngularVelocity value. - /// - public AngularVelocity3D(double valueX, double valueY, double valueZ, - AngularVelocity.UnitType type = AngularVelocity.UnitType.RevolutionsPerSecond) - { - X = new AngularVelocity(valueX, type); - Y = new AngularVelocity(valueY, type); - Z = new AngularVelocity(valueZ, type); - } + X = new AngularVelocity(valueX, type); + Y = new AngularVelocity(valueY, type); + Z = new AngularVelocity(valueZ, type); + } - /// - /// Creates a new `AngularVelocity3d` object. - /// - /// The X AngularVelocity component. - /// The Y AngularVelocity component. - /// The Z AngularVelocity component. - public AngularVelocity3D(AngularVelocity x, AngularVelocity y, AngularVelocity z) - { - X = new AngularVelocity(x); - Y = new AngularVelocity(y); - Z = new AngularVelocity(z); - } + /// + /// Creates a new object. + /// + /// The X AngularVelocity component. + /// The Y AngularVelocity component. + /// The Z AngularVelocity component. + public AngularVelocity3D(AngularVelocity x, AngularVelocity y, AngularVelocity z) + { + X = new AngularVelocity(x); + Y = new AngularVelocity(y); + Z = new AngularVelocity(z); + } - /// - /// Creates a new `AngularVelocity3d` object with values from another object - /// - /// angularVelocity3D source object. - public AngularVelocity3D(AngularVelocity3D angularVelocity3D) - { - this.X = new AngularVelocity(angularVelocity3D.X); - this.Y = new AngularVelocity(angularVelocity3D.Y); - this.Z = new AngularVelocity(angularVelocity3D.Z); - } + /// + /// Creates a new AngularVelocity3D object with values from another object + /// + /// angularVelocity3D source object. + public AngularVelocity3D(AngularVelocity3D angularVelocity3D) + { + X = new AngularVelocity(angularVelocity3D.X); + Y = new AngularVelocity(angularVelocity3D.Y); + Z = new AngularVelocity(angularVelocity3D.Z); + } - /// - /// X component of angular velocity - /// - public AngularVelocity X { get; set; } - /// - /// Y component of angular velocity - /// - public AngularVelocity Y { get; set; } - /// - /// Z component of angular velocity - /// - public AngularVelocity Z { get; set; } - - /// - /// Compare two 3DAngularVelocity objects - /// - /// object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((AngularVelocity3D)obj); - } + /// + /// X component of angular velocity + /// + public AngularVelocity X { get; set; } + /// + /// Y component of angular velocity + /// + public AngularVelocity Y { get; set; } + /// + /// Z component of angular velocity + /// + public AngularVelocity Z { get; set; } + + /// + /// Compare two 3DAngularVelocity objects + /// + /// object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Get hash of object - /// - /// hash as 32 bit integer - [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; + /// + /// Get hash of object + /// + /// hash as 32 bit integer + [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; - /// - /// Compare two 3DAngularVelocity objects - /// - /// object to compare - /// true if equal - [Pure] public bool Equals(AngularVelocity3D other) => - X == other.X && - Y == other.Y && - Z == other.Z; - - /// - /// Equals operator to compare two AngularVelocity3D objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(AngularVelocity3D left, AngularVelocity3D right) => Equals(left, right); - - /// - /// Not equals operator to compare two AngularVelocity3D objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(AngularVelocity3D left, AngularVelocity3D right) => !Equals(left, right); + /// + /// Compare two 3DAngularVelocity objects + /// + /// object to compare + /// true if equal + [Pure] public bool Equals(AngularVelocity3D other) => + X == other.X && + Y == other.Y && + Z == other.Z; + + /// + /// Equals operator to compare two AngularVelocity3D objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(AngularVelocity3D left, AngularVelocity3D right) => Equals(left, right); + + /// + /// Not equals operator to compare two AngularVelocity3D objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(AngularVelocity3D left, AngularVelocity3D right) => !Equals(left, right); - /// - /// Less than operator to compare two AngularVelocity3D objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) < 0; - - /// - /// Greater than operator to compare two AngularVelocity3D objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) > 0; - - /// - /// Less than or equal operator to compare two AngularVelocity3D objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) <= 0; - - /// - /// Greater than or equal operator to compare two AngularVelocity3D objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) >= 0; - - // Math - /// - /// Addition operator to add two AngularAcceleration objects - /// - /// left value - /// right value - /// A new AngularVelocity3D object with a value of left + right - [Pure] public static AngularVelocity3D operator +(AngularVelocity3D left, AngularVelocity3D right) - { - var x = left.X + right.X; - var y = left.Y + right.Y; - var z = left.Z + right.Z; + /// + /// Less than operator to compare two AngularVelocity3D objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) < 0; - return new AngularVelocity3D(x, y, z); - } + /// + /// Greater than operator to compare two AngularVelocity3D objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) > 0; - /// - /// Subtraction operator to subtract two AngularVelocity3D objects - /// - /// left value - /// right value - /// A new AngularVelocity3D object with a value of left - right - [Pure] public static AngularVelocity3D operator -(AngularVelocity3D left, AngularVelocity3D right) - { - var x = left.X - right.X; - var y = left.Y - right.Y; - var z = left.Z - right.Z; + /// + /// Less than or equal operator to compare two AngularVelocity3D objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) <= 0; - return new AngularVelocity3D(x, y, z); - } + /// + /// Greater than or equal operator to compare two AngularVelocity3D objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(AngularVelocity3D left, AngularVelocity3D right) => Comparer.Default.Compare(left, right) >= 0; - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; - - // IComparable - /// - /// Compare to another AngularVelocity3D object - /// - /// The other AngularVelocity3D cast to object - /// - /// - public int CompareTo(object obj) - { - throw new NotImplementedException(); - } + // Math + /// + /// Addition operator to add two AngularAcceleration objects + /// + /// left value + /// right value + /// A new AngularVelocity3D object with a value of left + right + [Pure] public static AngularVelocity3D operator +(AngularVelocity3D left, AngularVelocity3D right) + { + var x = left.X + right.X; + var y = left.Y + right.Y; + var z = left.Z + right.Z; - /// - /// Compare the default value to three double - /// - /// values to compare - /// true if equal - public bool Equals((double ValueX, double ValueY, double ValueZ) other) - { - return X.Equals(other.ValueX) && - Y.Equals(other.ValueY) && - Z.Equals(other.ValueZ); - } + return new AngularVelocity3D(x, y, z); + } + + /// + /// Subtraction operator to subtract two AngularVelocity3D objects + /// + /// left value + /// right value + /// A new AngularVelocity3D object with a value of left - right + [Pure] public static AngularVelocity3D operator -(AngularVelocity3D left, AngularVelocity3D right) + { + var x = left.X - right.X; + var y = left.Y - right.Y; + var z = left.Z - right.Z; + + return new AngularVelocity3D(x, y, z); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - public int CompareTo((double, double, double) other) + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; + + // IComparable + /// + /// Compare to another AngularVelocity3D object + /// + /// The other AngularVelocity3D cast to object + /// + public int CompareTo(object obj) + { + if (obj is AngularVelocity3D angularVelocity3D) { - throw new NotImplementedException(); + return X.CompareTo(angularVelocity3D.X) | + Y.CompareTo(angularVelocity3D.Y) | + Z.CompareTo(angularVelocity3D.Z); } + + throw new ArgumentException("Object is not an AngularVelocity3D"); + } + + /// + /// Compare the default value to three doubles + /// + /// values to compare + /// true if equal + public bool Equals((double ValueX, double ValueY, double ValueZ) other) + { + return X.Equals(other.ValueX) && + Y.Equals(other.ValueY) && + Z.Equals(other.ValueZ); + } + + /// + /// Compare the default value to a tuple of three doubles + /// + /// value to compare + /// 0 if equal + public int CompareTo((double, double, double) other) + { + return X.CompareTo(other.Item1) | + Y.CompareTo(other.Item2) | + Z.CompareTo(other.Item3); } } \ No newline at end of file diff --git a/Source/Meadow.Units/ApparentPower.cs b/Source/Meadow.Units/ApparentPower.cs index 4b8b280..38e10cd 100644 --- a/Source/Meadow.Units/ApparentPower.cs +++ b/Source/Meadow.Units/ApparentPower.cs @@ -5,408 +5,410 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Apparent Power +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct ApparentPower : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Apparent Power + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct ApparentPower : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The ApparentPower value. + /// Volt Amperes by default. + public ApparentPower(double value, UnitType type = UnitType.VoltAmperes) { - /// - /// Creates a new `ApparentPower` object. - /// - /// The ApparentPower value. - /// Volt Amperes by default. - public ApparentPower(double value, UnitType type = UnitType.VoltAmperes) - { - Value = ApparentPowerConversions.Convert(value, type, UnitType.VoltAmperes); - } + Value = ApparentPowerConversions.Convert(value, type, UnitType.VoltAmperes); + } - /// - /// Creates a new `ApparentPower` object from an existingApparentPower object - /// - /// - public ApparentPower(ApparentPower reactivePower) - { - this.Value = reactivePower.Value; - } + /// + /// Creates a new object from an existing ApparentPower object + /// + /// + public ApparentPower(ApparentPower reactivePower) + { + Value = reactivePower.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe ApparentPower. + /// + public enum UnitType + { /// - /// The type of units available to describe ApparentPower. + /// Gigavolt Ampere /// - public enum UnitType - { - /// - /// Gigavolt Ampere - /// - GigavoltAmperes, - /// - /// Megavolt Ampere - /// - MegavoltAmperes, - /// - /// Kilovolt Ampere - /// - KilovoltAmperes, - /// - /// Volt Ampere - /// - VoltAmperes, - /// - /// Millivolt Ampere - /// - MillivoltAmperes, - } - + GigavoltAmperes, /// - ///ApparentPower in Millovolt Amperes + /// Megavolt Ampere /// - public double MillivoltAmperes => From(UnitType.MillivoltAmperes); - + MegavoltAmperes, /// - ///ApparentPower in Volt Amperes + /// Kilovolt Ampere /// - public double VoltAmperes => From(UnitType.VoltAmperes); - + KilovoltAmperes, /// - ///ApparentPower in Kilvolt Amperes + /// Volt Ampere /// - public double KilovoltAmperes => From(UnitType.KilovoltAmperes); - + VoltAmperes, /// - ///ApparentPower in Megavolt Amperes + /// Millivolt Ampere /// - public double MegavoltAmperes => From(UnitType.MegavoltAmperes); + MillivoltAmperes, + } - /// - ///ApparentPower in Gigavolt Amperes - /// - public double GigavoltAmperes => From(UnitType.GigavoltAmperes); + /// + ///ApparentPower in Millovolt Amperes + /// + public double MillivoltAmperes => From(UnitType.MillivoltAmperes); - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] - public double From(UnitType convertTo) - { - return ApparentPowerConversions.Convert(Value, UnitType.VoltAmperes, convertTo); - } + /// + ///ApparentPower in Volt Amperes + /// + public double VoltAmperes => From(UnitType.VoltAmperes); - /// - /// Compare to another ApparentPower object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((ApparentPower)obj); - } + /// + ///ApparentPower in Kilvolt Amperes + /// + public double KilovoltAmperes => From(UnitType.KilovoltAmperes); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator ApparentPower(ushort value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(short value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(uint value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(long value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(int value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(float value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(double value) => new ApparentPower(value); - //[Pure] public static implicit operator ApparentPower(decimal value) => new ApparentPower((double)value); - - // Comparison - /// - /// Compare to another Apparent Power object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(ApparentPower other) => Value == other.Value; + /// + ///ApparentPower in Megavolt Amperes + /// + public double MegavoltAmperes => From(UnitType.MegavoltAmperes); - /// - /// Equals operator to compare two ApparentPower objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(ApparentPower left, ApparentPower right) => Equals(left.Value, right.Value); + /// + ///ApparentPower in Gigavolt Amperes + /// + public double GigavoltAmperes => From(UnitType.GigavoltAmperes); - /// - /// Not equals operator to compare twoApparentPower objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(ApparentPower left, ApparentPower right) => !Equals(left.Value, right.Value); + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] + public double From(UnitType convertTo) + { + return ApparentPowerConversions.Convert(Value, UnitType.VoltAmperes, convertTo); + } - /// - /// Compare to anotherApparentPower object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(ApparentPower other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Compare to another ApparentPower object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Less than operator to compare twoApparentPower objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator ApparentPower(ushort value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(short value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(uint value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(long value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(int value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(float value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(double value) => new ApparentPower(value); + //[Pure] public static implicit operator ApparentPower(decimal value) => new ApparentPower((double)value); + + // Comparison + /// + /// Compare to another Apparent Power object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(ApparentPower other) => Value == other.Value; - /// - /// Greater than operator to compare twoApparentPower objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Equals operator to compare two ApparentPower objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(ApparentPower left, ApparentPower right) => Equals(left.Value, right.Value); - /// - /// Less than or equal operator to compare twoApparentPower objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Not equals operator to compare two ApparentPower objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(ApparentPower left, ApparentPower right) => !Equals(left.Value, right.Value); - /// - /// Greater than or equal operator to compare twoApparentPower objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Compare to another ApparentPower object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(ApparentPower other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - // Math - /// - /// Addition operator to add twoApparentPower objects - /// - /// left value - /// right value - /// A new ApparentPower object with a value of left + right - [Pure] public static ApparentPower operator +(ApparentPower left, ApparentPower right) => new(left.Value + right.Value); + /// + /// Less than operator to compare two ApparentPower objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Subtraction operator to subtract twoApparentPower objects - /// - /// left value - /// right value - /// A new ApparentPower object with a value of left - right - [Pure] public static ApparentPower operator -(ApparentPower left, ApparentPower right) => new(left.Value - right.Value); + /// + /// Greater than operator to compare two ApparentPower objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - /// - /// Multipication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new ApparentPower object with a value of value multiplied by the operand - [Pure] public static ApparentPower operator *(ApparentPower value, double operand) => new(value.Value * operand); + /// + /// Less than or equal operator to compare two ApparentPower objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new ApparentPower object with a value of value divided by the operand - [Pure] public static ApparentPower operator /(ApparentPower value, double operand) => new(value.Value / operand); + /// + /// Greater than or equal operator to compare two ApparentPower objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(ApparentPower left, ApparentPower right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Returns the absolute length, that is, the length without regards to - /// negative polarity - /// - /// - [Pure] public ApparentPower Abs() { return new ApparentPower(Math.Abs(this.Value)); } + // Math + /// + /// Addition operator to add two ApparentPower objects + /// + /// left value + /// right value + /// A new ApparentPower object with a value of left + right + [Pure] public static ApparentPower operator +(ApparentPower left, ApparentPower right) => new(left.Value + right.Value); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Subtraction operator to subtract two ApparentPower objects + /// + /// left value + /// right value + /// A new ApparentPower object with a value of left - right + [Pure] public static ApparentPower operator -(ApparentPower left, ApparentPower right) => new(left.Value - right.Value); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new ApparentPower object with a value of value multiplied by the operand + [Pure] public static ApparentPower operator *(ApparentPower value, double operand) => new(value.Value * operand); - // IComparable - /// - /// Compare to anotherApparentPower object - /// - /// The otherApparentPower cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new ApparentPower object with a value of value divided by the operand + [Pure] public static ApparentPower operator /(ApparentPower value, double operand) => new(value.Value / operand); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Returns the absolute length, that is, the length without regards to + /// negative polarity + /// + /// + [Pure] public ApparentPower Abs() { return new ApparentPower(Math.Abs(Value)); } - /// - /// Covert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Covert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Covert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + // IComparable + /// + /// Compare to another ApparentPower object + /// + /// The other ApparentPower cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is ApparentPower apparentPower) + { + return Value.CompareTo(apparentPower.Value); + } - /// - /// Covert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + throw new ArgumentException("Object is not an ApparentPower"); + } - /// - /// Covert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Covert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Covert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Covert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Covert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Covert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Covert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Covert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Covert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Covert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Covert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Covert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Covert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Covert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Covert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Covert to type - /// - /// conversion type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Covert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Covert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Covert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Covert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Covert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Covert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Covert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Covert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Covert to type + /// + /// conversion type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Covert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Covert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Covert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Azimuth.cs b/Source/Meadow.Units/Azimuth.cs index 686f654..ba30b81 100644 --- a/Source/Meadow.Units/Azimuth.cs +++ b/Source/Meadow.Units/Azimuth.cs @@ -5,439 +5,434 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; +// TODO: add DegreesMinutes type. + +// Notes: +// 1. there are additional precision compass cardinal points: http://tamivox.org/dave/compass/index.html + +/// +/// Represents a cardinal direction; +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Azimuth : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { - // TODO: add DegreesMinutes type. + /// + /// Creates a new object. + /// + /// The cardinal direction value. + public Azimuth(double value) + { + Value = ConvertTo360(value); + } - // Notes: - // 1. there are additional precision compass cardinal points: http://tamivox.org/dave/compass/index.html + /// + /// Creates a new object. + /// + /// The cardinal direction. + public Azimuth(Azimuth16PointCardinalNames cardinalPoint) + { + Value = AzimuthConversions.Compass16CardinalsToDegrees(cardinalPoint); + } /// - /// Represents a cardinal direction; + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Azimuth : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// source object + public Azimuth(Azimuth azimuth) { - /// - /// Creates a new `Azimuth` object. - /// - /// The cardinal direction value. - public Azimuth(double value) - { - Value = ConvertTo360(value); - } + Value = azimuth.Value; + } - /// - /// Creates a new `Azimuth` object. - /// - /// The cardinal direction. - public Azimuth(Azimuth16PointCardinalNames cardinalPoint) - { - Value = AzimuthConversions.Compass16CardinalsToDegrees(cardinalPoint); - } + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the azimuth. + /// + public enum UnitType + { /// - /// Creates a new `Azimuth` object. + /// Degrees (decimal) /// - /// source object - public Azimuth(Azimuth azimuth) - { - Value = azimuth.Value; - } - + DecimalDegrees, /// - /// Internal canonical value. + /// Cardinal compass point names (English) /// - private readonly double Value; + Compass16CardinalPointNames + } - /// - /// The type of units available to describe the azimuth. - /// - public enum UnitType - { - /// - /// Degrees (decimal) - /// - DecimalDegrees, - /// - /// Cardinal compass point names (English) - /// - Compass16CardinalPointNames - } + //======================== + // TO property conversions - //======================== - // TO property conversions + /// + /// Gets the cardinal direction value expressed as a unit _Decimal Degrees_ (`°`) + /// + public double DecimalDegrees => Value; + /// + /// Gets the cardinal direction value expressed as Radians + /// + public double Radians => Value * Math.PI / 180.0; - /// - /// Gets the cardinal direction value expressed as a unit _Decimal Degrees_ (`°`) - /// - public double DecimalDegrees => Value; - /// - /// Gets the cardinal direction value expressed as Radians - /// - public double Radians => Value * Math.PI / 180.0; + /// + /// Gets the cardinal direction value expressed as a unit a 16 division cardinal point + /// name. + /// + public Azimuth16PointCardinalNames Compass16PointCardinalName => AzimuthConversions.DegressToCompass16PointCardinalName(Value); - /// - /// Gets the cardinal direction value expressed as a unit a 16 division cardinal point - /// name. - /// - public Azimuth16PointCardinalNames Compass16PointCardinalName - { - get - { - return AzimuthConversions.DegressToCompass16PointCardinalName(Value); - } - } + //============================= + // FROM convenience conversions - //============================= - // FROM convenience conversions + /// + /// Creates a new object from a unit value in _Decimal Degrees_ (`°`). + /// + /// The cardinal direction value. + /// A new cardinal direction object. + [Pure] public static Azimuth FromDecimalDegrees(double degrees) => new(ConvertTo360(degrees)); - /// - /// Creates a new `Azimuth` object from a unit value in _Decimal Degrees_ (`°`). - /// - /// The cardinal direction value. - /// A new cardinal direction object. - [Pure] public static Azimuth FromDecimalDegrees(double degrees) => new(ConvertTo360(degrees)); + /// + /// Creates a new object from a unit value in Radians. + /// + /// The cardinal direction value. + /// A new cardinal direction object. + [Pure] public static Azimuth FromRadians(double radians) => new(ConvertTo360(radians * 180 / Math.PI)); - /// - /// Creates a new `Azimuth` object from a unit value in Radians. - /// - /// The cardinal direction value. - /// A new cardinal direction object. - [Pure] public static Azimuth FromRadians(double radians) => new(ConvertTo360(radians * 180 / Math.PI)); + /// + /// Creates a new object + /// + /// The 16 point cardinal direction. + /// A new cardinal direction object. + [Pure] public static Azimuth FromCompass16PointCardinalName(Azimuth16PointCardinalNames name) => new(name); - /// - /// Creates a new `Azimuth` object - /// - /// The 16 point cardinal direction. - /// A new cardinal direction object. - [Pure] public static Azimuth FromCompass16PointCardinalName(Azimuth16PointCardinalNames name) => new(name); + /// + /// Compare to another Azimuth object. + /// + /// object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Compare to another Azimuth object. - /// - /// object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Azimuth)obj); - } + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Azimuth(ushort value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(short value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(uint value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(long value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(int value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(float value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(double value) => new Azimuth(value); + //[Pure] public static implicit operator Azimuth(decimal value) => new Azimuth((double)value); + + // Comparison + /// + /// Compare to another Azimuth object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Azimuth other) => Value == other.Value; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Azimuth(ushort value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(short value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(uint value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(long value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(int value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(float value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(double value) => new Azimuth(value); - //[Pure] public static implicit operator Azimuth(decimal value) => new Azimuth((double)value); - - // Comparison - /// - /// Compare to another Azimuth object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Azimuth other) => Value == other.Value; + /// + /// Equals operator to compare two Azimuth objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Azimuth left, Azimuth right) => Equals(left.Value, right.Value); - /// - /// Equals operator to compare two Azimuth objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Azimuth left, Azimuth right) => Equals(left.Value, right.Value); + /// + /// Not equals operator to compare two Azimuth objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Azimuth left, Azimuth right) => !Equals(left.Value, right.Value); - /// - /// Not equals operator to compare two Azimuth objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Azimuth left, Azimuth right) => !Equals(left.Value, right.Value); + /// + /// Compare to another Azimuth object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Azimuth other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Compare to another Azimuth object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Azimuth other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Less than operator to compare two Azimuth objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Less than operator to compare two Azimuth objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Greater than operator to compare two Azimuth objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - /// - /// Greater than operator to compare two Azimuth objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Less than or equal operator to compare two Azimuth objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Less than or equal operator to compare two Azimuth objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Greater than or equal operator to compare two Azimuth objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Greater than or equal operator to compare two Azimuth objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Azimuth left, Azimuth right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Helper method to ensure mathematical results 'wrap' correctly at 0/360 degrees. + /// + /// proper result in the range of [0,360) + private static double ConvertTo360(double value) + { + value %= 360; + if (value < 0) value += 360; + return value; + } - /// - /// Helper method to ensure mathematical results 'wrap' correctly at 0/360 degrees. - /// - /// proper result in the range of [0,360) - private static double ConvertTo360(double value) - { - value %= 360; - if (value < 0) value += 360; - return value; - } + // Math + /// + /// Addition operator to add two Azimuth objects + /// + /// left value + /// right value + /// A new Azimuth object with a value of left + right + [Pure] + public static Azimuth operator +(Azimuth left, Azimuth right) => new Azimuth(ConvertTo360(left.Value + right.Value)); - // Math - /// - /// Addition operator to add two Azimuth objects - /// - /// left value - /// right value - /// A new Azimuth object with a value of left + right - [Pure] - public static Azimuth operator +(Azimuth left, Azimuth right) => new Azimuth(ConvertTo360(left.Value + right.Value)); + /// + /// Subtraction operator to subtract two Azimuth objects + /// + /// left value + /// right value + /// A new Azimuth object with a value of left - right + [Pure] public static Azimuth operator -(Azimuth left, Azimuth right) => new(ConvertTo360(left.Value - right.Value)); - /// - /// Subtraction operator to subtract two Azimuth objects - /// - /// left value - /// right value - /// A new Azimuth object with a value of left - right - [Pure] public static Azimuth operator -(Azimuth left, Azimuth right) => new(ConvertTo360(left.Value - right.Value)); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Azimuth object with a value of value multiplied by the operand + [Pure] public static Azimuth operator *(Azimuth value, double operand) => new(ConvertTo360(value.Value * operand)); - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Azimuth object with a value of value multiplied by the operand - [Pure] public static Azimuth operator *(Azimuth value, double operand) => new(ConvertTo360(value.Value * operand)); + /// + /// Multiplication operator to divide by a double + /// + /// object to divide + /// operand to divide object + /// A new Azimuth object with a value of value divided by the operand + [Pure] public static Azimuth operator /(Azimuth value, double operand) => new(ConvertTo360(value.Value / operand)); - /// - /// Multiplication operator to divide by a double - /// - /// object to divide - /// operand to divide object - /// A new Azimuth object with a value of value divided by the operand - [Pure] public static Azimuth operator /(Azimuth value, double operand) => new(ConvertTo360(value.Value / operand)); + private static double StandardizeAzimuth(double value) + { + value %= 360d; + if (value < 0) return value + 360d; + return value; + } - private static double StandardizeAzimuth(double value) - { - value %= 360d; - if (value < 0) return value + 360d; - return value; - } + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Azimuth Abs() { return new Azimuth(Math.Abs(Value)); } - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Azimuth Abs() { return new Azimuth(Math.Abs(Value)); } + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + // IComparable + /// + /// Compare to another Azimuth object + /// + /// The other Azimuth cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Azimuth azimuth) + { + return Value.CompareTo(azimuth.Value); + } - // IComparable - /// - /// Compare to another AbsoluteHumidity object - /// - /// The other AbsoluteHumity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + throw new ArgumentException("Object is not an Azimuth"); + } - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); - } + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Azimuth16PointCardinalNames.cs b/Source/Meadow.Units/Azimuth16PointCardinalNames.cs index 0d15ff6..dbcab97 100644 --- a/Source/Meadow.Units/Azimuth16PointCardinalNames.cs +++ b/Source/Meadow.Units/Azimuth16PointCardinalNames.cs @@ -1,73 +1,72 @@ -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Cardinal compass directions +/// +public enum Azimuth16PointCardinalNames { /// - /// Cardinal compass directions - /// - public enum Azimuth16PointCardinalNames - { - /// - /// North - /// - N, - /// - /// North by northeast - /// - NNE, - /// - /// Northeast - /// - NE, - /// - /// East by northeast - /// - ENE, - /// - /// East - /// - E, - /// - /// East by southeast - /// - ESE, - /// - /// Southeast - /// - SE, - /// - /// South by southeast - /// - SSE, - /// - /// South - /// - S, - /// - /// South by southwest - /// - SSW, - /// - /// Southwest - /// - SW, - /// - /// West by southwest - /// - WSW, - /// - /// West - /// - W, - /// - /// West by northwest - /// - WNW, - /// - /// Northwest - /// - NW, - /// - /// North by northwest - /// - NNW - } + /// North + /// + N, + /// + /// North by northeast + /// + NNE, + /// + /// Northeast + /// + NE, + /// + /// East by northeast + /// + ENE, + /// + /// East + /// + E, + /// + /// East by southeast + /// + ESE, + /// + /// Southeast + /// + SE, + /// + /// South by southeast + /// + SSE, + /// + /// South + /// + S, + /// + /// South by southwest + /// + SSW, + /// + /// Southwest + /// + SW, + /// + /// West by southwest + /// + WSW, + /// + /// West + /// + W, + /// + /// West by northwest + /// + WNW, + /// + /// Northwest + /// + NW, + /// + /// North by northwest + /// + NNW } \ No newline at end of file diff --git a/Source/Meadow.Units/Charge.cs b/Source/Meadow.Units/Charge.cs index ea729e7..c1ddda7 100644 --- a/Source/Meadow.Units/Charge.cs +++ b/Source/Meadow.Units/Charge.cs @@ -4,353 +4,356 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents a value of Electric Charge. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Charge : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents a value of Electric Charge. + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Charge : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Charge value. + /// Coulombs by default. + public Charge(double value, UnitType type = UnitType.Coulombs) { - /// - /// Creates a new `Charge` object. - /// - /// The Charge value. - /// Coulombs by default. - public Charge(double value, UnitType type = UnitType.Coulombs) - { - Coulombs = value; - } + Coulombs = value; + } - /// - /// Creates a new `Charge` object from an existing Charge object - /// - /// - public Charge(Charge Charge) - { - Coulombs = Charge.Coulombs; - } + /// + /// Creates a new object from an existing Charge object + /// + /// + public Charge(Charge Charge) + { + Coulombs = Charge.Coulombs; + } - /// - /// The Charge in coulombs - /// - public double Coulombs { get; private set; } + /// + /// The Charge in coulombs + /// + public double Coulombs { get; private set; } + /// + /// The type of units available to describe the Charge. + /// + public enum UnitType + { /// - /// The type of units available to describe the Charge. + /// coulombs /// - public enum UnitType - { - /// - /// coulombs - /// - Coulombs - } + Coulombs + } - /// - /// Compare to another Charge object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Charge)obj); - } + /// + /// Compare to another Charge object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Coulombs.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Charge(ushort value) => new Charge(value); - //[Pure] public static implicit operator Charge(short value) => new Charge(value); - //[Pure] public static implicit operator Charge(uint value) => new Charge(value); - //[Pure] public static implicit operator Charge(long value) => new Charge(value); - //[Pure] public static implicit operator Charge(int value) => new Charge(value); - //[Pure] public static implicit operator Charge(float value) => new Charge(value); - //[Pure] public static implicit operator Charge(double value) => new Charge(value); - //[Pure] public static implicit operator Charge(decimal value) => new Charge((double)value); - - // Comparison - /// - /// Compare to another Charge object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Charge other) => Coulombs == other.Coulombs; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Coulombs.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Charge(ushort value) => new Charge(value); + //[Pure] public static implicit operator Charge(short value) => new Charge(value); + //[Pure] public static implicit operator Charge(uint value) => new Charge(value); + //[Pure] public static implicit operator Charge(long value) => new Charge(value); + //[Pure] public static implicit operator Charge(int value) => new Charge(value); + //[Pure] public static implicit operator Charge(float value) => new Charge(value); + //[Pure] public static implicit operator Charge(double value) => new Charge(value); + //[Pure] public static implicit operator Charge(decimal value) => new Charge((double)value); + + // Comparison + /// + /// Compare to another Charge object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Charge other) => Coulombs == other.Coulombs; - /// - /// Equals operator to compare two Charge objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Charge left, Charge right) => Equals(left.Coulombs, right.Coulombs); + /// + /// Equals operator to compare two Charge objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Charge left, Charge right) => Equals(left.Coulombs, right.Coulombs); - /// - /// Not equals operator to compare two Charge objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Charge left, Charge right) => !Equals(left.Coulombs, right.Coulombs); + /// + /// Not equals operator to compare two Charge objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Charge left, Charge right) => !Equals(left.Coulombs, right.Coulombs); - /// - /// Compare to another Charge object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Charge other) => Equals(Coulombs, other.Coulombs) ? 0 : Coulombs.CompareTo(other.Coulombs); + /// + /// Compare to another Charge object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Charge other) => Equals(Coulombs, other.Coulombs) ? 0 : Coulombs.CompareTo(other.Coulombs); - /// - /// Less than operator to compare two Charge objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) < 0; + /// + /// Less than operator to compare two Charge objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) < 0; - /// - /// Greater than operator to compare two Charge objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) > 0; + /// + /// Greater than operator to compare two Charge objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) > 0; - /// - /// Less than or equal operator to compare two Charge objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) <= 0; + /// + /// Less than or equal operator to compare two Charge objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) <= 0; - /// - /// Greater than or equal operator to compare two Charge objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) >= 0; + /// + /// Greater than or equal operator to compare two Charge objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Charge left, Charge right) => Comparer.Default.Compare(left.Coulombs, right.Coulombs) >= 0; - // Math - /// - /// Addition operator to add two Charge objects - /// - /// left value - /// right value - /// A new Charge object with a value of left + right - [Pure] public static Charge operator +(Charge left, Charge right) => new (left.Coulombs + right.Coulombs); + // Math + /// + /// Addition operator to add two Charge objects + /// + /// left value + /// right value + /// A new Charge object with a value of left + right + [Pure] public static Charge operator +(Charge left, Charge right) => new (left.Coulombs + right.Coulombs); - /// - /// Subtraction operator to subtract two Charge objects - /// - /// left value - /// right value - /// A new Charge object with a value of left - right - [Pure] public static Charge operator -(Charge left, Charge right) => new (left.Coulombs - right.Coulombs); + /// + /// Subtraction operator to subtract two Charge objects + /// + /// left value + /// right value + /// A new Charge object with a value of left - right + [Pure] public static Charge operator -(Charge left, Charge right) => new (left.Coulombs - right.Coulombs); - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Charge object with a value of value multiplied by the operand - [Pure] public static Charge operator *(Charge value, double operand) => new (value.Coulombs * operand); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Charge object with a value of value multiplied by the operand + [Pure] public static Charge operator *(Charge value, double operand) => new (value.Coulombs * operand); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Charge object with a value of value divided by the operand - [Pure] public static Charge operator /(Charge value, double operand) => new (value.Coulombs / operand); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Charge object with a value of value divided by the operand + [Pure] public static Charge operator /(Charge value, double operand) => new (value.Coulombs / operand); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Charge Abs() { return new Charge(Math.Abs(this.Coulombs)); } + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Charge Abs() { return new Charge(Math.Abs(Coulombs)); } - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Coulombs.ToString(); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Coulombs.ToString(); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Coulombs.ToString(format, formatProvider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Coulombs.ToString(format, formatProvider); - // IComparable - /// - /// Compare to another Charge object - /// - /// The other Charge cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Coulombs.CompareTo(obj); + // IComparable + /// + /// Compare to another Charge object + /// + /// The other Charge cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Charge charge) + { + return Coulombs.CompareTo(charge.Coulombs); + } - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Coulombs.GetTypeCode(); + throw new ArgumentException("Object is not a Charge"); + } - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Coulombs).ToBoolean(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Coulombs.GetTypeCode(); - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Coulombs).ToByte(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Coulombs).ToBoolean(provider); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Coulombs).ToChar(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Coulombs).ToByte(provider); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Coulombs).ToDateTime(provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Coulombs).ToChar(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Coulombs).ToDecimal(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Coulombs).ToDateTime(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Coulombs; + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Coulombs).ToDecimal(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Coulombs).ToInt16(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Coulombs; - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Coulombs).ToInt32(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Coulombs).ToInt16(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Coulombs).ToInt64(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Coulombs).ToInt32(provider); - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Coulombs).ToSByte(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Coulombs).ToInt64(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Coulombs).ToSingle(provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Coulombs).ToSByte(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Coulombs.ToString(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Coulombs).ToSingle(provider); - /// - /// Convert to type - /// - /// conversion unit type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Coulombs).ToType(conversionType, provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Coulombs.ToString(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Coulombs).ToUInt16(provider); + /// + /// Convert to type + /// + /// conversion unit type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Coulombs).ToType(conversionType, provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Coulombs).ToUInt32(provider); + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Coulombs).ToUInt16(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Coulombs).ToUInt64(provider); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Coulombs).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) - { - return (other is null) ? -1 : (Coulombs).CompareTo(other.Value); - } + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Coulombs).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Coulombs.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Coulombs).CompareTo(other.Value); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Coulombs.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Coulombs.Equals(other); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Coulombs.CompareTo(other); - } + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Coulombs.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Coulombs.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Concentration.cs b/Source/Meadow.Units/Concentration.cs index 65c30c1..902108c 100644 --- a/Source/Meadow.Units/Concentration.cs +++ b/Source/Meadow.Units/Concentration.cs @@ -5,392 +5,395 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Concentration +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Concentration : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Concentration + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Concentration : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Concentration value. + /// Parts Per Million by default. + public Concentration(double value, UnitType type = UnitType.PartsPerMillion) { - /// - /// Creates a new `Concentration` object. - /// - /// The Concentration value. - /// Parts Per Million by default. - public Concentration(double value, UnitType type = UnitType.PartsPerMillion) - { - Value = ConcentrationConversions.Convert(value, type, UnitType.PartsPerMillion); - } - - /// - /// Creates a copy of a `Concentration` object. - /// - /// Concentration to copy - public Concentration(Concentration concentration) - { - Value = concentration.Value; - } + Value = ConcentrationConversions.Convert(value, type, UnitType.PartsPerMillion); + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Creates a copy of a object. + /// + /// Concentration to copy + public Concentration(Concentration concentration) + { + Value = concentration.Value; + } - /// - /// The type of units available to describe the Concentration. - /// - public enum UnitType - { - /// - /// Parts per 100 - /// - PartsPerHundred, - /// - /// Parts per 1,000 - /// - PartsPerThousand, - /// - /// Parts per 1,000,000 - /// - PartsPerMillion, - /// - /// Parts per 1,000,000,000 - /// - PartsPerBillion, - } + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the Concentration. + /// + public enum UnitType + { /// - /// Get Concentration in parts per 100 + /// Parts per 100 /// - public double PartsPerHundred => From(UnitType.PartsPerHundred); + PartsPerHundred, /// - /// Get Concentration in parts per 1000 + /// Parts per 1,000 /// - public double PartsPerThousand => From(UnitType.PartsPerThousand); + PartsPerThousand, /// - /// Get Concentration in parts per 1,000,000 + /// Parts per 1,000,000 /// - public double PartsPerMillion => From(UnitType.PartsPerMillion); + PartsPerMillion, /// - /// Get Concentration in parts per 1,000,000,000 + /// Parts per 1,000,000,000 /// - public double PartsPerBillion => From(UnitType.PartsPerBillion); - - /// - /// Get Concentration for a specific unit - /// - /// unit - /// value as a double - [Pure] public double From(UnitType convertTo) - { - return ConcentrationConversions.Convert(Value, UnitType.PartsPerMillion, convertTo); - } + PartsPerBillion, + } - /// - /// Compare two Concentration objects - /// - /// - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Concentration)obj); - } + /// + /// Get Concentration in parts per 100 + /// + public double PartsPerHundred => From(UnitType.PartsPerHundred); + /// + /// Get Concentration in parts per 1000 + /// + public double PartsPerThousand => From(UnitType.PartsPerThousand); + /// + /// Get Concentration in parts per 1,000,000 + /// + public double PartsPerMillion => From(UnitType.PartsPerMillion); + /// + /// Get Concentration in parts per 1,000,000,000 + /// + public double PartsPerBillion => From(UnitType.PartsPerBillion); - /// - /// Get hash of Concentration object - /// - /// int32 hash of object - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Concentration(ushort value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(short value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(uint value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(long value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(int value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(float value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(double value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(decimal value) => new Concentration((double)value); - - // Comparison - /// - /// Compare to another Concentration objects - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Concentration other) => Value == other.Value; + /// + /// Get Concentration for a specific unit + /// + /// unit + /// value as a double + [Pure] public double From(UnitType convertTo) + { + return ConcentrationConversions.Convert(Value, UnitType.PartsPerMillion, convertTo); + } - /// - /// Equals operator to compare two Concentration objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Concentration left, Concentration right) => Equals(left.Value, right.Value); + /// + /// Compare two Concentration objects + /// + /// + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Not equals operator to compare two Concentration objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Concentration left, Concentration right) => !Equals(left.Value, right.Value); + /// + /// Get hash of Concentration object + /// + /// int32 hash of object + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Concentration(ushort value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(short value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(uint value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(long value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(int value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(float value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(double value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(decimal value) => new Concentration((double)value); + + // Comparison + /// + /// Compare to another Concentration objects + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Concentration other) => Value == other.Value; - /// - /// Compare to another Concentration object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Concentration other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Equals operator to compare two Concentration objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Concentration left, Concentration right) => Equals(left.Value, right.Value); - /// - /// Less than operator to compare two Concentration objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Not equals operator to compare two Concentration objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Concentration left, Concentration right) => !Equals(left.Value, right.Value); - /// - /// Greater than operator to compare two Concentration objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Compare to another Concentration object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Concentration other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Less than or equal operator to compare two Concentration objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Less than operator to compare two Concentration objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Greater than or equal operator to compare two Concentration objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Greater than operator to compare two Concentration objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - // Math - /// - /// Addition operator to add two Concentration objects - /// - /// left value - /// right value - /// A new Concentration object with a value of left + right - [Pure] public static Concentration operator +(Concentration left, Concentration right) => new (left.Value + right.Value); + /// + /// Less than or equal operator to compare two Concentration objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Subtraction operator to subtract two Concentration objects - /// - /// left value - /// right value - /// A new Concentration object with a value of left - right - [Pure] public static Concentration operator -(Concentration left, Concentration right) => new (left.Value - right.Value); + /// + /// Greater than or equal operator to compare two Concentration objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Concentration left, Concentration right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Concentration object with a value of value multiplied by the operand - [Pure] public static Concentration operator *(Concentration value, double operand) => new (value.Value * operand); + // Math + /// + /// Addition operator to add two Concentration objects + /// + /// left value + /// right value + /// A new Concentration object with a value of left + right + [Pure] public static Concentration operator +(Concentration left, Concentration right) => new (left.Value + right.Value); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Concentration object with a value of value divided by the operand - [Pure] public static Concentration operator /(Concentration value, double operand) => new (value.Value / operand); + /// + /// Subtraction operator to subtract two Concentration objects + /// + /// left value + /// right value + /// A new Concentration object with a value of left - right + [Pure] public static Concentration operator -(Concentration left, Concentration right) => new (left.Value - right.Value); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Concentration Abs() { return new Concentration(Math.Abs(this.Value)); } + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Concentration object with a value of value multiplied by the operand + [Pure] public static Concentration operator *(Concentration value, double operand) => new (value.Value * operand); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Concentration object with a value of value divided by the operand + [Pure] public static Concentration operator /(Concentration value, double operand) => new (value.Value / operand); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Concentration Abs() { return new Concentration(Math.Abs(Value)); } - // IComparable - /// - /// Compare to another Concentration object - /// - /// The other Concentration cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + // IComparable + /// + /// Compare to another Concentration object + /// + /// The other Concentration cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Concentration concentration) + { + return Value.CompareTo(concentration.Value); + } - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + throw new ArgumentException("Object is not a Concentration"); + } - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/ConcentrationInWater.cs b/Source/Meadow.Units/ConcentrationInWater.cs index e27c732..287390f 100644 --- a/Source/Meadow.Units/ConcentrationInWater.cs +++ b/Source/Meadow.Units/ConcentrationInWater.cs @@ -5,427 +5,430 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Concentration in water +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct ConcentrationInWater : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Concentration in water + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct ConcentrationInWater : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The ConcentrationInWater value. + /// Parts Per Million by default. + public ConcentrationInWater(double value, UnitType type = UnitType.PartsPerMillion) { - /// - /// Creates a new `ConcentrationInWater` object. - /// - /// The ConcentrationInWater value. - /// Parts Per Million by default. - public ConcentrationInWater(double value, UnitType type = UnitType.PartsPerMillion) - { - Value = ConcentrationInWaterConversions.Convert(value, type, UnitType.PartsPerMillion); - } - - /// - /// Creates a copy of a `ConcentrationInWater` object. - /// - /// ConcentrationInWater to copy - public ConcentrationInWater(ConcentrationInWater concentration) - { - Value = concentration.Value; - } + Value = ConcentrationInWaterConversions.Convert(value, type, UnitType.PartsPerMillion); + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Creates a copy of a ConcentrationInWater object. + /// + /// ConcentrationInWater to copy + public ConcentrationInWater(ConcentrationInWater concentration) + { + Value = concentration.Value; + } - /// - /// The type of units available to describe the ConcentrationInWater. - /// - public enum UnitType - { - /// - /// Parts per 100 - /// - PartsPerHundred, - /// - /// Parts per 1,000 - /// - PartsPerThousand, - /// - /// Parts per 1,000,000 - /// - PartsPerMillion, - /// - /// Milligrams per liter (mg/L) - /// - MilligramsPerLiter, - /// - /// Grams per cubic meter (g/m^3) - /// - GramsPerCubicMeter, - /// - /// Parts per 1,000,000,000 - /// - PartsPerBillion, - /// - /// Micrograms per liter (ug/L) - /// - MicrogramsPerLiter, - /// - /// Parts per 1,000,000,000,000 - /// - PartsPerTrillion, - } + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the ConcentrationInWater. + /// + public enum UnitType + { /// - /// Get ConcentrationInWater in parts per 100 - /// - public double PartsPerHundred => From(UnitType.PartsPerHundred); - /// - /// Get ConcentrationInWater in parts per 1000 + /// Parts per 100 /// - public double PartsPerThousand => From(UnitType.PartsPerThousand); + PartsPerHundred, /// - /// Get ConcentrationInWater in parts per 1,000,000 + /// Parts per 1,000 /// - public double PartsPerMillion => From(UnitType.PartsPerMillion); + PartsPerThousand, /// - /// Get ConcentrationInWater in parts per 1,000,000,000 + /// Parts per 1,000,000 /// - public double PartsPerBillion => From(UnitType.PartsPerBillion); + PartsPerMillion, /// - /// Get ConcentrationInWater in parts per 1,000,000,000,000 + /// Milligrams per liter (mg/L) /// - public double PartsPerTrillion => From(UnitType.PartsPerTrillion); + MilligramsPerLiter, /// - /// Get ConcentrationInWater in Grams Per Cubic Meter + /// Grams per cubic meter (g/m^3) /// - public double GramsPerCubicMeter => From(UnitType.GramsPerCubicMeter); + GramsPerCubicMeter, /// - /// Get ConcentrationInWater in Micrograms Per Liter + /// Parts per 1,000,000,000 /// - public double MicrogramsPerLiter => From(UnitType.MicrogramsPerLiter); + PartsPerBillion, /// - /// Get ConcentrationInWater in Milligrams Per Liter + /// Micrograms per liter (ug/L) /// - public double MilligramsPerLiter => From(UnitType.MilligramsPerLiter); - + MicrogramsPerLiter, /// - /// Get ConcentrationInWater for a specific unit + /// Parts per 1,000,000,000,000 /// - /// unit - /// value as a double - [Pure] - public double From(UnitType convertTo) - { - return ConcentrationInWaterConversions.Convert(Value, UnitType.PartsPerMillion, convertTo); - } + PartsPerTrillion, + } - /// - /// Compare two ConcentrationInWater objects - /// - /// - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Concentration)obj); - } + /// + /// Get ConcentrationInWater in parts per 100 + /// + public double PartsPerHundred => From(UnitType.PartsPerHundred); + /// + /// Get ConcentrationInWater in parts per 1000 + /// + public double PartsPerThousand => From(UnitType.PartsPerThousand); + /// + /// Get ConcentrationInWater in parts per 1,000,000 + /// + public double PartsPerMillion => From(UnitType.PartsPerMillion); + /// + /// Get ConcentrationInWater in parts per 1,000,000,000 + /// + public double PartsPerBillion => From(UnitType.PartsPerBillion); + /// + /// Get ConcentrationInWater in parts per 1,000,000,000,000 + /// + public double PartsPerTrillion => From(UnitType.PartsPerTrillion); + /// + /// Get ConcentrationInWater in Grams Per Cubic Meter + /// + public double GramsPerCubicMeter => From(UnitType.GramsPerCubicMeter); + /// + /// Get ConcentrationInWater in Micrograms Per Liter + /// + public double MicrogramsPerLiter => From(UnitType.MicrogramsPerLiter); + /// + /// Get ConcentrationInWater in Milligrams Per Liter + /// + public double MilligramsPerLiter => From(UnitType.MilligramsPerLiter); - /// - /// Get hash of ConcentrationInWater object - /// - /// int32 hash of object - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Concentration(ushort value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(short value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(uint value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(long value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(int value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(float value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(double value) => new Concentration(value); - //[Pure] public static implicit operator Concentration(decimal value) => new Concentration((double)value); - - // Comparison - /// - /// Compare to another ConcentrationInWater objects - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(ConcentrationInWater other) => Value == other.Value; + /// + /// Get ConcentrationInWater for a specific unit + /// + /// unit + /// value as a double + [Pure] + public double From(UnitType convertTo) + { + return ConcentrationInWaterConversions.Convert(Value, UnitType.PartsPerMillion, convertTo); + } - /// - /// Equals operator to compare two ConcentrationInWater objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(ConcentrationInWater left, ConcentrationInWater right) => Equals(left.Value, right.Value); + /// + /// Compare two ConcentrationInWater objects + /// + /// + /// true if equal + [Pure] + public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Not equals operator to compare two ConcentrationInWater objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(ConcentrationInWater left, ConcentrationInWater right) => !Equals(left.Value, right.Value); + /// + /// Get hash of ConcentrationInWater object + /// + /// int32 hash of object + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Concentration(ushort value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(short value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(uint value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(long value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(int value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(float value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(double value) => new Concentration(value); + //[Pure] public static implicit operator Concentration(decimal value) => new Concentration((double)value); + + // Comparison + /// + /// Compare to another ConcentrationInWater objects + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(ConcentrationInWater other) => Value == other.Value; - /// - /// Compare to another ConcentrationInWater object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(ConcentrationInWater other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Equals operator to compare two ConcentrationInWater objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(ConcentrationInWater left, ConcentrationInWater right) => Equals(left.Value, right.Value); - /// - /// Less than operator to compare two ConcentrationInWater objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Not equals operator to compare two ConcentrationInWater objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(ConcentrationInWater left, ConcentrationInWater right) => !Equals(left.Value, right.Value); - /// - /// Greater than operator to compare two ConcentrationInWater objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Compare to another ConcentrationInWater object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(ConcentrationInWater other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Less than or equal operator to compare two ConcentrationInWater objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Less than operator to compare two ConcentrationInWater objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Greater than or equal operator to compare two ConcentrationInWater objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Greater than operator to compare two ConcentrationInWater objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - // Math - /// - /// Addition operator to add two ConcentrationInWater objects - /// - /// left value - /// right value - /// A new ConcentrationInWater object with a value of left + right - [Pure] public static ConcentrationInWater operator +(ConcentrationInWater left, ConcentrationInWater right) => new(left.Value + right.Value); + /// + /// Less than or equal operator to compare two ConcentrationInWater objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Subtraction operator to subtract two ConcentrationInWater objects - /// - /// left value - /// right value - /// A new ConcentrationInWater object with a value of left - right - [Pure] public static ConcentrationInWater operator -(ConcentrationInWater left, ConcentrationInWater right) => new(left.Value - right.Value); + /// + /// Greater than or equal operator to compare two ConcentrationInWater objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(ConcentrationInWater left, ConcentrationInWater right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new ConcentrationInWater object with a value of value multiplied by the operand - [Pure] public static ConcentrationInWater operator *(ConcentrationInWater value, double operand) => new(value.Value * operand); + // Math + /// + /// Addition operator to add two ConcentrationInWater objects + /// + /// left value + /// right value + /// A new ConcentrationInWater object with a value of left + right + [Pure] public static ConcentrationInWater operator +(ConcentrationInWater left, ConcentrationInWater right) => new(left.Value + right.Value); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new ConcentrationInWater object with a value of value divided by the operand - [Pure] public static ConcentrationInWater operator /(ConcentrationInWater value, double operand) => new(value.Value / operand); + /// + /// Subtraction operator to subtract two ConcentrationInWater objects + /// + /// left value + /// right value + /// A new ConcentrationInWater object with a value of left - right + [Pure] public static ConcentrationInWater operator -(ConcentrationInWater left, ConcentrationInWater right) => new(left.Value - right.Value); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public ConcentrationInWater Abs() { return new ConcentrationInWater(Math.Abs(this.Value)); } + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new ConcentrationInWater object with a value of value multiplied by the operand + [Pure] public static ConcentrationInWater operator *(ConcentrationInWater value, double operand) => new(value.Value * operand); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new ConcentrationInWater object with a value of value divided by the operand + [Pure] public static ConcentrationInWater operator /(ConcentrationInWater value, double operand) => new(value.Value / operand); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public ConcentrationInWater Abs() { return new ConcentrationInWater(Math.Abs(Value)); } - // IComparable - /// - /// Compare to another ConcentrationInWater object - /// - /// The other ConcentrationInWater cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + // IComparable + /// + /// Compare to another ConcentrationInWater object + /// + /// The other ConcentrationInWater cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is ConcentrationInWater concentrationInWater) + { + return Value.CompareTo(concentrationInWater.Value); + } - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + throw new ArgumentException("Object is not a ConcentrationInWater"); + } - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Conductivity.cs b/Source/Meadow.Units/Conductivity.cs index 1de9940..7b02e8d 100644 --- a/Source/Meadow.Units/Conductivity.cs +++ b/Source/Meadow.Units/Conductivity.cs @@ -5,416 +5,418 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents a value of Electric Conductivity. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Conductivity : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents a value of Electric Conductivity. + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Conductivity : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Conductivity value. + /// Amps by default. + public Conductivity(double value, UnitType type = UnitType.SiemensPerCentimeter) { - /// - /// Creates a new `Conductivity` object. - /// - /// The Conductivity value. - /// Amps by default. - public Conductivity(double value, UnitType type = UnitType.SiemensPerCentimeter) - { - Value = ConductivityConversions.Convert(value, type, UnitType.SiemensPerCentimeter); - } + Value = ConductivityConversions.Convert(value, type, UnitType.SiemensPerCentimeter); + } - /// - /// Creates a new `Conductivity` object from an existing Conductivity object - /// - /// - public Conductivity(Conductivity Conductivity) - { - Value = Conductivity.Value; - } + /// + /// Creates a new object from an existing Conductivity object + /// + /// + public Conductivity(Conductivity Conductivity) + { + Value = Conductivity.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the Conductivity. + /// + public enum UnitType + { /// - /// The type of units available to describe the Conductivity. + /// Siemens per centimeter (S/cm) /// - public enum UnitType - { - /// - /// Siemens per centimeter (S/cm) - /// - SiemensPerCentimeter, - /// - /// Siemens per - /// - SiemensPerMeter, - /// - /// Milli siemens per centimeter (mS/cm) - /// - MilliSiemensPerCentimeter, - /// - /// Milli siemens per meter (mS/m) - /// - MilliSiemensPerMeter, - /// - /// Micro siemens per centimeter (uS/vm) - /// - MicroSiemensPerCentimeter, - /// - /// Micro siemens per meter (uS/m) - /// - MicroSiemensPerMeter, - } - + SiemensPerCentimeter, /// - /// Get Conductivity in Siemens Per Centimeter + /// Siemens per /// - public double SiemensPerCentimeter => From(UnitType.SiemensPerCentimeter); - + SiemensPerMeter, /// - /// Get Conductivity in Siemens Per Meter + /// Milli siemens per centimeter (mS/cm) /// - public double SiemensPerMeter => From(UnitType.SiemensPerMeter); - + MilliSiemensPerCentimeter, /// - /// Get Conductivity in MilliSiemens Per Centimeter + /// Milli siemens per meter (mS/m) /// - public double MilliSiemensPerCentimeter => From(UnitType.MilliSiemensPerCentimeter); - + MilliSiemensPerMeter, /// - /// Get Conductivity in MilliSiemens Per Meter + /// Micro siemens per centimeter (uS/vm) /// - public double MilliSiemensPerMeter => From(UnitType.MilliSiemensPerMeter); - + MicroSiemensPerCentimeter, /// - /// Get Conductivity in Micro Siemens Per Centimeter + /// Micro siemens per meter (uS/m) /// - public double MicroSiemensPerCentimeter => From(UnitType.MicroSiemensPerCentimeter); + MicroSiemensPerMeter, + } - /// - /// Get Conductivity in Micro Siemens PerMeter - /// - public double MicroSiemensPerMeter => From(UnitType.MicroSiemensPerMeter); + /// + /// Get Conductivity in Siemens Per Centimeter + /// + public double SiemensPerCentimeter => From(UnitType.SiemensPerCentimeter); - /// - /// Convert to a specific unit - /// - /// the unit to convert to - /// - [Pure] - public double From(UnitType convertTo) - { - return ConductivityConversions.Convert(Value, Conductivity.UnitType.SiemensPerCentimeter, convertTo); - } + /// + /// Get Conductivity in Siemens Per Meter + /// + public double SiemensPerMeter => From(UnitType.SiemensPerMeter); - /// - /// Compare to another Conductivity object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Conductivity)obj); - } + /// + /// Get Conductivity in MilliSiemens Per Centimeter + /// + public double MilliSiemensPerCentimeter => From(UnitType.MilliSiemensPerCentimeter); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Conductivity(ushort value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(short value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(uint value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(long value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(int value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(float value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(double value) => new Conductivity(value); - //[Pure] public static implicit operator Conductivity(decimal value) => new Conductivity((double)value); - - // Comparison - /// - /// Compare to another Conductivity object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Conductivity other) => Value == other.Value; + /// + /// Get Conductivity in MilliSiemens Per Meter + /// + public double MilliSiemensPerMeter => From(UnitType.MilliSiemensPerMeter); - /// - /// Equals operator to compare two Conductivity objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Conductivity left, Conductivity right) => Equals(left.Value, right.Value); + /// + /// Get Conductivity in Micro Siemens Per Centimeter + /// + public double MicroSiemensPerCentimeter => From(UnitType.MicroSiemensPerCentimeter); - /// - /// Not equals operator to compare two Conductivity objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Conductivity left, Conductivity right) => !Equals(left.Value, right.Value); + /// + /// Get Conductivity in Micro Siemens PerMeter + /// + public double MicroSiemensPerMeter => From(UnitType.MicroSiemensPerMeter); - /// - /// Compare to another Conductivity object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Conductivity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Convert to a specific unit + /// + /// the unit to convert to + /// + [Pure] + public double From(UnitType convertTo) + { + return ConductivityConversions.Convert(Value, UnitType.SiemensPerCentimeter, convertTo); + } - /// - /// Less than operator to compare two Conductivity objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Compare to another Conductivity object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Greater than operator to compare two Conductivity objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Conductivity(ushort value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(short value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(uint value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(long value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(int value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(float value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(double value) => new Conductivity(value); + //[Pure] public static implicit operator Conductivity(decimal value) => new Conductivity((double)value); + + // Comparison + /// + /// Compare to another Conductivity object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Conductivity other) => Value == other.Value; - /// - /// Less than or equal operator to compare two Conductivity objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Equals operator to compare two Conductivity objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Conductivity left, Conductivity right) => Equals(left.Value, right.Value); - /// - /// Greater than or equal operator to compare two Conductivity objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Not equals operator to compare two Conductivity objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Conductivity left, Conductivity right) => !Equals(left.Value, right.Value); - // Math - /// - /// Addition operator to add two Conductivity objects - /// - /// left value - /// right value - /// A new Conductivity object with a value of left + right - [Pure] public static Conductivity operator +(Conductivity left, Conductivity right) => new(left.Value + right.Value); + /// + /// Compare to another Conductivity object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Conductivity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Subtraction operator to subtract two Conductivity objects - /// - /// left value - /// right value - /// A new Conductivity object with a value of left - right - [Pure] public static Conductivity operator -(Conductivity left, Conductivity right) => new(left.Value - right.Value); + /// + /// Less than operator to compare two Conductivity objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Conductivity object with a value of value multiplied by the operand - [Pure] public static Conductivity operator *(Conductivity value, double operand) => new(value.Value * operand); + /// + /// Greater than operator to compare two Conductivity objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Conductivity object with a value of value divided by the operand - [Pure] public static Conductivity operator /(Conductivity value, double operand) => new(value.Value / operand); + /// + /// Less than or equal operator to compare two Conductivity objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Conductivity Abs() { return new Conductivity(Math.Abs(this.Value)); } + /// + /// Greater than or equal operator to compare two Conductivity objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Conductivity left, Conductivity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + // Math + /// + /// Addition operator to add two Conductivity objects + /// + /// left value + /// right value + /// A new Conductivity object with a value of left + right + [Pure] public static Conductivity operator +(Conductivity left, Conductivity right) => new(left.Value + right.Value); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Subtraction operator to subtract two Conductivity objects + /// + /// left value + /// right value + /// A new Conductivity object with a value of left - right + [Pure] public static Conductivity operator -(Conductivity left, Conductivity right) => new(left.Value - right.Value); - // IComparable - /// - /// Compare to another Conductivity object - /// - /// The other Conductivity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Conductivity object with a value of value multiplied by the operand + [Pure] public static Conductivity operator *(Conductivity value, double operand) => new(value.Value * operand); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Conductivity object with a value of value divided by the operand + [Pure] public static Conductivity operator /(Conductivity value, double operand) => new(value.Value / operand); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Conductivity Abs() { return new Conductivity(Math.Abs(Value)); } - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + // IComparable + /// + /// Compare to another Conductivity object + /// + /// The other Conductivity cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Conductivity conductivity) + { + return Value.CompareTo(conductivity.Value); + } - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + throw new ArgumentException("Object is not a Conductivity"); + } - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to type - /// - /// conversion unit type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to type + /// + /// conversion unit type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/AbsoluteHumidityConversions.cs b/Source/Meadow.Units/Conversions/AbsoluteHumidityConversions.cs index 08c1093..ecdd0a9 100644 --- a/Source/Meadow.Units/Conversions/AbsoluteHumidityConversions.cs +++ b/Source/Meadow.Units/Conversions/AbsoluteHumidityConversions.cs @@ -1,21 +1,20 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class AbsoluteHumidityConversions { - internal static class AbsoluteHumidityConversions + public static double Convert(double value, AbsoluteHumidity.UnitType from, AbsoluteHumidity.UnitType to) { - public static double Convert(double value, AbsoluteHumidity.UnitType from, AbsoluteHumidity.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * humidityConversions[(int)to] / humidityConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] humidityConversions = - { - 1000.0,//grams per cubic meter - 1.0,//kilograms per cubic meter - }; + return value * humidityConversions[(int)to] / humidityConversions[(int)from]; } + + //must align to enum + private static readonly double[] humidityConversions = + { + 1000.0,//grams per cubic meter + 1.0,//kilograms per cubic meter + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/AccelerationConversions.cs b/Source/Meadow.Units/Conversions/AccelerationConversions.cs index c8e9267..67b2514 100644 --- a/Source/Meadow.Units/Conversions/AccelerationConversions.cs +++ b/Source/Meadow.Units/Conversions/AccelerationConversions.cs @@ -1,26 +1,25 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class AccelerationConversions { - internal static class AccelerationConversions + public static double Convert(double value, Acceleration.UnitType from, Acceleration.UnitType to) { - public static double Convert(double value, Acceleration.UnitType from, Acceleration.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * accelConversions[(int)to] / accelConversions[(int)from]; + return value; } - - private static readonly double[] accelConversions = - { - 1.0, //meters per second squared - 100.0, //cm per s squared - 100.0, // gal (galileo) - 100000.0, // milligal - 0.0001019716213, //milli gravity - 0.1019716213,//gravity - 3.280839895,//feet per second squared - 39.37007874,//inches per s squared - }; + return value * accelConversions[(int)to] / accelConversions[(int)from]; } + + private static readonly double[] accelConversions = + { + 1.0, //meters per second squared + 100.0, //cm per s squared + 100.0, // gal (galileo) + 100000.0, // milligal + 0.0001019716213, //milli gravity + 0.1019716213,//gravity + 3.280839895,//feet per second squared + 39.37007874,//inches per s squared + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/AngleConversions.cs b/Source/Meadow.Units/Conversions/AngleConversions.cs index ac77bc1..4de4626 100644 --- a/Source/Meadow.Units/Conversions/AngleConversions.cs +++ b/Source/Meadow.Units/Conversions/AngleConversions.cs @@ -1,27 +1,26 @@ using System; -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class AngleConversions { - internal static class AngleConversions + public static double Convert(double value, Angle.UnitType from, Angle.UnitType to) { - public static double Convert(double value, Angle.UnitType from, Angle.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * angleConversions[(int)to] / angleConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] angleConversions = - { - 1, //revolution - 360, //degrees - 2*Math.PI, //radians - 400, //gradians - 21600, //minutes - 1296000, //seconds - }; + return value * angleConversions[(int)to] / angleConversions[(int)from]; } + + //must align to enum + private static readonly double[] angleConversions = + { + 1, //revolution + 360, //degrees + 2*Math.PI, //radians + 400, //gradians + 21600, //minutes + 1296000, //seconds + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/AngularAccelerationConversion.cs b/Source/Meadow.Units/Conversions/AngularAccelerationConversion.cs index 7c4e799..0ab5d09 100644 --- a/Source/Meadow.Units/Conversions/AngularAccelerationConversion.cs +++ b/Source/Meadow.Units/Conversions/AngularAccelerationConversion.cs @@ -1,25 +1,24 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class AngularAccelerationConversions { - internal static class AngularAccelerationConversions + public static double Convert(double value, AngularAcceleration.UnitType from, AngularAcceleration.UnitType to) { - public static double Convert(double value, AngularAcceleration.UnitType from, AngularAcceleration.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * angularAccelerationConversions[(int)to] / angularAccelerationConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] angularAccelerationConversions = - { - 1, //revolutions per second^2 - 3600, //revolutions per minute^2 - 6.28318530717959, //radians per second^2 - 22619.4671058465, //radians per minute^2 - 360, //degrees per second^2 - 1296000, //degrees per minute^2 - }; + return value * angularAccelerationConversions[(int)to] / angularAccelerationConversions[(int)from]; } + + //must align to enum + private static readonly double[] angularAccelerationConversions = + { + 1, //revolutions per second^2 + 3600, //revolutions per minute^2 + 6.28318530717959, //radians per second^2 + 22619.4671058465, //radians per minute^2 + 360, //degrees per second^2 + 1296000, //degrees per minute^2 + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/AngularVelocityConversion.cs b/Source/Meadow.Units/Conversions/AngularVelocityConversion.cs index 166b163..12ca55e 100644 --- a/Source/Meadow.Units/Conversions/AngularVelocityConversion.cs +++ b/Source/Meadow.Units/Conversions/AngularVelocityConversion.cs @@ -1,25 +1,24 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class AngularVelocityConversions { - internal static class AngularVelocityConversions + public static double Convert(double value, AngularVelocity.UnitType from, AngularVelocity.UnitType to) { - public static double Convert(double value, AngularVelocity.UnitType from, AngularVelocity.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * angularVelocityConversions[(int)to] / angularVelocityConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] angularVelocityConversions = - { - 1, //revolutions per second - 60, //revolutions per minute - 6.28318530717959, //radians per second - 376.991118430775, //radians per minute - 360, //degrees per second - 21600, //degrees per minute - }; + return value * angularVelocityConversions[(int)to] / angularVelocityConversions[(int)from]; } + + //must align to enum + private static readonly double[] angularVelocityConversions = + { + 1, //revolutions per second + 60, //revolutions per minute + 6.28318530717959, //radians per second + 376.991118430775, //radians per minute + 360, //degrees per second + 21600, //degrees per minute + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ApparentPowerConversions.cs b/Source/Meadow.Units/Conversions/ApparentPowerConversions.cs index 776597b..b2d4399 100644 --- a/Source/Meadow.Units/Conversions/ApparentPowerConversions.cs +++ b/Source/Meadow.Units/Conversions/ApparentPowerConversions.cs @@ -1,24 +1,23 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ApparentPowerConversions { - internal static class ApparentPowerConversions + public static double Convert(double value, ApparentPower.UnitType from, ApparentPower.UnitType to) { - public static double Convert(double value, ApparentPower.UnitType from, ApparentPower.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * apparentPowerConversions[(int)to] / apparentPowerConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] apparentPowerConversions = - { - 0.000000001, //Gigavolt ampere - 0.000001, //Megavolt ampere - 0.001, //Kilovolt ampere - 1.0, //Volt ampere - 1000.0, //Millivolt ampere - }; + return value * apparentPowerConversions[(int)to] / apparentPowerConversions[(int)from]; } + + //must align to enum + private static readonly double[] apparentPowerConversions = + { + 0.000000001, //Gigavolt ampere + 0.000001, //Megavolt ampere + 0.001, //Kilovolt ampere + 1.0, //Volt ampere + 1000.0, //Millivolt ampere + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/AzimuthConversions.cs b/Source/Meadow.Units/Conversions/AzimuthConversions.cs index adcc4cf..921c985 100644 --- a/Source/Meadow.Units/Conversions/AzimuthConversions.cs +++ b/Source/Meadow.Units/Conversions/AzimuthConversions.cs @@ -1,83 +1,82 @@ using System; -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +/// +/// Azimuth value conversions +/// +public static class AzimuthConversions { /// - /// Azimuth value conversions + /// Compass 16 Cardinals to degrees /// - public static class AzimuthConversions - { - /// - /// Compass 16 Cardinals to degrees - /// - // To Base (`CompassDegrees`) - public static Func Compass16CardinalsToDegrees = (value) => { - return value switch - { - Azimuth16PointCardinalNames.N => 0d, - Azimuth16PointCardinalNames.NNE => 22.5d, - Azimuth16PointCardinalNames.NE => 45d, - Azimuth16PointCardinalNames.ENE => 67.5d, - Azimuth16PointCardinalNames.E => 90d, - Azimuth16PointCardinalNames.ESE => 112.5d, - Azimuth16PointCardinalNames.SE => 135d, - Azimuth16PointCardinalNames.SSE => 157.5d, - Azimuth16PointCardinalNames.S => 180d, - Azimuth16PointCardinalNames.SSW => 202.5d, - Azimuth16PointCardinalNames.SW => 225d, - Azimuth16PointCardinalNames.WSW => 247.5d, - Azimuth16PointCardinalNames.W => 270d, - Azimuth16PointCardinalNames.WNW => 292.5d, - Azimuth16PointCardinalNames.NW => 315d, - Azimuth16PointCardinalNames.NNW => 337.5d, - _ => 0d, - }; + // To Base (`CompassDegrees`) + public static Func Compass16CardinalsToDegrees = (value) => { + return value switch + { + Azimuth16PointCardinalNames.N => 0d, + Azimuth16PointCardinalNames.NNE => 22.5d, + Azimuth16PointCardinalNames.NE => 45d, + Azimuth16PointCardinalNames.ENE => 67.5d, + Azimuth16PointCardinalNames.E => 90d, + Azimuth16PointCardinalNames.ESE => 112.5d, + Azimuth16PointCardinalNames.SE => 135d, + Azimuth16PointCardinalNames.SSE => 157.5d, + Azimuth16PointCardinalNames.S => 180d, + Azimuth16PointCardinalNames.SSW => 202.5d, + Azimuth16PointCardinalNames.SW => 225d, + Azimuth16PointCardinalNames.WSW => 247.5d, + Azimuth16PointCardinalNames.W => 270d, + Azimuth16PointCardinalNames.WNW => 292.5d, + Azimuth16PointCardinalNames.NW => 315d, + Azimuth16PointCardinalNames.NNW => 337.5d, + _ => 0d, }; + }; - /// - /// Degrees to compass cardinal names - /// - public static Func DegressToCompass16PointCardinalName = (value) => { - /* - N 348.75 - 11.25 - NNE 11.25 - 33.75 - NE 33.75 - 56.25 - ENE 56.25 - 78.75 - E 78.75 - 101.25 - ESE 101.25 - 123.75 - SE 123.75 - 146.25 - SSE 146.25 - 168.75 - S 168.75 - 191.25 - SSW 191.25 - 213.75 - SW 213.75 - 236.25 - WSW 236.25 - 258.75 - W 258.75 - 281.25 - WNW 281.25 - 303.75 - NW 303.75 - 326.25 - NNW 326.25 - 348.75 - */ + /// + /// Degrees to compass cardinal names + /// + public static Func DegressToCompass16PointCardinalName = (value) => { + /* + N 348.75 - 11.25 + NNE 11.25 - 33.75 + NE 33.75 - 56.25 + ENE 56.25 - 78.75 + E 78.75 - 101.25 + ESE 101.25 - 123.75 + SE 123.75 - 146.25 + SSE 146.25 - 168.75 + S 168.75 - 191.25 + SSW 191.25 - 213.75 + SW 213.75 - 236.25 + WSW 236.25 - 258.75 + W 258.75 - 281.25 + WNW 281.25 - 303.75 + NW 303.75 - 326.25 + NNW 326.25 - 348.75 + */ - return value switch - { - double v when (v >= 348.75f && v < 11.25) => Azimuth16PointCardinalNames.N, - double v when (v >= 11.25f && v < 33.75) => Azimuth16PointCardinalNames.NNE, - double v when (v >= 33.75f && v < 56.25) => Azimuth16PointCardinalNames.NE, - double v when (v >= 56.25f && v < 78.75) => Azimuth16PointCardinalNames.ENE, - double v when (v >= 78.75f && v < 101.25) => Azimuth16PointCardinalNames.E, - double v when (v >= 101.25f && v < 123.75) => Azimuth16PointCardinalNames.ESE, - double v when (v >= 123.75f && v < 146.25) => Azimuth16PointCardinalNames.SE, - double v when (v >= 146.25f && v < 168.75) => Azimuth16PointCardinalNames.SSE, - double v when (v >= 168.75f && v < 191.25) => Azimuth16PointCardinalNames.S, - double v when (v >= 191.25f && v < 213.75) => Azimuth16PointCardinalNames.SSW, - double v when (v >= 213.75f && v < 236.25) => Azimuth16PointCardinalNames.SW, - double v when (v >= 236.25f && v < 258.75) => Azimuth16PointCardinalNames.WSW, - double v when (v >= 258.75f && v < 281.25) => Azimuth16PointCardinalNames.W, - double v when (v >= 281.25f && v < 303.75) => Azimuth16PointCardinalNames.WNW, - double v when (v >= 303.75f && v < 326.25) => Azimuth16PointCardinalNames.NW, - double v when (v >= 326.25f && v < 348.75) => Azimuth16PointCardinalNames.NNW, - _ => Azimuth16PointCardinalNames.N, - }; + return value switch + { + double v when (v >= 348.75f && v < 11.25) => Azimuth16PointCardinalNames.N, + double v when (v >= 11.25f && v < 33.75) => Azimuth16PointCardinalNames.NNE, + double v when (v >= 33.75f && v < 56.25) => Azimuth16PointCardinalNames.NE, + double v when (v >= 56.25f && v < 78.75) => Azimuth16PointCardinalNames.ENE, + double v when (v >= 78.75f && v < 101.25) => Azimuth16PointCardinalNames.E, + double v when (v >= 101.25f && v < 123.75) => Azimuth16PointCardinalNames.ESE, + double v when (v >= 123.75f && v < 146.25) => Azimuth16PointCardinalNames.SE, + double v when (v >= 146.25f && v < 168.75) => Azimuth16PointCardinalNames.SSE, + double v when (v >= 168.75f && v < 191.25) => Azimuth16PointCardinalNames.S, + double v when (v >= 191.25f && v < 213.75) => Azimuth16PointCardinalNames.SSW, + double v when (v >= 213.75f && v < 236.25) => Azimuth16PointCardinalNames.SW, + double v when (v >= 236.25f && v < 258.75) => Azimuth16PointCardinalNames.WSW, + double v when (v >= 258.75f && v < 281.25) => Azimuth16PointCardinalNames.W, + double v when (v >= 281.25f && v < 303.75) => Azimuth16PointCardinalNames.WNW, + double v when (v >= 303.75f && v < 326.25) => Azimuth16PointCardinalNames.NW, + double v when (v >= 326.25f && v < 348.75) => Azimuth16PointCardinalNames.NNW, + _ => Azimuth16PointCardinalNames.N, }; + }; - } -} +} \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ConcentrationConversions.cs b/Source/Meadow.Units/Conversions/ConcentrationConversions.cs index fd1d7f3..08561d2 100644 --- a/Source/Meadow.Units/Conversions/ConcentrationConversions.cs +++ b/Source/Meadow.Units/Conversions/ConcentrationConversions.cs @@ -1,23 +1,22 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ConcentrationConversions { - internal static class ConcentrationConversions + public static double Convert(double value, Concentration.UnitType from, Concentration.UnitType to) { - public static double Convert(double value, Concentration.UnitType from, Concentration.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - - return value * concentrationConversions[(int)to] / concentrationConversions[(int)from]; + return value; } - private static readonly double[] concentrationConversions = - { - 1.0,//pph - 10.0,//ppt - 10000.0, // ppm - 10000000.0, // ppb - }; + return value * concentrationConversions[(int)to] / concentrationConversions[(int)from]; } + + private static readonly double[] concentrationConversions = + { + 1.0,//pph + 10.0,//ppt + 10000.0, // ppm + 10000000.0, // ppb + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ConcentrationInWaterConversions.cs b/Source/Meadow.Units/Conversions/ConcentrationInWaterConversions.cs index 0fb5cea..bdbf55f 100644 --- a/Source/Meadow.Units/Conversions/ConcentrationInWaterConversions.cs +++ b/Source/Meadow.Units/Conversions/ConcentrationInWaterConversions.cs @@ -1,27 +1,26 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ConcentrationInWaterConversions { - internal static class ConcentrationInWaterConversions + public static double Convert(double value, ConcentrationInWater.UnitType from, ConcentrationInWater.UnitType to) { - public static double Convert(double value, ConcentrationInWater.UnitType from, ConcentrationInWater.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - - return value * concentrationConversions[(int)to] / concentrationConversions[(int)from]; + return value; } - private static readonly double[] concentrationConversions = - { - 1.0,//pph - 10.0,//ppt - 10000.0, // ppm - 10000.0, // mg/L - 10000.0, // g/m^3 - 10000000.0, // ppb - 10000000.0, // ug/L - 10000000000.0, // ppt - }; + return value * concentrationConversions[(int)to] / concentrationConversions[(int)from]; } + + private static readonly double[] concentrationConversions = + { + 1.0,//pph + 10.0,//ppt + 10000.0, // ppm + 10000.0, // mg/L + 10000.0, // g/m^3 + 10000000.0, // ppb + 10000000.0, // ug/L + 10000000000.0, // ppt + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ConductivityConversions.cs b/Source/Meadow.Units/Conversions/ConductivityConversions.cs index b7473bf..5387adf 100644 --- a/Source/Meadow.Units/Conversions/ConductivityConversions.cs +++ b/Source/Meadow.Units/Conversions/ConductivityConversions.cs @@ -1,25 +1,24 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ConductivityConversions { - internal static class ConductivityConversions + public static double Convert(double value, Conductivity.UnitType from, Conductivity.UnitType to) { - public static double Convert(double value, Conductivity.UnitType from, Conductivity.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * conductivityConversions[(int)to] / conductivityConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] conductivityConversions = - { - 1, //siemen per cm - 100, //siemen per m - 1000, //milli siemen per cm - 100000, //milli siemen per m - 1000000, //micro siemen per cm - 100000000, //micro siemen per m - }; + return value * conductivityConversions[(int)to] / conductivityConversions[(int)from]; } + + //must align to enum + private static readonly double[] conductivityConversions = + { + 1, //siemen per cm + 100, //siemen per m + 1000, //milli siemen per cm + 100000, //milli siemen per m + 1000000, //micro siemen per cm + 100000000, //micro siemen per m + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/CurrentConversions.cs b/Source/Meadow.Units/Conversions/CurrentConversions.cs index 332221e..8d1b9e5 100644 --- a/Source/Meadow.Units/Conversions/CurrentConversions.cs +++ b/Source/Meadow.Units/Conversions/CurrentConversions.cs @@ -1,25 +1,24 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class CurrentConversions { - internal static class CurrentConversions + public static double Convert(double value, Current.UnitType from, Current.UnitType to) { - public static double Convert(double value, Current.UnitType from, Current.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * currentConversions[(int)to] / currentConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] currentConversions = - { - 1, //Amps - 1000, //Milliamps - 1000000, //Microamps - 0.001, //Kiloamps - 0.000001, //Megaamps - 0.000000001, //Gigaamps - }; + return value * currentConversions[(int)to] / currentConversions[(int)from]; } + + //must align to enum + private static readonly double[] currentConversions = + { + 1, //Amps + 1000, //Milliamps + 1000000, //Microamps + 0.001, //Kiloamps + 0.000001, //Megaamps + 0.000000001, //Gigaamps + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/DensityConversions.cs b/Source/Meadow.Units/Conversions/DensityConversions.cs index a8651c9..4c70bed 100644 --- a/Source/Meadow.Units/Conversions/DensityConversions.cs +++ b/Source/Meadow.Units/Conversions/DensityConversions.cs @@ -1,30 +1,29 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class DensityConversions { - internal static class DensityConversions + public static double Convert(double value, Density.UnitType from, Density.UnitType to) { - public static double Convert(double value, Density.UnitType from, Density.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * densityConversions[(int)to] / densityConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] densityConversions = - { - 1000000000,//microgram m^3 - 1000000,//milligram m^3 - 0.001,//gram cm^3 - 1000,//gram m^3 - 1.0,//gram/l - 1.0,//kg m^3 - 0.000578036672,//ounce inch - 0.99884736919,//ounce ft - 0.000036127292,//pound /inch - 0.062427960576,//pound /ft^3 - 0.0010018032458,//water (20) - }; + return value * densityConversions[(int)to] / densityConversions[(int)from]; } + + //must align to enum + private static readonly double[] densityConversions = + { + 1000000000,//microgram m^3 + 1000000,//milligram m^3 + 0.001,//gram cm^3 + 1000,//gram m^3 + 1.0,//gram/l + 1.0,//kg m^3 + 0.000578036672,//ounce inch + 0.99884736919,//ounce ft + 0.000036127292,//pound /inch + 0.062427960576,//pound /ft^3 + 0.0010018032458,//water (20) + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/DigitalStorageConversions.cs b/Source/Meadow.Units/Conversions/DigitalStorageConversions.cs index fe5b3e9..2aa4c72 100644 --- a/Source/Meadow.Units/Conversions/DigitalStorageConversions.cs +++ b/Source/Meadow.Units/Conversions/DigitalStorageConversions.cs @@ -1,32 +1,31 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class DigitalStorageConversions { - internal static class DigitalStorageConversions + public static double Convert(double value, DigitalStorage.UnitType from, DigitalStorage.UnitType to) { - public static double Convert(double value, DigitalStorage.UnitType from, DigitalStorage.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * digitalStorageConversions[(int)from] / digitalStorageConversions[(int)to]; + return value; } - - private static readonly double[] digitalStorageConversions = - { - 1.0 / 8.0, // Bit - 1, // Byte - 1_000, // Kilobyte - 1_000_000, // Megabyte - 1_000_000_000, // Gigabyte - 1_000_000_000_000, // Terabyte - 1_000_000_000_000_000, // Petabyte - 1_000_000_000_000_000_000,// Exabyte - 1024, // KiB - 1024 * 8, // Kib - 1024 * 1024, // MiB - 1024 * 1024 * 8, // Mib - 1024 * 1024 * 1024, // GiB - // 1024 * 1024 * 1204 * 8 // Gib OVERFLOW - NOT SUPPORTED - }; + return value * digitalStorageConversions[(int)from] / digitalStorageConversions[(int)to]; } + + private static readonly double[] digitalStorageConversions = + { + 1.0 / 8.0, // Bit + 1, // Byte + 1_000, // Kilobyte + 1_000_000, // Megabyte + 1_000_000_000, // Gigabyte + 1_000_000_000_000, // Terabyte + 1_000_000_000_000_000, // Petabyte + 1_000_000_000_000_000_000,// Exabyte + 1024, // KiB + 1024 * 8, // Kib + 1024 * 1024, // MiB + 1024 * 1024 * 8, // Mib + 1024 * 1024 * 1024, // GiB + // 1024 * 1024 * 1204 * 8 // Gib OVERFLOW - NOT SUPPORTED + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/EnergyConversions.cs b/Source/Meadow.Units/Conversions/EnergyConversions.cs index eb8b460..2f24f46 100644 --- a/Source/Meadow.Units/Conversions/EnergyConversions.cs +++ b/Source/Meadow.Units/Conversions/EnergyConversions.cs @@ -1,30 +1,29 @@ using System; -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class EnergyConversions { - internal static class EnergyConversions + public static double Convert(double value, Energy.UnitType from, Energy.UnitType to) { - public static double Convert(double value, Energy.UnitType from, Energy.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * energyConversions[(int)to] / energyConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] energyConversions = - { - 0.00094845138281, //BTU - 0.23900573614, //calories (thermo) - 1.0,//joules - 0.00023900573614, //kilocals - 0.001,//kilojoules - 0.000000277777777778, //kilowatt hours - 9.4804342797*Math.Pow(10.0,-9.0), //therms - 0.000277777777778, //watt hours, - 1.0, //watt seconds - }; + return value * energyConversions[(int)to] / energyConversions[(int)from]; } + + //must align to enum + private static readonly double[] energyConversions = + { + 0.00094845138281, //BTU + 0.23900573614, //calories (thermo) + 1.0,//joules + 0.00023900573614, //kilocals + 0.001,//kilojoules + 0.000000277777777778, //kilowatt hours + 9.4804342797*Math.Pow(10.0,-9.0), //therms + 0.000277777777778, //watt hours, + 1.0, //watt seconds + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/FrequencyConversions.cs b/Source/Meadow.Units/Conversions/FrequencyConversions.cs index 6fbae7c..807cd36 100644 --- a/Source/Meadow.Units/Conversions/FrequencyConversions.cs +++ b/Source/Meadow.Units/Conversions/FrequencyConversions.cs @@ -1,23 +1,22 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class FrequencyConversions { - internal static class FrequencyConversions + public static double Convert(double value, Frequency.UnitType from, Frequency.UnitType to) { - public static double Convert(double value, Frequency.UnitType from, Frequency.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * frequencyConversions[(int)to] / frequencyConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] frequencyConversions = - { - 1, //GHz - 1000, //MHz - 1000000, //kHz - 1000000000, //Hz - }; + return value * frequencyConversions[(int)to] / frequencyConversions[(int)from]; } + + //must align to enum + private static readonly double[] frequencyConversions = + { + 1, //GHz + 1000, //MHz + 1000000, //kHz + 1000000000, //Hz + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/IlluminanceConversions.cs b/Source/Meadow.Units/Conversions/IlluminanceConversions.cs index 82df93f..b51df32 100644 --- a/Source/Meadow.Units/Conversions/IlluminanceConversions.cs +++ b/Source/Meadow.Units/Conversions/IlluminanceConversions.cs @@ -1,22 +1,21 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class IlluminanceConversions { - internal static class IlluminanceConversions + public static double Convert(double value, Illuminance.UnitType from, Illuminance.UnitType to) { - public static double Convert(double value, Illuminance.UnitType from, Illuminance.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * illuminanceConversions[(int)to] / illuminanceConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] illuminanceConversions = - { - 0.001, //kilo lux - 1.0, //lux - 0.0930578820026056//foot-candle - }; + return value * illuminanceConversions[(int)to] / illuminanceConversions[(int)from]; } + + //must align to enum + private static readonly double[] illuminanceConversions = + { + 0.001, //kilo lux + 1.0, //lux + 0.0930578820026056//foot-candle + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/LengthConversions.cs b/Source/Meadow.Units/Conversions/LengthConversions.cs index 1569421..ac72cc3 100644 --- a/Source/Meadow.Units/Conversions/LengthConversions.cs +++ b/Source/Meadow.Units/Conversions/LengthConversions.cs @@ -1,31 +1,30 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class LengthConversions { - internal static class LengthConversions + public static double Convert(double value, Length.UnitType from, Length.UnitType to) { - public static double Convert(double value, Length.UnitType from, Length.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * lengthConversions[(int)to] / lengthConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] lengthConversions = - { - 0.001,//km - 1,//meter - 100,//cm - 10,//dm - 1000,//millimeter - 1000000,//micron / micrometer - 1000000000,//nm - 0.00062137119224,//mile - 0.000539956803,//nautical miles - 1.0936132983,//yard - 3.280839895,//ft - 39.37007874,//inch - }; + return value * lengthConversions[(int)to] / lengthConversions[(int)from]; } + + //must align to enum + private static readonly double[] lengthConversions = + { + 0.001,//km + 1,//meter + 100,//cm + 10,//dm + 1000,//millimeter + 1000000,//micron / micrometer + 1000000000,//nm + 0.00062137119224,//mile + 0.000539956803,//nautical miles + 1.0936132983,//yard + 3.280839895,//ft + 39.37007874,//inch + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/MagneticFieldConversions.cs b/Source/Meadow.Units/Conversions/MagneticFieldConversions.cs index 7fd9d35..f22acda 100644 --- a/Source/Meadow.Units/Conversions/MagneticFieldConversions.cs +++ b/Source/Meadow.Units/Conversions/MagneticFieldConversions.cs @@ -1,27 +1,26 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class MagneticFieldConversions { - internal static class MagneticFieldConversions + public static double Convert(double value, MagneticField.UnitType from, MagneticField.UnitType to) { - public static double Convert(double value, MagneticField.UnitType from, MagneticField.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * magneticFieldConversions[(int)to] / magneticFieldConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] magneticFieldConversions = - { - 0.000001, //MegaTesla - 0.001, //KiloTesla - 1.0, //Tesla - 1000.0,//MilliTesla - 1000000.0, //MicroTesla - 1000000000.0, //NanoTesla - 1000000000000.0, //PicoTesla - 10000.0, //Gauss - }; + return value * magneticFieldConversions[(int)to] / magneticFieldConversions[(int)from]; } + + //must align to enum + private static readonly double[] magneticFieldConversions = + { + 0.000001, //MegaTesla + 0.001, //KiloTesla + 1.0, //Tesla + 1000.0,//MilliTesla + 1000000.0, //MicroTesla + 1000000000.0, //NanoTesla + 1000000000000.0, //PicoTesla + 10000.0, //Gauss + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/MassConversions.cs b/Source/Meadow.Units/Conversions/MassConversions.cs index 9643a2f..d9bba29 100644 --- a/Source/Meadow.Units/Conversions/MassConversions.cs +++ b/Source/Meadow.Units/Conversions/MassConversions.cs @@ -1,28 +1,27 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class MassConversions { - internal static class MassConversions + public static double Convert(double value, Mass.UnitType from, Mass.UnitType to) { - public static double Convert(double value, Mass.UnitType from, Mass.UnitType to) + if(from == to) { - if(from == to) - { - return value; - } - return value * massConversions[(int)to] / massConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] massConversions = - { - 1, //grams (identity unit) - 0.001, //kg - 0.0352739619495804129156758082152,//ounces - 0.0022046226218487758072297380134, //pounds - 0.000001, //tonnes metric - 0.0000011023113109243879036148690, //Tons US Short ... 5000 pounds - 0.0000009842065276110606282275616,//Tons UK Long - 15.4323584, // grains - 5, // Carats - }; + return value * massConversions[(int)to] / massConversions[(int)from]; } + + //must align to enum + private static readonly double[] massConversions = + { + 1, //grams (identity unit) + 0.001, //kg + 0.0352739619495804129156758082152,//ounces + 0.0022046226218487758072297380134, //pounds + 0.000001, //tonnes metric + 0.0000011023113109243879036148690, //Tons US Short ... 5000 pounds + 0.0000009842065276110606282275616,//Tons UK Long + 15.4323584, // grains + 5, // Carats + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ParticleDensityConversions.cs b/Source/Meadow.Units/Conversions/ParticleDensityConversions.cs index 63e37a5..b527212 100644 --- a/Source/Meadow.Units/Conversions/ParticleDensityConversions.cs +++ b/Source/Meadow.Units/Conversions/ParticleDensityConversions.cs @@ -1,22 +1,21 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ParticleDensityConversions { - internal static class ParticleDensityConversions + public static double Convert(double value, ParticleDensity.UnitType from, ParticleDensity.UnitType to) { - public static double Convert(double value, ParticleDensity.UnitType from, ParticleDensity.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * densityConversions[(int)to] / densityConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] densityConversions = - { - 1.0,//particles/l - 100.0,//particles/cl - 1000.0,//particles/ml - }; + return value * densityConversions[(int)to] / densityConversions[(int)from]; } + + //must align to enum + private static readonly double[] densityConversions = + { + 1.0,//particles/l + 100.0,//particles/cl + 1000.0,//particles/ml + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/PowerConversions.cs b/Source/Meadow.Units/Conversions/PowerConversions.cs index 750a6be..5feaa40 100644 --- a/Source/Meadow.Units/Conversions/PowerConversions.cs +++ b/Source/Meadow.Units/Conversions/PowerConversions.cs @@ -1,39 +1,38 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class PowerConversions { - internal static class PowerConversions + public static double Convert(double value, Power.UnitType from, Power.UnitType to) { - public static double Convert(double value, Power.UnitType from, Power.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * powerConversions[(int)to] / powerConversions[(int)from]; + return value; } + return value * powerConversions[(int)to] / powerConversions[(int)from]; + } - //must align to enum - private static readonly double[] powerConversions = - { - 0.000000001, //gigawatts - 0.000001, //milliwatts - 0.001, //kwatts - 1.0, //watts - 1000.0, //milliwatt + //must align to enum + private static readonly double[] powerConversions = + { + 0.000000001, //gigawatts + 0.000001, //milliwatts + 0.001, //kwatts + 1.0, //watts + 1000.0, //milliwatt - 0.0013596216173, //HP metric - 0.0013410220924, //HP IT + 0.0013596216173, //HP metric + 0.0013410220924, //HP IT - 0.23884589663, //cals/s - 14.330753798, //cals/min - 859.84522786, //cals/hour - 0.00094781712087, //BTU/sec - 0.056869027252, //BTU/min - 3.4121416351, //BTU/hour + 0.23884589663, //cals/s + 14.330753798, //cals/min + 859.84522786, //cals/hour + 0.00094781712087, //BTU/sec + 0.056869027252, //BTU/min + 3.4121416351, //BTU/hour - 0.73756217557, //ft-pounds/sec - 44.253730534, //ft-pound/min - 2655.2238321, //ft-pounds/hour - 0.00028434513609399,//Tons refrigeration - }; - } + 0.73756217557, //ft-pounds/sec + 44.253730534, //ft-pound/min + 2655.2238321, //ft-pounds/hour + 0.00028434513609399,//Tons refrigeration + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/PressureConversions.cs b/Source/Meadow.Units/Conversions/PressureConversions.cs index 5225f56..d1a0aa9 100644 --- a/Source/Meadow.Units/Conversions/PressureConversions.cs +++ b/Source/Meadow.Units/Conversions/PressureConversions.cs @@ -1,38 +1,37 @@ using System; -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class PressureConversions { - internal static class PressureConversions - { - // To Base (`Pa`) - public static Func PsiToPa = (value) => (value * 6894.7572931783); - public static Func AtToPa = (value) => (value * 101325); - public static Func BarToPa = (value) => (value * 100000); + // To Base (`Pa`) + public static Func PsiToPa = (value) => (value * 6894.7572931783); + public static Func AtToPa = (value) => (value * 101325); + public static Func BarToPa = (value) => (value * 100000); - // From Base - public static Func PaToPsi = (value) => (value / 6894.7572931783); - public static Func PaToAt = (value) => (value / 101325); - public static Func PaToBar = (value) => (value / 100000); + // From Base + public static Func PaToPsi = (value) => (value / 6894.7572931783); + public static Func PaToAt = (value) => (value / 101325); + public static Func PaToBar = (value) => (value / 100000); - public static double Convert(double value, Pressure.UnitType from, Pressure.UnitType to) - { - if (from == to) { - return value; - } - return value * pressureConversions[(int)to] / pressureConversions[(int)from]; + public static double Convert(double value, Pressure.UnitType from, Pressure.UnitType to) + { + if (from == to) { + return value; } + return value * pressureConversions[(int)to] / pressureConversions[(int)from]; + } - //must align to enum - private static readonly double[] pressureConversions = - { - 1,//Bar - 100000, //Pascal, - 14.503773773, //Psi, - 0.9869232667, //StandardAtmosphere, - 1000, // mBar - 1000, // hPa (yes, same as millibar, but both are common) - 100, //kPa - }; + //must align to enum + private static readonly double[] pressureConversions = + { + 1,//Bar + 100000, //Pascal, + 14.503773773, //Psi, + 0.9869232667, //StandardAtmosphere, + 1000, // mBar + 1000, // hPa (yes, same as millibar, but both are common) + 100, //kPa + }; - } -} +} \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ReactiveEnergyConversions.cs b/Source/Meadow.Units/Conversions/ReactiveEnergyConversions.cs index fdc576d..dc6b0d2 100644 --- a/Source/Meadow.Units/Conversions/ReactiveEnergyConversions.cs +++ b/Source/Meadow.Units/Conversions/ReactiveEnergyConversions.cs @@ -1,24 +1,23 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ReactiveEnergyConversions { - internal static class ReactiveEnergyConversions + public static double Convert(double value, ReactiveEnergy.UnitType from, ReactiveEnergy.UnitType to) { - public static double Convert(double value, ReactiveEnergy.UnitType from, ReactiveEnergy.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * reactiveEnergyConversions[(int)to] / reactiveEnergyConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] reactiveEnergyConversions = - { - 0.000000001, //Gigavolt ampere - 0.000001, //Megavolt ampere - 0.001, //Kilovolt ampere - 1.0, //Volt ampere - 1000.0, //Millivolt ampere - }; + return value * reactiveEnergyConversions[(int)to] / reactiveEnergyConversions[(int)from]; } + + //must align to enum + private static readonly double[] reactiveEnergyConversions = + { + 0.000000001, //Gigavolt ampere + 0.000001, //Megavolt ampere + 0.001, //Kilovolt ampere + 1.0, //Volt ampere + 1000.0, //Millivolt ampere + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ReactivePowerConversions.cs b/Source/Meadow.Units/Conversions/ReactivePowerConversions.cs index 56801f8..a053144 100644 --- a/Source/Meadow.Units/Conversions/ReactivePowerConversions.cs +++ b/Source/Meadow.Units/Conversions/ReactivePowerConversions.cs @@ -1,24 +1,23 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ReactivePowerConversions { - internal static class ReactivePowerConversions + public static double Convert(double value, ReactivePower.UnitType from, ReactivePower.UnitType to) { - public static double Convert(double value, ReactivePower.UnitType from, ReactivePower.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * reactivePowerConversions[(int)to] / reactivePowerConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] reactivePowerConversions = - { - 0.000000001, //Gigavolt ampere - 0.000001, //Megavolt ampere - 0.001, //Kilovolt ampere - 1.0, //Volt ampere - 1000.0, //Millivolt ampere - }; + return value * reactivePowerConversions[(int)to] / reactivePowerConversions[(int)from]; } + + //must align to enum + private static readonly double[] reactivePowerConversions = + { + 0.000000001, //Gigavolt ampere + 0.000001, //Megavolt ampere + 0.001, //Kilovolt ampere + 1.0, //Volt ampere + 1000.0, //Millivolt ampere + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/ResistanceConversions.cs b/Source/Meadow.Units/Conversions/ResistanceConversions.cs index bd7c9e3..2e36067 100644 --- a/Source/Meadow.Units/Conversions/ResistanceConversions.cs +++ b/Source/Meadow.Units/Conversions/ResistanceConversions.cs @@ -1,23 +1,22 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class ResistanceConversions { - internal static class ResistanceConversions + public static double Convert(double value, Resistance.UnitType from, Resistance.UnitType to) { - public static double Convert(double value, Resistance.UnitType from, Resistance.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * resistanceConversions[(int)to] / resistanceConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] resistanceConversions = - { - 1000, //MilliOhms - 1, //Ohms - 0.001, //KiloOhms - 0.000001, //MegaOhms - }; + return value * resistanceConversions[(int)to] / resistanceConversions[(int)from]; } + + //must align to enum + private static readonly double[] resistanceConversions = + { + 1000, //MilliOhms + 1, //Ohms + 0.001, //KiloOhms + 0.000001, //MegaOhms + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/SpeedConversions.cs b/Source/Meadow.Units/Conversions/SpeedConversions.cs index 40985b6..a7440e7 100644 --- a/Source/Meadow.Units/Conversions/SpeedConversions.cs +++ b/Source/Meadow.Units/Conversions/SpeedConversions.cs @@ -1,32 +1,31 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class SpeedConversions { - internal static class SpeedConversions + public static double Convert(double value, Speed.UnitType from, Speed.UnitType to) { - public static double Convert(double value, Speed.UnitType from, Speed.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * speedConversions[(int)to] / speedConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] speedConversions = - { - 196850.3937,// ft/min - 3280.839895,// ft/s - 3600.0,// km/h - 60.0,//km/min - 1.0,// km/s - 1943.8444925,// knots - 60000.0,// m/min - 1000.0,// m/s - 2236.9362922064,// mph - 37.2822715344,//mpm - 0.62137119224,//mps - 0.000003335640952,// c - 2.938669958// mach - }; + return value * speedConversions[(int)to] / speedConversions[(int)from]; } + + //must align to enum + private static readonly double[] speedConversions = + { + 196850.3937,// ft/min + 3280.839895,// ft/s + 3600.0,// km/h + 60.0,//km/min + 1.0,// km/s + 1943.8444925,// knots + 60000.0,// m/min + 1000.0,// m/s + 2236.9362922064,// mph + 37.2822715344,//mpm + 0.62137119224,//mps + 0.000003335640952,// c + 2.938669958// mach + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/TempConversions.cs b/Source/Meadow.Units/Conversions/TempConversions.cs index f7feabb..5cd00a9 100644 --- a/Source/Meadow.Units/Conversions/TempConversions.cs +++ b/Source/Meadow.Units/Conversions/TempConversions.cs @@ -1,46 +1,45 @@ using System; -namespace Meadow.Units.Conversions -{ - internal static class TempConversions - { - public static Func FToC = (value) => (value - 32d) * (5d / 9d); - public static Func KToC = (value) => value - 273.15d; - public static Func CToF = (value) => value * (9d / 5d) + 32d; - public static Func CToK = (value) => value + 273.15d; - public static Func FToK = (value) => FToC(value) + 273.15d; - public static Func KToF = (value) => CToF(value - 273.15); +namespace Meadow.Units.Conversions; +internal static class TempConversions +{ + public static Func FToC = (value) => (value - 32d) * (5d / 9d); + public static Func KToC = (value) => value - 273.15d; + public static Func CToF = (value) => value * (9d / 5d) + 32d; + public static Func CToK = (value) => value + 273.15d; + public static Func FToK = (value) => FToC(value) + 273.15d; + public static Func KToF = (value) => CToF(value - 273.15); - public static double Convert(double value, Temperature.UnitType from, Temperature.UnitType to) - { - if (from == to) { return value; } - switch (from) - { - case Temperature.UnitType.Celsius: - return to switch - { - Temperature.UnitType.Kelvin => CToK(value), - Temperature.UnitType.Fahrenheit => CToF(value), - _ => value - }; - case Temperature.UnitType.Fahrenheit: - return to switch - { - Temperature.UnitType.Kelvin => FToK(value), - Temperature.UnitType.Celsius => FToC(value), - _ => value - }; - case Temperature.UnitType.Kelvin: - return to switch - { - Temperature.UnitType.Fahrenheit => KToF(value), - Temperature.UnitType.Celsius => KToC(value), - _ => value - }; - } + public static double Convert(double value, Temperature.UnitType from, Temperature.UnitType to) + { + if (from == to) { return value; } - throw new NotSupportedException(); + switch (from) + { + case Temperature.UnitType.Celsius: + return to switch + { + Temperature.UnitType.Kelvin => CToK(value), + Temperature.UnitType.Fahrenheit => CToF(value), + _ => value + }; + case Temperature.UnitType.Fahrenheit: + return to switch + { + Temperature.UnitType.Kelvin => FToK(value), + Temperature.UnitType.Celsius => FToC(value), + _ => value + }; + case Temperature.UnitType.Kelvin: + return to switch + { + Temperature.UnitType.Fahrenheit => KToF(value), + Temperature.UnitType.Celsius => KToC(value), + _ => value + }; } + + throw new NotSupportedException(); } -} +} \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/TorqueConversions.cs b/Source/Meadow.Units/Conversions/TorqueConversions.cs index d6b7505..9dee84b 100644 --- a/Source/Meadow.Units/Conversions/TorqueConversions.cs +++ b/Source/Meadow.Units/Conversions/TorqueConversions.cs @@ -1,27 +1,26 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class TorqueConversions { - internal static class TorqueConversions + public static double Convert(double value, Torque.UnitType from, Torque.UnitType to) { - public static double Convert(double value, Torque.UnitType from, Torque.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * speedConversions[(int)to] / speedConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] speedConversions = - { - 1.355817952,//newton-meter - 1.0,//foot pound - 0.13825495475,//kilogram-meter - 13.825495475,//kg-cm - 13825.495475,//g-cm - 12.0,//inch pound - 192.0,//inch oz - 13558179.52,//dyne centimeter - }; + return value * speedConversions[(int)to] / speedConversions[(int)from]; } + + //must align to enum + private static readonly double[] speedConversions = + { + 1.355817952,//newton-meter + 1.0,//foot pound + 0.13825495475,//kilogram-meter + 13.825495475,//kg-cm + 13825.495475,//g-cm + 12.0,//inch pound + 192.0,//inch oz + 13558179.52,//dyne centimeter + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/VoltageConversions.cs b/Source/Meadow.Units/Conversions/VoltageConversions.cs index ec60004..b829da7 100644 --- a/Source/Meadow.Units/Conversions/VoltageConversions.cs +++ b/Source/Meadow.Units/Conversions/VoltageConversions.cs @@ -1,26 +1,25 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class VoltageConversions { - internal static class VoltageConversions + public static double Convert(double value, Voltage.UnitType from, Voltage.UnitType to) { - public static double Convert(double value, Voltage.UnitType from, Voltage.UnitType to) - { - if (from == to) { - return value; - } - return value * voltageConversions[(int)to] / voltageConversions[(int)from]; + if (from == to) { + return value; } - - //must align to enum - private static readonly double[] voltageConversions = - { - 1, //Volts - 1000, //Millivolts - 1000000, //Microvolts - 0.001, //Kilovolts - 0.000001, //Megavolts - 0.000000001, //Gigavolts - 299.792458, //Statvolts - 1000000000, //Nanovolts - }; + return value * voltageConversions[(int)to] / voltageConversions[(int)from]; } -} + + //must align to enum + private static readonly double[] voltageConversions = + { + 1, //Volts + 1000, //Millivolts + 1000000, //Microvolts + 0.001, //Kilovolts + 0.000001, //Megavolts + 0.000000001, //Gigavolts + 299.792458, //Statvolts + 1000000000, //Nanovolts + }; +} \ No newline at end of file diff --git a/Source/Meadow.Units/Conversions/VolumeConversions.cs b/Source/Meadow.Units/Conversions/VolumeConversions.cs index 9d925db..b69e684 100644 --- a/Source/Meadow.Units/Conversions/VolumeConversions.cs +++ b/Source/Meadow.Units/Conversions/VolumeConversions.cs @@ -1,27 +1,26 @@ -namespace Meadow.Units.Conversions +namespace Meadow.Units.Conversions; + +internal static class VolumeConversions { - internal static class VolumeConversions + public static double Convert(double value, Volume.UnitType from, Volume.UnitType to) { - public static double Convert(double value, Volume.UnitType from, Volume.UnitType to) + if (from == to) { - if (from == to) - { - return value; - } - return value * volumeConversions[(int)to] / volumeConversions[(int)from]; + return value; } - - //must align to enum - private static readonly double[] volumeConversions = - { - 1.0, //gal US - 133.2278701, //ounces - 0.13368055556 , //cubic feet - 231.0, //cubic inches - 3.785411784, //liters - 378.5411784, //cl - 3785.411784, //ml - 0.003785411784, //m3 - }; + return value * volumeConversions[(int)to] / volumeConversions[(int)from]; } + + //must align to enum + private static readonly double[] volumeConversions = + { + 1.0, //gal US + 133.2278701, //ounces + 0.13368055556 , //cubic feet + 231.0, //cubic inches + 3.785411784, //liters + 378.5411784, //cl + 3785.411784, //ml + 0.003785411784, //m3 + }; } \ No newline at end of file diff --git a/Source/Meadow.Units/Current.cs b/Source/Meadow.Units/Current.cs index 22efc83..1ec59cb 100644 --- a/Source/Meadow.Units/Current.cs +++ b/Source/Meadow.Units/Current.cs @@ -30,7 +30,7 @@ static Current() public static Current Zero => _zero; /// - /// Creates a new `Current` object. + /// Creates a new object. /// /// The Current value. /// Amps by default. @@ -40,7 +40,7 @@ public Current(double value, UnitType type = UnitType.Amps) } /// - /// Creates a new `Current` object from an existing Current object + /// Creates a new object from an existing Current object /// /// public Current(Current Current) @@ -97,7 +97,7 @@ public enum UnitType /// [Pure] public double From(UnitType convertTo) { - return CurrentConversions.Convert(Value, Current.UnitType.Amps, convertTo); + return CurrentConversions.Convert(Value, UnitType.Amps, convertTo); } /// @@ -105,12 +105,7 @@ [Pure] public double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Current)obj); - } + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object @@ -228,7 +223,7 @@ [Pure] public override bool Equals(object obj) /// Returns the absolute value of the /// /// - [Pure] public Current Abs() { return new Current(Math.Abs(this.Value)); } + [Pure] public Current Abs() { return new Current(Math.Abs(Value)); } /// /// Get a string representation of the object @@ -250,7 +245,16 @@ [Pure] public override bool Equals(object obj) /// /// The other Current cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is Current current) + { + return Value.CompareTo(current.Value); + } + + throw new ArgumentException("Object is not a Current"); + } /// /// Get type code of object diff --git a/Source/Meadow.Units/Density.cs b/Source/Meadow.Units/Density.cs index 64ed256..8f8c2e0 100644 --- a/Source/Meadow.Units/Density.cs +++ b/Source/Meadow.Units/Density.cs @@ -5,445 +5,447 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Density +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Density : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Density + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Density : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Density value. + /// Kilograms per meters cubed by default. + public Density(double value, UnitType type = UnitType.KilogramsPerMetersCubed) { - /// - /// Creates a new `Density` object. - /// - /// The Density value. - /// Kilograms per meters cubed by default. - public Density(double value, UnitType type = UnitType.KilogramsPerMetersCubed) - { - Value = DensityConversions.Convert(value, type, UnitType.KilogramsPerMetersCubed); - } - - /// - /// Creates a new `Density` object from an existing Density object - /// - /// - public Density(Density density) - { - Value = density.Value; - } + Value = DensityConversions.Convert(value, type, UnitType.KilogramsPerMetersCubed); + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Creates a new object from an existing Density object + /// + /// + public Density(Density density) + { + Value = density.Value; + } - /// - /// The type of units available to describe the Density. - /// - public enum UnitType - { - /// - /// Grams per meters cubed - /// - MicroGramsPerMetersCubed, - /// - /// Grams per meters cubed - /// - MilliGramsPerMetersCubed, - /// - /// Grams per centimeters cubed - /// - GramsPerCentimetersCubed, - /// - /// Grams per meters cubed - /// - GramsPerMetersCubed, - /// - /// Grams per liter - /// - GramsPerLiter, - /// - /// Kilograms per meters cubed - /// - KilogramsPerMetersCubed, - /// - /// Ounces per inches cubed - /// - OuncesPerInchesCubed, - /// - /// Ounces per feet cubed - /// - OuncesPerFeetCubed, - /// - /// Pounds per inches cubed - /// - PoundsPerInchesCubed, - /// - /// Pounds per feet cubed - /// - PoundsPerFeetCubed, - /// - /// Density of water - /// - Water - } + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the Density. + /// + public enum UnitType + { /// - /// Get the density in micrograms per meters cubed - /// - public double MicroGramsPerMetersCubed => From(UnitType.MicroGramsPerMetersCubed); - /// - /// Get the density in milligrams per meters cubed + /// Grams per meters cubed /// - public double MilliGramsPerMetersCubed => From(UnitType.MilliGramsPerMetersCubed); + MicroGramsPerMetersCubed, /// - /// Get the density in grams per centimeters cubed + /// Grams per meters cubed /// - public double GramsPerCentimetersCubed => From(UnitType.GramsPerCentimetersCubed); + MilliGramsPerMetersCubed, /// - /// Get the density in grams per meters cubed + /// Grams per centimeters cubed /// - public double GramsPerMetersCubed => From(UnitType.GramsPerMetersCubed); + GramsPerCentimetersCubed, /// - /// Get the density in grams per liter cubed + /// Grams per meters cubed /// - public double GramsPerLiter => From(UnitType.GramsPerLiter); + GramsPerMetersCubed, /// - /// Get the density in kilograms per meters cubed + /// Grams per liter /// - public double KilogramsPerMetersCubed => From(UnitType.KilogramsPerMetersCubed); + GramsPerLiter, /// - /// Get the density in ounces per inches cubed + /// Kilograms per meters cubed /// - public double OuncesPerInchesCubed => From(UnitType.OuncesPerInchesCubed); + KilogramsPerMetersCubed, /// - /// Get the density in ounces per feet cubed + /// Ounces per inches cubed /// - public double OuncesPerFeetCubed => From(UnitType.OuncesPerFeetCubed); + OuncesPerInchesCubed, /// - /// Get the density in pounds per inches cubed + /// Ounces per feet cubed /// - public double PoundsPerInchesCubed => From(UnitType.PoundsPerInchesCubed); + OuncesPerFeetCubed, /// - /// Get the density in pounds per feet cubed + /// Pounds per inches cubed /// - public double PoundsPerFeetCubed => From(UnitType.PoundsPerFeetCubed); + PoundsPerInchesCubed, /// - /// Get the density relative to water + /// Pounds per feet cubed /// - public double Water => From(UnitType.Water); - + PoundsPerFeetCubed, /// - /// Get a double value for a specific unit + /// Density of water /// - /// unit to covert to - /// the converted value - [Pure] - public double From(UnitType convertTo) - { - return DensityConversions.Convert(Value, UnitType.KilogramsPerMetersCubed, convertTo); - } + Water + } - /// - /// Compare to another Density object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Density)obj); - } + /// + /// Get the density in micrograms per meters cubed + /// + public double MicroGramsPerMetersCubed => From(UnitType.MicroGramsPerMetersCubed); + /// + /// Get the density in milligrams per meters cubed + /// + public double MilliGramsPerMetersCubed => From(UnitType.MilliGramsPerMetersCubed); + /// + /// Get the density in grams per centimeters cubed + /// + public double GramsPerCentimetersCubed => From(UnitType.GramsPerCentimetersCubed); + /// + /// Get the density in grams per meters cubed + /// + public double GramsPerMetersCubed => From(UnitType.GramsPerMetersCubed); + /// + /// Get the density in grams per liter cubed + /// + public double GramsPerLiter => From(UnitType.GramsPerLiter); + /// + /// Get the density in kilograms per meters cubed + /// + public double KilogramsPerMetersCubed => From(UnitType.KilogramsPerMetersCubed); + /// + /// Get the density in ounces per inches cubed + /// + public double OuncesPerInchesCubed => From(UnitType.OuncesPerInchesCubed); + /// + /// Get the density in ounces per feet cubed + /// + public double OuncesPerFeetCubed => From(UnitType.OuncesPerFeetCubed); + /// + /// Get the density in pounds per inches cubed + /// + public double PoundsPerInchesCubed => From(UnitType.PoundsPerInchesCubed); + /// + /// Get the density in pounds per feet cubed + /// + public double PoundsPerFeetCubed => From(UnitType.PoundsPerFeetCubed); + /// + /// Get the density relative to water + /// + public double Water => From(UnitType.Water); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Density(ushort value) => new Density(value); - //[Pure] public static implicit operator Density(short value) => new Density(value); - //[Pure] public static implicit operator Density(uint value) => new Density(value); - //[Pure] public static implicit operator Density(long value) => new Density(value); - //[Pure] public static implicit operator Density(int value) => new Density(value); - //[Pure] public static implicit operator Density(float value) => new Density(value); - //[Pure] public static implicit operator Density(double value) => new Density(value); - //[Pure] public static implicit operator Density(decimal value) => new Density((double)value); - - // Comparison - /// - /// Compare to another Density object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Density other) => Value == other.Value; + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] + public double From(UnitType convertTo) + { + return DensityConversions.Convert(Value, UnitType.KilogramsPerMetersCubed, convertTo); + } - /// - /// Equals operator to compare two Density objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Density left, Density right) => Equals(left.Value, right.Value); + /// + /// Compare to another Density object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Not equals operator to compare two Density objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Density left, Density right) => !Equals(left.Value, right.Value); + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Density(ushort value) => new Density(value); + //[Pure] public static implicit operator Density(short value) => new Density(value); + //[Pure] public static implicit operator Density(uint value) => new Density(value); + //[Pure] public static implicit operator Density(long value) => new Density(value); + //[Pure] public static implicit operator Density(int value) => new Density(value); + //[Pure] public static implicit operator Density(float value) => new Density(value); + //[Pure] public static implicit operator Density(double value) => new Density(value); + //[Pure] public static implicit operator Density(decimal value) => new Density((double)value); + + // Comparison + /// + /// Compare to another Density object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Density other) => Value == other.Value; - /// - /// Compare to another Density object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Density other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Equals operator to compare two Density objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Density left, Density right) => Equals(left.Value, right.Value); - /// - /// Less than operator to compare two Density objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Not equals operator to compare two Density objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Density left, Density right) => !Equals(left.Value, right.Value); - /// - /// Greater than operator to compare two Density objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Compare to another Density object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Density other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Less than or equal operator to compare two Density objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Less than operator to compare two Density objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Greater than or equal operator to compare two Density objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Greater than operator to compare two Density objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - // Math - /// - /// Addition operator to add two Density objects - /// - /// left value - /// right value - /// A new Density object with a value of left + right - [Pure] public static Density operator +(Density left, Density right) => new(left.Value + right.Value); + /// + /// Less than or equal operator to compare two Density objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Subtraction operator to subtract two Density objects - /// - /// left value - /// right value - /// A new Density object with a value of left - right - [Pure] public static Density operator -(Density left, Density right) => new(left.Value - right.Value); + /// + /// Greater than or equal operator to compare two Density objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Density left, Density right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Density object with a value of value multiplied by the operand - [Pure] public static Density operator *(Density value, double operand) => new(value.Value * operand); + // Math + /// + /// Addition operator to add two Density objects + /// + /// left value + /// right value + /// A new Density object with a value of left + right + [Pure] public static Density operator +(Density left, Density right) => new(left.Value + right.Value); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Density object with a value of value divided by the operand - [Pure] public static Density operator /(Density value, double operand) => new(value.Value / operand); + /// + /// Subtraction operator to subtract two Density objects + /// + /// left value + /// right value + /// A new Density object with a value of left - right + [Pure] public static Density operator -(Density left, Density right) => new(left.Value - right.Value); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Density object with a value of value multiplied by the operand + [Pure] public static Density operator *(Density value, double operand) => new(value.Value * operand); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Density object with a value of value divided by the operand + [Pure] public static Density operator /(Density value, double operand) => new(value.Value / operand); - // IComparable - /// - /// Compare to another Density object - /// - /// The other Density cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + // IComparable + /// + /// Compare to another Density object + /// + /// The other Density cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Density density) + { + return Value.CompareTo(density.Value); + } - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + throw new ArgumentException("Object is not a Density"); + } - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/DigitalStorage.cs b/Source/Meadow.Units/DigitalStorage.cs index 3a451ed..87ca0c0 100644 --- a/Source/Meadow.Units/DigitalStorage.cs +++ b/Source/Meadow.Units/DigitalStorage.cs @@ -5,491 +5,493 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents a value of Digital Storage. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct DigitalStorage : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { + private static DigitalStorage _zero; + + static DigitalStorage() + { + _zero = new DigitalStorage(0, UnitType.Bytes); + } + /// - /// Represents a value of Digital Storage. + /// Gets a DigitalStorage with a zero value /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct DigitalStorage : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + public static DigitalStorage Zero => _zero; + + /// + /// Creates a new object. + /// + /// The DigitalStorage value. + /// Bytes by default. + public DigitalStorage(double value, UnitType type = UnitType.Bytes) { - private static DigitalStorage _zero; + Value = DigitalStorageConversions.Convert(value, type, UnitType.Bytes); + } - static DigitalStorage() - { - _zero = new DigitalStorage(0, UnitType.Bytes); - } + /// + /// Creates a new object from an existing DigitalStorage object + /// + /// + public DigitalStorage(DigitalStorage DigitalStorage) + { + Value = DigitalStorage.Value; + } - /// - /// Gets a DigitalStorage with a zero value - /// - public static DigitalStorage Zero => _zero; + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the DigitalStorage. + /// + public enum UnitType + { /// - /// Creates a new `DigitalStorage` object. + /// Represents a unit of data size in bits /// - /// The DigitalStorage value. - /// Bytes by default. - public DigitalStorage(double value, UnitType type = UnitType.Bytes) - { - Value = DigitalStorageConversions.Convert(value, type, UnitType.Bytes); - } - + Bits, /// - /// Creates a new `DigitalStorage` object from an existing DigitalStorage object + /// Represents a unit of data size in bytes /// - /// - public DigitalStorage(DigitalStorage DigitalStorage) - { - Value = DigitalStorage.Value; - } - + Bytes, /// - /// Internal canonical value. + /// Represents a unit of data size in kilobytes (KB) /// - private readonly double Value; - + KiloBytes, /// - /// The type of units available to describe the DigitalStorage. + /// Represents a unit of data size in megabytes (MB) /// - public enum UnitType - { - /// - /// Represents a unit of data size in bits - /// - Bits, - /// - /// Represents a unit of data size in bytes - /// - Bytes, - /// - /// Represents a unit of data size in kilobytes (KB) - /// - KiloBytes, - /// - /// Represents a unit of data size in megabytes (MB) - /// - MegaBytes, - /// - /// Represents a unit of data size in gigabytes (GB) - /// - GigaBytes, - /// - /// Represents a unit of data size in terabytes (TB) - /// - TeraBytes, - /// - /// Represents a unit of data size in petabytes (PB) - /// - PetaBytes, - /// - /// Represents a unit of data size in exabytes (EB) - /// - ExaBytes, - /// - /// Represents a unit of data size in kibibytes (KiB) - /// - KibiBytes, - /// - /// Represents a unit of data size in kibibits (Kib) - /// - Kibibits, - /// - /// Represents a unit of data size in mebibytes (MiB) - /// - MebiBytes, - /// - /// Represents a unit of data size in mebibits (Mib) - /// - Mebibits, - /// - /// Represents a unit of data size in gibibytes (GiB) - /// - GibiBytes, - } - + MegaBytes, /// - /// Get DigitalStorage in bits + /// Represents a unit of data size in gigabytes (GB) /// - public double Bits => From(UnitType.Bits); - + GigaBytes, /// - /// Get DigitalStorage in bytes + /// Represents a unit of data size in terabytes (TB) /// - public double Bytes => From(UnitType.Bytes); - + TeraBytes, /// - /// Get DigitalStorage in kilobytes (KB) + /// Represents a unit of data size in petabytes (PB) /// - public double KiloBytes => From(UnitType.KiloBytes); - + PetaBytes, /// - /// Get DigitalStorage in megabytes (MB) + /// Represents a unit of data size in exabytes (EB) /// - public double MegaBytes => From(UnitType.MegaBytes); - + ExaBytes, /// - /// Get DigitalStorage in gigabytes (GB) + /// Represents a unit of data size in kibibytes (KiB) /// - public double GigaBytes => From(UnitType.GigaBytes); - + KibiBytes, /// - /// Get DigitalStorage in terabytes (TB) + /// Represents a unit of data size in kibibits (Kib) /// - public double TeraBytes => From(UnitType.TeraBytes); - + Kibibits, /// - /// Get DigitalStorage in petabytes (PB) + /// Represents a unit of data size in mebibytes (MiB) /// - public double PetaBytes => From(UnitType.PetaBytes); - + MebiBytes, /// - /// Get DigitalStorage in exabytes (EB) + /// Represents a unit of data size in mebibits (Mib) /// - public double ExaBytes => From(UnitType.ExaBytes); - + Mebibits, /// - /// Get DigitalStorage in kibibytes (KiB) + /// Represents a unit of data size in gibibytes (GiB) /// - public double KibiBytes => From(UnitType.KibiBytes); + GibiBytes, + } - /// - /// Get DigitalStorage in kibibits (Kibit) - /// - public double Kibibits => From(UnitType.Kibibits); + /// + /// Get DigitalStorage in bits + /// + public double Bits => From(UnitType.Bits); - /// - /// Get DigitalStorage in mebibits (MiB) - /// - public double MebiBytes => From(UnitType.MebiBytes); + /// + /// Get DigitalStorage in bytes + /// + public double Bytes => From(UnitType.Bytes); - /// - /// Get DigitalStorage in mebibits (Mib) - /// - public double Mebibits => From(UnitType.Mebibits); + /// + /// Get DigitalStorage in kilobytes (KB) + /// + public double KiloBytes => From(UnitType.KiloBytes); - /// - /// Get DigitalStorage in gibibytes (GiB) - /// - public double GibiBytes => From(UnitType.GibiBytes); + /// + /// Get DigitalStorage in megabytes (MB) + /// + public double MegaBytes => From(UnitType.MegaBytes); - /// - /// Convert to a specific unit - /// - /// the unit to convert to - /// - [Pure] - public double From(UnitType convertTo) - { - return DigitalStorageConversions.Convert(Value, DigitalStorage.UnitType.Bytes, convertTo); - } + /// + /// Get DigitalStorage in gigabytes (GB) + /// + public double GigaBytes => From(UnitType.GigaBytes); - /// - /// Compare to another DigitalStorage object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && ((DigitalStorage)obj).Value == this.Value; - } + /// + /// Get DigitalStorage in terabytes (TB) + /// + public double TeraBytes => From(UnitType.TeraBytes); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator DigitalStorage(ushort value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(short value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(uint value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(long value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(int value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(float value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(double value) => new DigitalStorage(value); - //[Pure] public static implicit operator DigitalStorage(decimal value) => new DigitalStorage((double)value); - - // Comparison - /// - /// Compare to another DigitalStorage object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(DigitalStorage other) => Value == other.Value; + /// + /// Get DigitalStorage in petabytes (PB) + /// + public double PetaBytes => From(UnitType.PetaBytes); - /// - /// Equals operator to compare two DigitalStorage objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(DigitalStorage left, DigitalStorage right) => Equals(left.Value, right.Value); + /// + /// Get DigitalStorage in exabytes (EB) + /// + public double ExaBytes => From(UnitType.ExaBytes); - /// - /// Not equals operator to compare two DigitalStorage objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(DigitalStorage left, DigitalStorage right) => !Equals(left.Value, right.Value); + /// + /// Get DigitalStorage in kibibytes (KiB) + /// + public double KibiBytes => From(UnitType.KibiBytes); - /// - /// Compare to another DigitalStorage object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(DigitalStorage other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Get DigitalStorage in kibibits (Kibit) + /// + public double Kibibits => From(UnitType.Kibibits); - /// - /// Less than operator to compare two DigitalStorage objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Get DigitalStorage in mebibits (MiB) + /// + public double MebiBytes => From(UnitType.MebiBytes); - /// - /// Greater than operator to compare two DigitalStorage objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Get DigitalStorage in mebibits (Mib) + /// + public double Mebibits => From(UnitType.Mebibits); - /// - /// Less than or equal operator to compare two DigitalStorage objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Get DigitalStorage in gibibytes (GiB) + /// + public double GibiBytes => From(UnitType.GibiBytes); - /// - /// Greater than or equal operator to compare two DigitalStorage objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Convert to a specific unit + /// + /// the unit to convert to + /// + [Pure] + public double From(UnitType convertTo) + { + return DigitalStorageConversions.Convert(Value, UnitType.Bytes, convertTo); + } - // Math - /// - /// Addition operator to add two DigitalStorage objects - /// - /// left value - /// right value - /// A new DigitalStorage object with a value of left + right - [Pure] public static DigitalStorage operator +(DigitalStorage left, DigitalStorage right) => new(left.Value + right.Value); + /// + /// Compare to another DigitalStorage object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Subtraction operator to subtract two DigitalStorage objects - /// - /// left value - /// right value - /// A new DigitalStorage object with a value of left - right - [Pure] public static DigitalStorage operator -(DigitalStorage left, DigitalStorage right) => new(left.Value - right.Value); + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator DigitalStorage(ushort value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(short value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(uint value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(long value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(int value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(float value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(double value) => new DigitalStorage(value); + //[Pure] public static implicit operator DigitalStorage(decimal value) => new DigitalStorage((double)value); + + // Comparison + /// + /// Compare to another DigitalStorage object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(DigitalStorage other) => Value == other.Value; - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new DigitalStorage object with a value of value multiplied by the operand - [Pure] public static DigitalStorage operator *(DigitalStorage value, double operand) => new(value.Value * operand); + /// + /// Equals operator to compare two DigitalStorage objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(DigitalStorage left, DigitalStorage right) => Equals(left.Value, right.Value); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new DigitalStorage object with a value of value divided by the operand - [Pure] public static DigitalStorage operator /(DigitalStorage value, double operand) => new(value.Value / operand); + /// + /// Not equals operator to compare two DigitalStorage objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(DigitalStorage left, DigitalStorage right) => !Equals(left.Value, right.Value); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public DigitalStorage Abs() { return new DigitalStorage(Math.Abs(this.Value)); } + /// + /// Compare to another DigitalStorage object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(DigitalStorage other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Less than operator to compare two DigitalStorage objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Greater than operator to compare two DigitalStorage objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - // IComparable - /// - /// Compare to another DigitalStorage object - /// - /// The other DigitalStorage cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Less than or equal operator to compare two DigitalStorage objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Greater than or equal operator to compare two DigitalStorage objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(DigitalStorage left, DigitalStorage right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + // Math + /// + /// Addition operator to add two DigitalStorage objects + /// + /// left value + /// right value + /// A new DigitalStorage object with a value of left + right + [Pure] public static DigitalStorage operator +(DigitalStorage left, DigitalStorage right) => new(left.Value + right.Value); - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Subtraction operator to subtract two DigitalStorage objects + /// + /// left value + /// right value + /// A new DigitalStorage object with a value of left - right + [Pure] public static DigitalStorage operator -(DigitalStorage left, DigitalStorage right) => new(left.Value - right.Value); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new DigitalStorage object with a value of value multiplied by the operand + [Pure] public static DigitalStorage operator *(DigitalStorage value, double operand) => new(value.Value * operand); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new DigitalStorage object with a value of value divided by the operand + [Pure] public static DigitalStorage operator /(DigitalStorage value, double operand) => new(value.Value / operand); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public DigitalStorage Abs() { return new DigitalStorage(Math.Abs(Value)); } - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + // IComparable + /// + /// Compare to another DigitalStorage object + /// + /// The other DigitalStorage cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is DigitalStorage digitalStorage) + { + return Value.CompareTo(digitalStorage.Value); + } - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + throw new ArgumentException("Object is not a DigitalStorage"); + } - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to type - /// - /// conversion unit type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// conversion unit type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Energy.cs b/Source/Meadow.Units/Energy.cs index 4e925c6..7850fbe 100644 --- a/Source/Meadow.Units/Energy.cs +++ b/Source/Meadow.Units/Energy.cs @@ -5,432 +5,435 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Energy +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Energy : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Energy + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Energy : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Energy value. + /// Joules by default. + public Energy(double value, UnitType type = UnitType.Joules) { - /// - /// Creates a new `Energy` object. - /// - /// The Energy value. - /// Joules by default. - public Energy(double value, UnitType type = UnitType.Joules) - { - Value = EnergyConversions.Convert(value, type, UnitType.Joules); - } - - /// - /// Creates a new `Energy` object from an existing Energy object - /// - /// - public Energy(Energy energy) - { - Value = energy.Value; - } + Value = EnergyConversions.Convert(value, type, UnitType.Joules); + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Creates a new object from an existing Energy object + /// + /// + public Energy(Energy energy) + { + Value = energy.Value; + } - /// - /// The type of units available to describe the Energy. - /// - public enum UnitType - { - /// - /// BTU - /// - BTU, - /// - /// Calories - /// - Calories, - /// - /// Joules - /// - Joules, - /// - /// Kilocalories - /// - Kilocalories, - /// - /// Kilojoules - /// - Kilojoules, - /// - /// Kilowatt hours - /// - KilowattHours, - /// - /// Therms - /// - Therms, - /// - /// Watt hours - /// - WattHours, - /// - /// Watt seconds - /// - WattSeconds - } + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe the Energy. + /// + public enum UnitType + { /// - /// Get energy value as BTUs + /// BTU /// - public double BTU => From(UnitType.BTU); + BTU, /// - /// Get energy value as calories + /// Calories /// - public double Calories => From(UnitType.Calories); + Calories, /// - /// Get energy value as joules + /// Joules /// - public double Joules => From(UnitType.Joules); + Joules, /// - /// Get energy value as kilocalories + /// Kilocalories /// - public double Kilocalories => From(UnitType.Kilocalories); + Kilocalories, /// - /// Get energy value kilojoules + /// Kilojoules /// - public double Kilojoules => From(UnitType.Kilojoules); + Kilojoules, /// - /// Get energy value as kilowatt hours + /// Kilowatt hours /// - public double KilowattHours => From(UnitType.KilowattHours); + KilowattHours, /// - /// Get energy value as Therms + /// Therms /// - public double Therms => From(UnitType.Therms); + Therms, /// - /// Get energy value as watt hours + /// Watt hours /// - public double WattHours => From(UnitType.WattHours); + WattHours, /// - /// Get energy value as watt seconds + /// Watt seconds /// - public double WattSecond => From(UnitType.WattSeconds); + WattSeconds + } + + /// + /// Get energy value as BTUs + /// + public double BTU => From(UnitType.BTU); + /// + /// Get energy value as calories + /// + public double Calories => From(UnitType.Calories); + /// + /// Get energy value as joules + /// + public double Joules => From(UnitType.Joules); + /// + /// Get energy value as kilocalories + /// + public double Kilocalories => From(UnitType.Kilocalories); + /// + /// Get energy value kilojoules + /// + public double Kilojoules => From(UnitType.Kilojoules); + /// + /// Get energy value as kilowatt hours + /// + public double KilowattHours => From(UnitType.KilowattHours); + /// + /// Get energy value as Therms + /// + public double Therms => From(UnitType.Therms); + /// + /// Get energy value as watt hours + /// + public double WattHours => From(UnitType.WattHours); + /// + /// Get energy value as watt seconds + /// + public double WattSecond => From(UnitType.WattSeconds); - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return EnergyConversions.Convert(Value, UnitType.Joules, convertTo); - } + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return EnergyConversions.Convert(Value, UnitType.Joules, convertTo); + } - /// - /// Compare to another Energy object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Energy)obj); - } + /// + /// Compare to another Energy object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Energy(ushort value) => new Energy(value); - //[Pure] public static implicit operator Energy(short value) => new Energy(value); - //[Pure] public static implicit operator Energy(uint value) => new Energy(value); - //[Pure] public static implicit operator Energy(long value) => new Energy(value); - //[Pure] public static implicit operator Energy(int value) => new Energy(value); - //[Pure] public static implicit operator Energy(float value) => new Energy(value); - //[Pure] public static implicit operator Energy(double value) => new Energy(value); - //[Pure] public static implicit operator Energy(decimal value) => new Energy((double)value); - - // Comparison - /// - /// Compare to another Energy object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Energy other) => Value == other.Value; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Energy(ushort value) => new Energy(value); + //[Pure] public static implicit operator Energy(short value) => new Energy(value); + //[Pure] public static implicit operator Energy(uint value) => new Energy(value); + //[Pure] public static implicit operator Energy(long value) => new Energy(value); + //[Pure] public static implicit operator Energy(int value) => new Energy(value); + //[Pure] public static implicit operator Energy(float value) => new Energy(value); + //[Pure] public static implicit operator Energy(double value) => new Energy(value); + //[Pure] public static implicit operator Energy(decimal value) => new Energy((double)value); + + // Comparison + /// + /// Compare to another Energy object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Energy other) => Value == other.Value; - /// - /// Equals operator to compare two Energy objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Energy left, Energy right) => Equals(left.Value, right.Value); + /// + /// Equals operator to compare two Energy objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Energy left, Energy right) => Equals(left.Value, right.Value); - /// - /// Not equals operator to compare two Energy objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Energy left, Energy right) => !Equals(left.Value, right.Value); + /// + /// Not equals operator to compare two Energy objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Energy left, Energy right) => !Equals(left.Value, right.Value); - /// - /// Compare to another Energy object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Energy other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Compare to another Energy object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Energy other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Less than operator to compare two Energy objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Less than operator to compare two Energy objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Greater than operator to compare two Energy objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Greater than operator to compare two Energy objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - /// - /// Less than or equal operator to compare two Energy objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Less than or equal operator to compare two Energy objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Greater than or equal operator to compare two Energy objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Greater than or equal operator to compare two Energy objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Energy left, Energy right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - // Math - /// - /// Addition operator to add two Energy objects - /// - /// left value - /// right value - /// A new Energy object with a value of left + right - [Pure] public static Energy operator +(Energy left, Energy right) => new (left.Value + right.Value); + // Math + /// + /// Addition operator to add two Energy objects + /// + /// left value + /// right value + /// A new Energy object with a value of left + right + [Pure] public static Energy operator +(Energy left, Energy right) => new (left.Value + right.Value); - /// - /// Subtraction operator to subtract two Energy objects - /// - /// left value - /// right value - /// A new Energy object with a value of left - right - [Pure] public static Energy operator -(Energy left, Energy right) => new (left.Value - right.Value); + /// + /// Subtraction operator to subtract two Energy objects + /// + /// left value + /// right value + /// A new Energy object with a value of left - right + [Pure] public static Energy operator -(Energy left, Energy right) => new (left.Value - right.Value); - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Energy object with a value of value multiplied by the operand - [Pure] public static Energy operator *(Energy value, double operand) => new (value.Value * operand); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Energy object with a value of value multiplied by the operand + [Pure] public static Energy operator *(Energy value, double operand) => new (value.Value * operand); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Energy object with a value of value divided by the operand - [Pure] public static Energy operator /(Energy value, double operand) => new (value.Value / operand); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Energy object with a value of value divided by the operand + [Pure] public static Energy operator /(Energy value, double operand) => new (value.Value / operand); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Energy Abs() => new (Math.Abs(Value)); + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Energy Abs() => new (Math.Abs(Value)); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - // IComparable - /// - /// Compare to another Energy object - /// - /// The other Energy cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + // IComparable + /// + /// Compare to another Energy object + /// + /// The other Energy cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Energy energy) + { + return Value.CompareTo(energy.Value); + } - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + throw new ArgumentException("Object is not an Energy"); + } - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Convert to type - /// - /// type to covert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Convert to type + /// + /// type to covert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); - } + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Frequency.cs b/Source/Meadow.Units/Frequency.cs index dbd39b6..20b28a0 100644 --- a/Source/Meadow.Units/Frequency.cs +++ b/Source/Meadow.Units/Frequency.cs @@ -5,392 +5,387 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Frequency +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Frequency : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Frequency + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Frequency : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Frequency value. + /// cycles per second by default. + public Frequency(double value, UnitType type = UnitType.Hertz) { - /// - /// Creates a new `Frequency` object. - /// - /// The Frequency value. - /// cycles per second by default. - public Frequency(double value, UnitType type = UnitType.Hertz) - { - Value = FrequencyConversions.Convert(value, type, UnitType.Hertz); - } + Value = FrequencyConversions.Convert(value, type, UnitType.Hertz); + } - /// - /// Creates a new `Frequency` object from an existing Frequency object - /// - /// - public Frequency(Frequency frequency) - { - this.Value = frequency.Value; - } + /// + /// Creates a new object from an existing Frequency object + /// + /// + public Frequency(Frequency frequency) + { + Value = frequency.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Frequency. - /// - public enum UnitType - { - /// - /// Gigahertz - /// - Gigahertz, - /// - /// Megahertz - /// - Megahertz, - /// - /// Kilohertz - /// - Kilohertz, - /// - /// Hertz - /// - Hertz, - } + /// + /// The type of units available to describe the Frequency. + /// + public enum UnitType + { + /// Gigahertz + Gigahertz, + /// Megahertz + Megahertz, + /// Kilohertz + Kilohertz, + /// Hertz + Hertz, + } - /// - /// Get frequency value as gigahertz - /// - public double Gigahertz => From(UnitType.Gigahertz); - /// - /// Get frequency value as megahertz - /// - public double Megahertz => From(UnitType.Megahertz); - /// - /// Get frequency value as kilohertz - /// - public double Kilohertz => From(UnitType.Kilohertz); - /// - /// Get frequency value as hertz - /// - public double Hertz => From(UnitType.Hertz); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return FrequencyConversions.Convert(Value, UnitType.Hertz, convertTo); - } + /// + /// Get frequency value as gigahertz + /// + public double Gigahertz => From(UnitType.Gigahertz); + /// + /// Get frequency value as megahertz + /// + public double Megahertz => From(UnitType.Megahertz); + /// + /// Get frequency value as kilohertz + /// + public double Kilohertz => From(UnitType.Kilohertz); + /// + /// Get frequency value as hertz + /// + public double Hertz => From(UnitType.Hertz); - /// - /// Compare to another Frequency object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Frequency)obj); - } + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return FrequencyConversions.Convert(Value, UnitType.Hertz, convertTo); + } + + /// + /// Compare to another Frequency object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Frequency(ushort value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(short value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(uint value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(long value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(int value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(float value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(double value) => new Frequency(value); + //[Pure] public static implicit operator Frequency(decimal value) => new Frequency((double)value); + + // Comparison + /// + /// Compare to another Frequency object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Frequency other) => Value == other.Value; + + /// + /// Equals operator to compare two Frequency objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Frequency left, Frequency right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Frequency objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Frequency left, Frequency right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Frequency object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Frequency other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Frequency objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Frequency objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Frequency objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Frequency objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Frequency(ushort value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(short value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(uint value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(long value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(int value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(float value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(double value) => new Frequency(value); - //[Pure] public static implicit operator Frequency(decimal value) => new Frequency((double)value); - - // Comparison - /// - /// Compare to another Frequency object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Frequency other) => Value == other.Value; - - /// - /// Equals operator to compare two Frequency objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Frequency left, Frequency right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Frequency objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Frequency left, Frequency right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Frequency object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Frequency other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Frequency objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Frequency objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Frequency objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Frequency objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Frequency left, Frequency right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Frequency objects - /// - /// left value - /// right value - /// A new Frequency object with a value of left + right - [Pure] public static Frequency operator +(Frequency left, Frequency right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Frequency objects - /// - /// left value - /// right value - /// A new Frequency object with a value of left - right - [Pure] public static Frequency operator -(Frequency left, Frequency right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Frequency object with a value of value multiplied by the operand - [Pure] public static Frequency operator *(Frequency value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Frequency object with a value of value divided by the operand - [Pure] public static Frequency operator /(Frequency value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Frequency Abs() => new (Math.Abs(Value)); - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Frequency object - /// - /// The other Frequency cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + // Math + /// + /// Addition operator to add two Frequency objects + /// + /// left value + /// right value + /// A new Frequency object with a value of left + right + [Pure] public static Frequency operator +(Frequency left, Frequency right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Frequency objects + /// + /// left value + /// right value + /// A new Frequency object with a value of left - right + [Pure] public static Frequency operator -(Frequency left, Frequency right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Frequency object with a value of value multiplied by the operand + [Pure] public static Frequency operator *(Frequency value, double operand) => new (value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Frequency object with a value of value divided by the operand + [Pure] public static Frequency operator /(Frequency value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Frequency Abs() => new (Math.Abs(Value)); + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Frequency object + /// + /// The other Frequency cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Frequency frequency) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(frequency.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Frequency"); } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); + } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/GeoLocation.cs b/Source/Meadow.Units/GeoLocation.cs index f036825..60cfb5c 100644 --- a/Source/Meadow.Units/GeoLocation.cs +++ b/Source/Meadow.Units/GeoLocation.cs @@ -1,115 +1,114 @@ using System; using System.Diagnostics.Contracts; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents a location on the surface of an ideal Earth (latitude and longitude) +/// +public record GeoLocation { /// - /// Represents a location on the surface of an ideal Earth (latitude and longitude) + /// Idealized earth radius used for internal calculations + /// + public Length EarthRadius => new Length(6371.01, Length.UnitType.Kilometers); + + /// + /// The latitude portion of the GeoLocation + /// + public double Latitude { get; set; } + /// + /// The Longitude portion of the GeoLocation + /// + public double Longitude { get; set; } + + /// + /// Creates a GeoLocation instance /// - public record GeoLocation + /// + /// + public GeoLocation(double latitude, double longitude) { - /// - /// Idealized earth radius used for internal calculations - /// - public Length EarthRadius => new Length(6371.01, Length.UnitType.Kilometers); - - /// - /// The latitude portion of the GeoLocation - /// - public double Latitude { get; set; } - /// - /// The Longitude portion of the GeoLocation - /// - public double Longitude { get; set; } - - /// - /// Creates a GeoLocation instance - /// - /// - /// - public GeoLocation(double latitude, double longitude) - { - Latitude = latitude; - Longitude = longitude; - } + Latitude = latitude; + Longitude = longitude; + } - [Pure] - private static double DegreesToRadians(double degrees) - { - return degrees * Math.PI / 180.0; - } + [Pure] + private static double DegreesToRadians(double degrees) + { + return degrees * Math.PI / 180.0; + } - [Pure] - private static double RadiansToDegrees(double radians) - { - return radians * 180 / Math.PI; - } + [Pure] + private static double RadiansToDegrees(double radians) + { + return radians * 180 / Math.PI; + } - /// - /// Calculates the distance to another GeoLocation - /// - /// - /// - [Pure] - public Length DistanceTo(GeoLocation other) - { - var diffLat = DegreesToRadians(other.Latitude - this.Latitude); - var diffLong = DegreesToRadians(other.Longitude - this.Longitude); + /// + /// Calculates the distance to another GeoLocation + /// + /// + /// + [Pure] + public Length DistanceTo(GeoLocation other) + { + var diffLat = DegreesToRadians(other.Latitude - Latitude); + var diffLong = DegreesToRadians(other.Longitude - Longitude); - var a = Math.Sin(diffLat / 2) * Math.Sin(diffLat / 2) + - Math.Cos(DegreesToRadians(this.Latitude)) * Math.Cos(DegreesToRadians(other.Latitude)) * - Math.Sin(diffLong / 2) * Math.Sin(diffLong / 2); - var c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a))); - var d = EarthRadius * c; + var a = Math.Sin(diffLat / 2) * Math.Sin(diffLat / 2) + + Math.Cos(DegreesToRadians(Latitude)) * Math.Cos(DegreesToRadians(other.Latitude)) * + Math.Sin(diffLong / 2) * Math.Sin(diffLong / 2); + var c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a))); + var d = EarthRadius * c; - return d; - } + return d; + } - /// - /// Calculates the bearing to another GeoLocation - /// - /// - /// - [Pure] - public Azimuth BearingTo(GeoLocation other) + /// + /// Calculates the bearing to another GeoLocation + /// + /// + /// + [Pure] + public Azimuth BearingTo(GeoLocation other) + { + var dLon = DegreesToRadians(other.Longitude - Longitude); + var dPhi = Math.Log(Math.Tan(DegreesToRadians(other.Latitude) / 2 + Math.PI / 4) / Math.Tan(DegreesToRadians(Latitude) / 2 + Math.PI / 4)); + if (Math.Abs(dLon) > Math.PI) { - var dLon = DegreesToRadians(other.Longitude - this.Longitude); - var dPhi = Math.Log(Math.Tan(DegreesToRadians(other.Latitude) / 2 + Math.PI / 4) / Math.Tan(DegreesToRadians(this.Latitude) / 2 + Math.PI / 4)); - if (Math.Abs(dLon) > Math.PI) - { - dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon); - } - - return Azimuth.FromRadians(Math.Atan2(dLon, dPhi)); + dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon); } - /// - /// Creates a new GeoLocation a given bearing and distance from the current GeoLocation - /// - /// Bearing angle to the new location - /// Distance to the new location - /// - [Pure] - public GeoLocation Move(Azimuth bearing, Length distance) // double initialBearingRadians, double distanceKilometres) - { - var distRatio = distance.Meters / EarthRadius.Meters; - var distRatioSine = Math.Sin(distRatio); - var distRatioCosine = Math.Cos(distRatio); + return Azimuth.FromRadians(Math.Atan2(dLon, dPhi)); + } - var startLatRad = DegreesToRadians(this.Latitude); - var startLonRad = DegreesToRadians(this.Longitude); + /// + /// Creates a new GeoLocation a given bearing and distance from the current GeoLocation + /// + /// Bearing angle to the new location + /// Distance to the new location + /// + [Pure] + public GeoLocation Move(Azimuth bearing, Length distance) // double initialBearingRadians, double distanceKilometres) + { + var distRatio = distance.Meters / EarthRadius.Meters; + var distRatioSine = Math.Sin(distRatio); + var distRatioCosine = Math.Cos(distRatio); - var startLatCos = Math.Cos(startLatRad); - var startLatSin = Math.Sin(startLatRad); + var startLatRad = DegreesToRadians(Latitude); + var startLonRad = DegreesToRadians(Longitude); - var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(bearing.Radians))); + var startLatCos = Math.Cos(startLatRad); + var startLatSin = Math.Sin(startLatRad); - var endLonRads = startLonRad - + Math.Atan2( - Math.Sin(bearing.Radians) * distRatioSine * startLatCos, - distRatioCosine - startLatSin * Math.Sin(endLatRads)); + var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(bearing.Radians))); - return new GeoLocation(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads)); - } + var endLonRads = startLonRad + + Math.Atan2( + Math.Sin(bearing.Radians) * distRatioSine * startLatCos, + distRatioCosine - startLatSin * Math.Sin(endLatRads)); + + return new GeoLocation(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads)); } } \ No newline at end of file diff --git a/Source/Meadow.Units/Illuminance.cs b/Source/Meadow.Units/Illuminance.cs index bc9d08a..db622aa 100644 --- a/Source/Meadow.Units/Illuminance.cs +++ b/Source/Meadow.Units/Illuminance.cs @@ -5,396 +5,385 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Illuminance +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Illuminance : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { + private static Illuminance _zero; + + static Illuminance() + { + _zero = new Illuminance(0, UnitType.Lux); + } + + /// + /// Gets a voltage of 0 Volts + /// + public static Illuminance Zero => _zero; + /// - /// Represents Illuminance + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Illuminance : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Illuminance value. + /// Lux by default. + public Illuminance(double value, UnitType type = UnitType.Lux) { - private static Illuminance _zero; + Value = IlluminanceConversions.Convert(value, type, UnitType.Lux); + } - static Illuminance() - { - _zero = new Illuminance(0, UnitType.Lux); - } + /// + /// Creates a new object from an existing Illuminance object + /// + /// + public Illuminance(Illuminance illuminance) + { + Value = illuminance.Value; + } - /// - /// Gets a voltage of 0 Volts - /// - public static Illuminance Zero => _zero; - - /// - /// Creates a new `Illuminance` object. - /// - /// The Illuminance value. - /// Lux by default. - public Illuminance(double value, UnitType type = UnitType.Lux) - { - Value = IlluminanceConversions.Convert(value, type, UnitType.Lux); - } + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// Creates a new `Illuminance` object from an existing Illuminance object - /// - /// - public Illuminance(Illuminance illuminance) - { - this.Value = illuminance.Value; - } + /// + /// The type of units available to describe the Illuminance. + /// + public enum UnitType + { + /// KiloLux + KiloLux, + /// Lux + Lux, + /// Foot candles + FootCandles, + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Get illuminance value as kiloLux + /// + public double KiloLux => From(UnitType.KiloLux); + /// + /// Get illuminance value as Lux + /// + public double Lux => From(UnitType.Lux); + /// + /// Get illuminance value as foot candles + /// + public double FootCandles => From(UnitType.FootCandles); - /// - /// The type of units available to describe the Illuminance. - /// - public enum UnitType - { - /// - /// KiloLux - /// - KiloLux, - /// - /// Lux - /// - Lux, - /// - /// Foot candles - /// - FootCandles, - } + /// + /// Get illuminance value for a given unit + /// + /// unit to convert to + /// + [Pure] + public double From(UnitType convertTo) + { + return IlluminanceConversions.Convert(Value, UnitType.Lux, convertTo); + } - /// - /// Get illuminance value as kiloLux - /// - public double KiloLux => From(UnitType.KiloLux); - /// - /// Get illuminance value as Lux - /// - public double Lux => From(UnitType.Lux); - /// - /// Get illuminance value as foot candles - /// - public double FootCandles => From(UnitType.FootCandles); - - /// - /// Get illuminance value for a given unit - /// - /// unit to convert to - /// - [Pure] - public double From(UnitType convertTo) - { - return IlluminanceConversions.Convert(Value, UnitType.Lux, convertTo); - } + /// + /// Compare to another Illuminance object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Compare to another Illuminance object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - return this.CompareTo(obj) == 0; - } + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // Comparison - /// - /// Compare to another Illuminance object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Illuminance other) => Value == other.Value; - - /// - /// Equals operator to compare two Illuminance objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Illuminance left, Illuminance right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Illuminance objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Illuminance left, Illuminance right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Illuminance object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Illuminance other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Illuminance objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Illuminance objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Illuminance objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Illuminance objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Illuminance objects - /// - /// left value - /// right value - /// A new Illuminance object with a value of left + right - [Pure] public static Illuminance operator +(Illuminance left, Illuminance right) => new(left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Illuminance objects - /// - /// left value - /// right value - /// A new Illuminance object with a value of left - right - [Pure] public static Illuminance operator -(Illuminance left, Illuminance right) => new(left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Illuminance object with a value of value multiplied by the operand - [Pure] public static Illuminance operator *(Illuminance value, double operand) => new(value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Illuminance object with a value of value divided by the operand - [Pure] public static Illuminance operator /(Illuminance value, double operand) => new(value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Illuminance Abs() => new(Math.Abs(Value)); - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Illuminance object - /// - /// The other Illuminance cast to object - /// 0 if equal - [Pure] - public int CompareTo(object obj) - { - if (obj is Illuminance illuminance) - { - return Value.CompareTo(illuminance.Value); - } + // Comparison + /// + /// Compare to another Illuminance object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Illuminance other) => Value == other.Value; - throw new ArgumentException("Object is not an Illuminance"); - } + /// + /// Equals operator to compare two Illuminance objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Illuminance left, Illuminance right) => Equals(left.Value, right.Value); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) + /// + /// Not equals operator to compare two Illuminance objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Illuminance left, Illuminance right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Illuminance object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Illuminance other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Illuminance objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Illuminance objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Illuminance objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Illuminance objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Illuminance left, Illuminance right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Illuminance objects + /// + /// left value + /// right value + /// A new Illuminance object with a value of left + right + [Pure] public static Illuminance operator +(Illuminance left, Illuminance right) => new(left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Illuminance objects + /// + /// left value + /// right value + /// A new Illuminance object with a value of left - right + [Pure] public static Illuminance operator -(Illuminance left, Illuminance right) => new(left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Illuminance object with a value of value multiplied by the operand + [Pure] public static Illuminance operator *(Illuminance value, double operand) => new(value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Illuminance object with a value of value divided by the operand + [Pure] public static Illuminance operator /(Illuminance value, double operand) => new(value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Illuminance Abs() => new(Math.Abs(Value)); + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Illuminance object + /// + /// The other Illuminance cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Illuminance illuminance) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(illuminance.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not an Illuminance"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Length.cs b/Source/Meadow.Units/Length.cs index 14d7cec..cdf5515 100644 --- a/Source/Meadow.Units/Length.cs +++ b/Source/Meadow.Units/Length.cs @@ -30,81 +30,57 @@ static Length() public static Length Zero => _zero; /// - /// Creates a new `Length` object. + /// Creates a new object. /// /// The Length value. /// Meters by default. public Length(double value, UnitType type = UnitType.Meters) { - _value = LengthConversions.Convert(value, type, UnitType.Meters); + Value = LengthConversions.Convert(value, type, UnitType.Meters); } /// - /// Creates a new `Length` object from an existing Length object + /// Creates a new object from an existing Length object /// /// public Length(Length length) { - this._value = length._value; + Value = length.Value; } /// /// Internal canonical value. /// - private readonly double _value; + private readonly double Value; /// /// The type of units available to describe the Length. /// public enum UnitType { - /// - /// Kilometers - /// + /// Kilometers Kilometers, - /// - /// Meters - /// + /// Meters Meters, - /// - /// Centimeters - /// + /// Centimeters Centimeters, - /// - /// Decimeters - /// + /// Decimeters Decimeters, - /// - /// Millimeters - /// + /// Millimeters Millimeters, - /// - /// Microns - /// + /// Microns Microns, - /// - /// Nanometers - /// + /// Nanometers Nanometers, - /// - /// Miles - /// + /// Miles Miles, - /// - /// Nautical miles - /// + /// Nautical miles NauticalMiles, - /// - /// Yards - /// + /// Yards Yards, - /// - /// Feet - /// + /// Feet Feet, - /// - /// Inches - /// + /// Inches Inches, } @@ -164,7 +140,7 @@ public enum UnitType /// the converted value [Pure] public readonly double From(UnitType convertTo) { - return LengthConversions.Convert(_value, UnitType.Meters, convertTo); + return LengthConversions.Convert(Value, UnitType.Meters, convertTo); } /// @@ -172,18 +148,13 @@ [Pure] public readonly double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] public override readonly bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Length)obj); - } + [Pure] public readonly override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object /// /// int32 hash value - [Pure] public override readonly int GetHashCode() => _value.GetHashCode(); + [Pure] public readonly override int GetHashCode() => Value.GetHashCode(); // implicit conversions //[Pure] public static implicit operator Length(ushort value) => new Length(value); @@ -201,7 +172,7 @@ [Pure] public override readonly bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public readonly bool Equals(Length other) => _value == other._value; + [Pure] public readonly bool Equals(Length other) => Value == other.Value; /// /// Equals operator to compare two Length objects @@ -209,7 +180,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(Length left, Length right) => Equals(left._value, right._value); + [Pure] public static bool operator ==(Length left, Length right) => Equals(left.Value, right.Value); /// /// Not equals operator to compare two Length objects @@ -217,14 +188,14 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(Length left, Length right) => !Equals(left._value, right._value); + [Pure] public static bool operator !=(Length left, Length right) => !Equals(left.Value, right.Value); /// /// Compare to another Length object /// /// /// 0 if equal - [Pure] public readonly int CompareTo(Length other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); + [Pure] public readonly int CompareTo(Length other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); /// /// Less than operator to compare two Length objects @@ -232,7 +203,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(Length left, Length right) => Comparer.Default.Compare(left._value, right._value) < 0; + [Pure] public static bool operator <(Length left, Length right) => Comparer.Default.Compare(left.Value, right.Value) < 0; /// /// Greater than operator to compare two Length objects @@ -240,7 +211,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(Length left, Length right) => Comparer.Default.Compare(left._value, right._value) > 0; + [Pure] public static bool operator >(Length left, Length right) => Comparer.Default.Compare(left.Value, right.Value) > 0; /// /// Less than or equal operator to compare two Length objects @@ -248,7 +219,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(Length left, Length right) => Comparer.Default.Compare(left._value, right._value) <= 0; + [Pure] public static bool operator <=(Length left, Length right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; /// /// Greater than or equal operator to compare two Length objects @@ -256,7 +227,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Length left, Length right) => Comparer.Default.Compare(left._value, right._value) >= 0; + [Pure] public static bool operator >=(Length left, Length right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; // Math /// @@ -265,7 +236,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// A new Length object with a value of left + right - [Pure] public static Length operator +(Length left, Length right) => new(left._value + right._value); + [Pure] public static Length operator +(Length left, Length right) => new(left.Value + right.Value); /// /// Subtraction operator to subtract two Length objects @@ -273,7 +244,7 @@ [Pure] public override readonly bool Equals(object obj) /// left value /// right value /// A new Length object with a value of left - right - [Pure] public static Length operator -(Length left, Length right) => new(left._value - right._value); + [Pure] public static Length operator -(Length left, Length right) => new(left.Value - right.Value); /// /// Multiplication operator to multiply by a double @@ -281,7 +252,7 @@ [Pure] public override readonly bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new Length object with a value of value multiplied by the operand - [Pure] public static Length operator *(Length value, double operand) => new(value._value * operand); + [Pure] public static Length operator *(Length value, double operand) => new(value.Value * operand); /// /// Division operator to divide by a double @@ -289,19 +260,19 @@ [Pure] public override readonly bool Equals(object obj) /// object to be divided /// operand to divide object /// A new Length object with a value of value divided by the operand - [Pure] public static Length operator /(Length value, double operand) => new(value._value / operand); + [Pure] public static Length operator /(Length value, double operand) => new(value.Value / operand); /// /// Returns the absolute value of the /// /// - [Pure] public readonly Length Abs() { return new Length(Math.Abs(this._value)); } + [Pure] public readonly Length Abs() { return new Length(Math.Abs(Value)); } /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override readonly string ToString() => _value.ToString(); + [Pure] public readonly override string ToString() => Value.ToString(); /// /// Get a string representation of the object @@ -309,7 +280,7 @@ [Pure] public override readonly bool Equals(object obj) /// format /// format provider /// A string representing the object - [Pure] public readonly string ToString(string format, IFormatProvider formatProvider) => _value.ToString(format, formatProvider); + [Pure] public readonly string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); // IComparable /// @@ -317,97 +288,106 @@ [Pure] public override readonly bool Equals(object obj) /// /// The other Length cast to object /// 0 if equal - [Pure] public readonly int CompareTo(object obj) => _value.CompareTo(obj); + [Pure] + public readonly int CompareTo(object obj) + { + if (obj is Length length) + { + return Value.CompareTo(length.Value); + } + + throw new ArgumentException("Object is not a Length"); + } /// /// Get type code of object /// /// The TypeCode - [Pure] public readonly TypeCode GetTypeCode() => _value.GetTypeCode(); + [Pure] public readonly TypeCode GetTypeCode() => Value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public readonly bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); + [Pure] public readonly bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public readonly byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); + [Pure] public readonly byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public readonly char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); + [Pure] public readonly char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public readonly DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); + [Pure] public readonly DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public readonly decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); + [Pure] public readonly decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public readonly double ToDouble(IFormatProvider provider) => _value; + [Pure] public readonly double ToDouble(IFormatProvider provider) => Value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public readonly short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + [Pure] public readonly short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public readonly int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + [Pure] public readonly int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public readonly long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + [Pure] public readonly long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public readonly sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + [Pure] public readonly sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public readonly float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); + [Pure] public readonly float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public readonly string ToString(IFormatProvider provider) => _value.ToString(provider); + [Pure] public readonly string ToString(IFormatProvider provider) => Value.ToString(provider); /// /// Convert to type @@ -415,28 +395,28 @@ [Pure] public override readonly bool Equals(object obj) /// type to covert to /// format provider /// type representation of the object - [Pure] public readonly object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); + [Pure] public readonly object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public readonly ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + [Pure] public readonly ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public readonly uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + [Pure] public readonly uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); /// /// Convert to uint64 /// /// format provider /// uint64 representation of the object - [Pure] public readonly ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); + [Pure] public readonly ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); /// /// Compare the default value to a double @@ -446,7 +426,7 @@ [Pure] public override readonly bool Equals(object obj) [Pure] public readonly int CompareTo(double? other) { - return (other is null) ? -1 : (_value).CompareTo(other.Value); + return (other is null) ? -1 : (Value).CompareTo(other.Value); } /// @@ -454,19 +434,19 @@ public readonly int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public readonly bool Equals(double? other) => _value.Equals(other); + [Pure] public readonly bool Equals(double? other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public readonly bool Equals(double other) => _value.Equals(other); + [Pure] public readonly bool Equals(double other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public readonly int CompareTo(double other) => _value.CompareTo(other); + [Pure] public readonly int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/MagneticField.cs b/Source/Meadow.Units/MagneticField.cs index b557c21..95a1587 100644 --- a/Source/Meadow.Units/MagneticField.cs +++ b/Source/Meadow.Units/MagneticField.cs @@ -5,424 +5,411 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents MagneticField +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct MagneticField : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents MagneticField + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct MagneticField : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The MagneticField value. + /// kilometers meters per second by default. + public MagneticField(double value, UnitType type = UnitType.Tesla) { - /// - /// Creates a new `MagneticField` object. - /// - /// The MagneticField value. - /// kilometers meters per second by default. - public MagneticField(double value, UnitType type = UnitType.Tesla) - { - Value = MagneticFieldConversions.Convert(value, type, UnitType.Tesla); - } + Value = MagneticFieldConversions.Convert(value, type, UnitType.Tesla); + } - /// - /// Creates a new `MagneticField` object from an existing MagneticField object - /// - /// - public MagneticField(MagneticField magneticField) - { - Value = magneticField.Value; - } + /// + /// Creates a new object from an existing MagneticField object + /// + /// + public MagneticField(MagneticField magneticField) + { + Value = magneticField.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the MagneticField. - /// - public enum UnitType - { - /// - /// MegaTelsa - /// - MegaTesla, - /// - /// KiloTesla - /// - KiloTesla, - /// - /// Tesla - /// - Tesla, - /// - /// MilliTesla - /// - MilliTesla, - /// - /// MicroTesla - /// - MicroTesla, - /// - /// NanoTesla - /// - NanoTesla, - /// - /// PicoTesla - /// - PicoTesla, - /// - /// Gauss - /// - Gauss - } + /// + /// The type of units available to describe the MagneticField. + /// + public enum UnitType + { + /// MegaTelsa + MegaTesla, + /// KiloTesla + KiloTesla, + /// Tesla + Tesla, + /// MilliTesla + MilliTesla, + /// MicroTesla + MicroTesla, + /// NanoTesla + NanoTesla, + /// PicoTesla + PicoTesla, + /// Gauss + Gauss + } - /// - /// Get the magnetic field value as MegaTesla - /// - public double MegaTesla => From(UnitType.MegaTesla); - /// - /// Get the magnetic field value as KiloTesla - /// - public double KiloTesla => From(UnitType.KiloTesla); - /// - /// Get the magnetic field value as Tesla - /// - public double Tesla => From(UnitType.Tesla); - /// - /// Get the magnetic field value as MilliTesla - /// - public double MilliTesla => From(UnitType.MilliTesla); - /// - /// Get the magnetic field value as MicroTesla - /// - public double MicroTesla => From(UnitType.MicroTesla); - /// - /// Get the magnetic field value as NanoTesla - /// - public double NanoTesla => From(UnitType.NanoTesla); - /// - /// Get the magnetic field value as PicoTesla - /// - public double PicoTesla => From(UnitType.PicoTesla); - /// - /// Get the magnetic field value as Gauss - /// - public double Gauss => From(UnitType.Gauss); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return MagneticFieldConversions.Convert(Value, UnitType.Tesla, convertTo); - } + /// + /// Get the magnetic field value as MegaTesla + /// + public double MegaTesla => From(UnitType.MegaTesla); + /// + /// Get the magnetic field value as KiloTesla + /// + public double KiloTesla => From(UnitType.KiloTesla); + /// + /// Get the magnetic field value as Tesla + /// + public double Tesla => From(UnitType.Tesla); + /// + /// Get the magnetic field value as MilliTesla + /// + public double MilliTesla => From(UnitType.MilliTesla); + /// + /// Get the magnetic field value as MicroTesla + /// + public double MicroTesla => From(UnitType.MicroTesla); + /// + /// Get the magnetic field value as NanoTesla + /// + public double NanoTesla => From(UnitType.NanoTesla); + /// + /// Get the magnetic field value as PicoTesla + /// + public double PicoTesla => From(UnitType.PicoTesla); + /// + /// Get the magnetic field value as Gauss + /// + public double Gauss => From(UnitType.Gauss); - /// - /// Compare to another MagneticField object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((MagneticField)obj); - } + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return MagneticFieldConversions.Convert(Value, UnitType.Tesla, convertTo); + } + + /// + /// Compare to another MagneticField object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator MagneticField(ushort value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(short value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(uint value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(long value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(int value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(float value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(double value) => new MagneticField(value); + //[Pure] public static implicit operator MagneticField(decimal value) => new MagneticField((double)value); + + // Comparison + /// + /// Compare to another MagneticField object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(MagneticField other) => Value == other.Value; + + /// + /// Equals operator to compare two MagneticField objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(MagneticField left, MagneticField right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two MagneticField objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(MagneticField left, MagneticField right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another MagneticField object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(MagneticField other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two MagneticField objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two MagneticField objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two MagneticField objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two MagneticField objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator MagneticField(ushort value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(short value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(uint value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(long value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(int value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(float value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(double value) => new MagneticField(value); - //[Pure] public static implicit operator MagneticField(decimal value) => new MagneticField((double)value); - - // Comparison - /// - /// Compare to another MagneticField object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(MagneticField other) => Value == other.Value; - - /// - /// Equals operator to compare two MagneticField objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(MagneticField left, MagneticField right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two MagneticField objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(MagneticField left, MagneticField right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another MagneticField object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(MagneticField other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two MagneticField objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two MagneticField objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two MagneticField objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two MagneticField objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(MagneticField left, MagneticField right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two MagneticField objects - /// - /// left value - /// right value - /// A new MagneticField object with a value of left + right - [Pure] public static MagneticField operator +(MagneticField left, MagneticField right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two MagneticField objects - /// - /// left value - /// right value - /// A new MagneticField object with a value of left - right - [Pure] public static MagneticField operator -(MagneticField left, MagneticField right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new MagneticField object with a value of value multiplied by the operand - [Pure] public static MagneticField operator *(MagneticField value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new MagneticField object with a value of value divided by the operand - [Pure] public static MagneticField operator /(MagneticField value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public MagneticField Abs() { return new MagneticField(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another MagneticField object - /// - /// The other MagneticField cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + // Math + /// + /// Addition operator to add two MagneticField objects + /// + /// left value + /// right value + /// A new MagneticField object with a value of left + right + [Pure] public static MagneticField operator +(MagneticField left, MagneticField right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two MagneticField objects + /// + /// left value + /// right value + /// A new MagneticField object with a value of left - right + [Pure] public static MagneticField operator -(MagneticField left, MagneticField right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new MagneticField object with a value of value multiplied by the operand + [Pure] public static MagneticField operator *(MagneticField value, double operand) => new (value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new MagneticField object with a value of value divided by the operand + [Pure] public static MagneticField operator /(MagneticField value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public MagneticField Abs() { return new MagneticField(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another MagneticField object + /// + /// The other MagneticField cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is MagneticField magneticField) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(magneticField.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a MagneticField"); } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); + } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/MagneticField3D.cs b/Source/Meadow.Units/MagneticField3D.cs index 0f92958..5494f4c 100644 --- a/Source/Meadow.Units/MagneticField3D.cs +++ b/Source/Meadow.Units/MagneticField3D.cs @@ -5,224 +5,226 @@ using System.Runtime.InteropServices; using MU = Meadow.Units.MagneticField.UnitType; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents a 3-dimensional (X,Y,Z) magnetic field. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct MagneticField3D : + IFormattable, IComparable, + IEquatable<(double X, double Y, double Z)>, + IComparable<(double, double, double)> { /// - /// Represents a 3-dimensional (X,Y,Z) magnetic field. + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct MagneticField3D : - IFormattable, IComparable, - IEquatable<(double X, double Y, double Z)>, - IComparable<(double, double, double)> + /// The X MagneticField3d value. + /// The Y MagneticField3d value. + /// The Z MagneticField3d value. + /// + public MagneticField3D(double x, double y, double z, MU type = MU.Tesla) { - /// - /// Creates a new `MagneticField3d` object. - /// - /// The X MagneticField3d value. - /// The Y MagneticField3d value. - /// The Z MagneticField3d value. - /// - public MagneticField3D(double x, double y, double z, MU type = MU.Tesla) - { - X = new MagneticField(x, type); - Y = new MagneticField(y, type); - Z = new MagneticField(z, type); - } + X = new MagneticField(x, type); + Y = new MagneticField(y, type); + Z = new MagneticField(z, type); + } - /// - /// Creates a new `MagneticField3D` object. - /// - /// The X MagneticField value. - /// The Y MagneticField value. - /// The Z MagneticField value. - public MagneticField3D(MagneticField x, MagneticField y, MagneticField z) - { - X = new MagneticField(x); - Y = new MagneticField(y); - Z = new MagneticField(z); - } + /// + /// Creates a new object. + /// + /// The X MagneticField value. + /// The Y MagneticField value. + /// The Z MagneticField value. + public MagneticField3D(MagneticField x, MagneticField y, MagneticField z) + { + X = new MagneticField(x); + Y = new MagneticField(y); + Z = new MagneticField(z); + } - /// - /// Creates a new `MagneticField3D` object from an existing object. - /// - /// Source object - public MagneticField3D(MagneticField3D magneticField3D) - { - this.X = new MagneticField(magneticField3D.X); - this.Y = new MagneticField(magneticField3D.Y); - this.Z = new MagneticField(magneticField3D.Z); - } + /// + /// Creates a new object from an existing object. + /// + /// Source object + public MagneticField3D(MagneticField3D magneticField3D) + { + X = new MagneticField(magneticField3D.X); + Y = new MagneticField(magneticField3D.Y); + Z = new MagneticField(magneticField3D.Z); + } - /// - /// X component of the magnetic field - /// - public MagneticField X { get; set; } - /// - /// Y component of the magnetic field - /// - public MagneticField Y { get; set; } - /// - /// Z component of the magnetic field - /// - public MagneticField Z { get; set; } - - /// - /// Compare to another MagneticField3D object - /// - /// object to compare to - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((MagneticField3D)obj); - } + /// + /// X component of the magnetic field + /// + public MagneticField X { get; set; } + /// + /// Y component of the magnetic field + /// + public MagneticField Y { get; set; } + /// + /// Z component of the magnetic field + /// + public MagneticField Z { get; set; } + + /// + /// Compare to another MagneticField3D object + /// + /// object to compare to + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; + + // Comparison + /// + /// Compare to another MagneticField3D object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(MagneticField3D other) => + X == other.X && + Y == other.Y && + Z == other.Z; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => (X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode()) / 3; - - // Comparison - /// - /// Compare to another AngularAcceleration object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(MagneticField3D other) => - X == other.X && - Y == other.Y && - Z == other.Z; - - /// - /// Equals operator to compare two MagneticField3D objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(MagneticField3D left, MagneticField3D right) => Equals(left, right); - - /// - /// Not equals operator to compare two MagneticField3D objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(MagneticField3D left, MagneticField3D right) => !Equals(left, right); + /// + /// Equals operator to compare two MagneticField3D objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(MagneticField3D left, MagneticField3D right) => Equals(left, right); + + /// + /// Not equals operator to compare two MagneticField3D objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(MagneticField3D left, MagneticField3D right) => !Equals(left, right); - /// - /// Less than operator to compare two MagneticField3D objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) < 0; - - /// - /// Greater than operator to compare two MagneticField3D objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) > 0; - - /// - /// Less than or equal operator to compare two MagneticField3D objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) <= 0; - - /// - /// Greater than or equal operator to compare two MagneticField3D objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) >= 0; - - // Math - /// - /// Addition operator to add two AngularAcceleration objects - /// - /// left value - /// right value - /// A new MagneticField3D object with a value of left + right - [Pure] public static MagneticField3D operator +(MagneticField3D left, MagneticField3D right) - { - var x = left.X + right.X; - var y = left.Y + right.Y; - var z = left.Z + right.Z; + /// + /// Less than operator to compare two MagneticField3D objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) < 0; - return new MagneticField3D(x, y, z); - } + /// + /// Greater than operator to compare two MagneticField3D objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) > 0; - /// - /// Subtraction operator to subtract two MagneticField3D objects - /// - /// left value - /// right value - /// A new MagneticField3D object with a value of left - right - [Pure] public static MagneticField3D operator -(MagneticField3D left, MagneticField3D right) - { - var x = left.X - right.X; - var y = left.Y - right.Y; - var z = left.Z - right.Z; + /// + /// Less than or equal operator to compare two MagneticField3D objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) <= 0; - return new MagneticField3D(x, y, z); - } + /// + /// Greater than or equal operator to compare two MagneticField3D objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(MagneticField3D left, MagneticField3D right) => Comparer.Default.Compare(left, right) >= 0; - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; - - // IComparable - /// - /// Compare to another MagneticField3D object - /// - /// The other MagneticField3D cast to object - /// - /// - public int CompareTo(object obj) - { - throw new NotImplementedException(); - } + // Math + /// + /// Addition operator to add two MagneticField3D objects + /// + /// left value + /// right value + /// A new MagneticField3D object with a value of left + right + [Pure] public static MagneticField3D operator +(MagneticField3D left, MagneticField3D right) + { + var x = left.X + right.X; + var y = left.Y + right.Y; + var z = left.Z + right.Z; - /// - /// Compare the default value to three double - /// - /// values to compare - /// true if equal - public bool Equals((double X, double Y, double Z) other) - { - return X.Equals(other.X) && - Y.Equals(other.Y) && - Z.Equals(other.Z); - } + return new MagneticField3D(x, y, z); + } + + /// + /// Subtraction operator to subtract two MagneticField3D objects + /// + /// left value + /// right value + /// A new MagneticField3D object with a value of left - right + [Pure] public static MagneticField3D operator -(MagneticField3D left, MagneticField3D right) + { + var x = left.X - right.X; + var y = left.Y - right.Y; + var z = left.Z - right.Z; + + return new MagneticField3D(x, y, z); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - public int CompareTo((double, double, double) other) + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => $"{X}, {Y}, {Z}"; + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => $"{X.ToString(format, formatProvider)}, {Y.ToString(format, formatProvider)}, {Z.ToString(format, formatProvider)}"; + + // IComparable + /// + /// Compare to another MagneticField3D object + /// + /// The other MagneticField3D cast to object + /// + public int CompareTo(object obj) + { + if (obj is MagneticField3D magneticField3D) { - throw new NotImplementedException(); + return X.CompareTo(magneticField3D.X) | + Y.CompareTo(magneticField3D.Y) | + Z.CompareTo(magneticField3D.Z); } + + throw new ArgumentException("Object is not a MagneticField3D"); + } + + /// + /// Compare the default value to three doubles + /// + /// values to compare + /// true if equal + public bool Equals((double X, double Y, double Z) other) + { + return X.Equals(other.X) && + Y.Equals(other.Y) && + Z.Equals(other.Z); + } + + /// + /// Compare the default value to a tuple of three doubles + /// + /// value to compare + /// 0 if equal + public int CompareTo((double, double, double) other) + { + return X.CompareTo(other.Item1) | + Y.CompareTo(other.Item2) | + Z.CompareTo(other.Item3); } } \ No newline at end of file diff --git a/Source/Meadow.Units/Mass.cs b/Source/Meadow.Units/Mass.cs index 0e80cac..8556320 100644 --- a/Source/Meadow.Units/Mass.cs +++ b/Source/Meadow.Units/Mass.cs @@ -5,432 +5,417 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents mass, or weight of an object +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Mass : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents mass, or weight of an object + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Mass : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The mass value. + /// Grams by default. + public Mass(double value, UnitType type = UnitType.Grams) { - /// - /// Creates a new `Mass` object. - /// - /// The mass value. - /// Grams by default. - public Mass(double value, UnitType type = UnitType.Grams) - { - Value = MassConversions.Convert(value, type, UnitType.Grams); - } + Value = MassConversions.Convert(value, type, UnitType.Grams); + } - /// - /// Creates a new `Mass` object from an existing Mass object - /// - /// - public Mass(Mass mass) - { - Value = mass.Value; - } + /// + /// Creates a new object from an existing Mass object + /// + /// + public Mass(Mass mass) + { + Value = mass.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Mass. - /// - public enum UnitType - { - /// - /// Grams - /// - Grams, - /// - /// Kilograms - /// - Kilograms, - /// - /// Ounces - /// - Ounces, - /// - /// Pounds - /// - Pounds, - /// - /// Tons metric - /// - TonsMetric, - /// - /// Tons US short - /// - TonsUSShort, - /// - /// Tons UK long - /// - TonsUKLong, - /// - /// Grains - /// - Grains, - /// - /// Carats - /// - Carats - } + /// + /// The type of units available to describe the Mass. + /// + public enum UnitType + { + /// Grams + Grams, + /// Kilograms + Kilograms, + /// Ounces + Ounces, + /// Pounds + Pounds, + /// Tons metric + TonsMetric, + /// Tons US short + TonsUSShort, + /// Tons UK long + TonsUKLong, + /// Grains + Grains, + /// Carats + Carats + } - /// - /// Mass in grams - /// - public double Grams => From(UnitType.Grams); - /// - /// Mass in kilograms - /// - public double Kilograms => From(UnitType.Kilograms); - /// - /// Mass in ounces - /// - public double Ounces => From(UnitType.Ounces); - /// - /// Mass in pounds - /// - public double Pounds => From(UnitType.Pounds); - /// - /// Mass in tons metric - /// - public double TonsMetric => From(UnitType.TonsMetric); - /// - /// Mass in tons US short - /// - public double TonsUSShort => From(UnitType.TonsUSShort); - /// - /// Mass in tons UK long - /// - public double TonsUKLong => From(UnitType.TonsUKLong); - /// - /// Mass in grains - /// - public double Grains => From(UnitType.Grains); - /// - /// Mass in karats - /// - public double Karats => From(UnitType.Carats); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return MassConversions.Convert(Value, UnitType.Grams, convertTo); - } + /// + /// Mass in grams + /// + public double Grams => From(UnitType.Grams); + /// + /// Mass in kilograms + /// + public double Kilograms => From(UnitType.Kilograms); + /// + /// Mass in ounces + /// + public double Ounces => From(UnitType.Ounces); + /// + /// Mass in pounds + /// + public double Pounds => From(UnitType.Pounds); + /// + /// Mass in tons metric + /// + public double TonsMetric => From(UnitType.TonsMetric); + /// + /// Mass in tons US short + /// + public double TonsUSShort => From(UnitType.TonsUSShort); + /// + /// Mass in tons UK long + /// + public double TonsUKLong => From(UnitType.TonsUKLong); + /// + /// Mass in grains + /// + public double Grains => From(UnitType.Grains); + /// + /// Mass in karats + /// + public double Karats => From(UnitType.Carats); - /// - /// Compare to another Mass object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Mass)obj); - } + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return MassConversions.Convert(Value, UnitType.Grams, convertTo); + } + + /// + /// Compare to another Mass object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Mass(ushort value) => new Mass(value); + //[Pure] public static implicit operator Mass(short value) => new Mass(value); + //[Pure] public static implicit operator Mass(uint value) => new Mass(value); + //[Pure] public static implicit operator Mass(long value) => new Mass(value); + //[Pure] public static implicit operator Mass(int value) => new Mass(value); + //[Pure] public static implicit operator Mass(float value) => new Mass(value); + //[Pure] public static implicit operator Mass(double value) => new Mass(value); + //[Pure] public static implicit operator Mass(decimal value) => new Mass((double)value); + + // Comparison + /// + /// Compare to another Mass object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Mass other) => Value == other.Value; + + /// + /// Equals operator to compare two Mass objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Mass left, Mass right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Mass objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Mass left, Mass right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Mass object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Mass other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Mass objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Mass objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Mass objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Mass objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Mass objects + /// + /// left value + /// right value + /// A new Mass object with a value of left + right + [Pure] public static Mass operator +(Mass left, Mass right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Mass objects + /// + /// left value + /// right value + /// A new Mass object with a value of left - right + [Pure] public static Mass operator -(Mass left, Mass right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Mass object with a value of value multiplied by the operand + [Pure] public static Mass operator *(Mass value, double operand) => new (value.Value * operand); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Mass(ushort value) => new Mass(value); - //[Pure] public static implicit operator Mass(short value) => new Mass(value); - //[Pure] public static implicit operator Mass(uint value) => new Mass(value); - //[Pure] public static implicit operator Mass(long value) => new Mass(value); - //[Pure] public static implicit operator Mass(int value) => new Mass(value); - //[Pure] public static implicit operator Mass(float value) => new Mass(value); - //[Pure] public static implicit operator Mass(double value) => new Mass(value); - //[Pure] public static implicit operator Mass(decimal value) => new Mass((double)value); - - // Comparison - /// - /// Compare to another Mass object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Mass other) => Value == other.Value; - - /// - /// Equals operator to compare two Mass objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Mass left, Mass right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Mass objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Mass left, Mass right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Mass object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Mass other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Mass objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Mass objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Mass objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Mass objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Mass left, Mass right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Mass objects - /// - /// left value - /// right value - /// A new Mass object with a value of left + right - [Pure] public static Mass operator +(Mass left, Mass right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Mass objects - /// - /// left value - /// right value - /// A new Mass object with a value of left - right - [Pure] public static Mass operator -(Mass left, Mass right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Mass object with a value of value multiplied by the operand - [Pure] public static Mass operator *(Mass value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Mass object with a value of value divided by the operand - [Pure] public static Mass operator /(Mass value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Mass Abs() { return new Mass(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Mass object - /// - /// The other Mass cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// conversion type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Mass object with a value of value divided by the operand + [Pure] public static Mass operator /(Mass value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Mass Abs() { return new Mass(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Mass object + /// + /// The other Mass cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Mass mass) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(mass.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Mass"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// conversion type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/ParticleDensity.cs b/Source/Meadow.Units/ParticleDensity.cs index e64f0d4..2bb88da 100644 --- a/Source/Meadow.Units/ParticleDensity.cs +++ b/Source/Meadow.Units/ParticleDensity.cs @@ -5,359 +5,355 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents the density of particles per volume (typically in air) +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct ParticleDensity : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents the density of particles per volume (typically in air) + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct ParticleDensity : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Density value. + /// Kilograms per meters cubed by default. + public ParticleDensity(double value, UnitType type = UnitType.ParticlesPerLiter) { - /// - /// Creates a new `ParticleDensity` object. - /// - /// The Density value. - /// Kilograms per meters cubed by default. - public ParticleDensity(double value, UnitType type = UnitType.ParticlesPerLiter) - { - Value = ParticleDensityConversions.Convert(value, type, UnitType.ParticlesPerLiter); - } + Value = ParticleDensityConversions.Convert(value, type, UnitType.ParticlesPerLiter); + } - /// - /// Creates a new `ParticleDensity` object from an existing ParticleDensity object - /// - /// - public ParticleDensity(ParticleDensity density) - { - Value = density.Value; - } + /// + /// Creates a new object from an existing ParticleDensity object + /// + /// + public ParticleDensity(ParticleDensity density) + { + Value = density.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the ParticleDensity. - /// - public enum UnitType - { - /// - /// Particles per liter - /// - ParticlesPerLiter, - /// - /// Particles per centiliter - /// - ParticlesPerCentiliter, - /// - /// Particles per milliliter - /// - ParticlesPerMilliliter, - } + /// + /// The type of units available to describe the ParticleDensity. + /// + public enum UnitType + { + /// Particles per liter + ParticlesPerLiter, + /// Particles per centiliter + ParticlesPerCentiliter, + /// Particles per milliliter + ParticlesPerMilliliter, + } - /// - /// Get the particle density in particles per liter - /// - public double ParticlesPerLiter => From(UnitType.ParticlesPerLiter); - - /// - /// Get the particle density in particles per centiliter - /// - public double ParticlesPerCentiliter => From(UnitType.ParticlesPerCentiliter); - - /// - /// Get the particle density in particles per milliliter - /// - public double ParticlesPerMilliliter => From(UnitType.ParticlesPerMilliliter); - - /// - /// Get a double value for a specific unit - /// - /// unit to convert to - /// the converted value - [Pure] - public double From(UnitType convertTo) - { - return ParticleDensityConversions.Convert(Value, UnitType.ParticlesPerLiter, convertTo); - } + /// + /// Get the particle density in particles per liter + /// + public double ParticlesPerLiter => From(UnitType.ParticlesPerLiter); - /// - /// Compare to another ParticleDensity object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Density)obj); - } + /// + /// Get the particle density in particles per centiliter + /// + public double ParticlesPerCentiliter => From(UnitType.ParticlesPerCentiliter); + + /// + /// Get the particle density in particles per milliliter + /// + public double ParticlesPerMilliliter => From(UnitType.ParticlesPerMilliliter); + + /// + /// Get a double value for a specific unit + /// + /// unit to convert to + /// the converted value + [Pure] + public double From(UnitType convertTo) + { + return ParticleDensityConversions.Convert(Value, UnitType.ParticlesPerLiter, convertTo); + } + + /// + /// Compare to another ParticleDensity object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // Comparison + /// + /// Compare to another ParticleDensity object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(ParticleDensity other) => Value == other.Value; + + /// + /// Equals operator to compare two ParticleDensity objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(ParticleDensity left, ParticleDensity right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two ParticleDensity objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(ParticleDensity left, ParticleDensity right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another ParticleDensity object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(ParticleDensity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two ParticleDensity objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two ParticleDensity objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two ParticleDensity objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two ParticleDensity objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two ParticleDensity objects + /// + /// left value + /// right value + /// A new Density object with a value of left + right + [Pure] public static ParticleDensity operator +(ParticleDensity left, ParticleDensity right) => new(left.Value + right.Value); + + /// + /// Subtraction operator to subtract two ParticleDensity objects + /// + /// left value + /// right value + /// A new Density object with a value of left - right + [Pure] public static ParticleDensity operator -(ParticleDensity left, ParticleDensity right) => new(left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Density object with a value of value multiplied by the operand + [Pure] public static ParticleDensity operator *(ParticleDensity value, double operand) => new(value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Density object with a value of value divided by the operand + [Pure] public static ParticleDensity operator /(ParticleDensity value, double operand) => new(value.Value / operand); + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // Comparison - /// - /// Compare to another ParticleDensity object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(ParticleDensity other) => Value == other.Value; - - /// - /// Equals operator to compare two ParticleDensity objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(ParticleDensity left, ParticleDensity right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two ParticleDensity objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(ParticleDensity left, ParticleDensity right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another ParticleDensity object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(ParticleDensity other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two ParticleDensity objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two ParticleDensity objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two ParticleDensity objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two ParticleDensity objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(ParticleDensity left, ParticleDensity right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two ParticleDensity objects - /// - /// left value - /// right value - /// A new Density object with a value of left + right - [Pure] public static ParticleDensity operator +(ParticleDensity left, ParticleDensity right) => new(left.Value + right.Value); - - /// - /// Subtraction operator to subtract two ParticleDensity objects - /// - /// left value - /// right value - /// A new Density object with a value of left - right - [Pure] public static ParticleDensity operator -(ParticleDensity left, ParticleDensity right) => new(left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Density object with a value of value multiplied by the operand - [Pure] public static ParticleDensity operator *(ParticleDensity value, double operand) => new(value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Density object with a value of value divided by the operand - [Pure] public static ParticleDensity operator /(ParticleDensity value, double operand) => new(value.Value / operand); - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Density object - /// - /// The other Density cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) + // IComparable + /// + /// Compare to another ParticleDensity object + /// + /// The other ParticleDensity cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is ParticleDensity particleDensity) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(particleDensity.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); - - /// - public bool ToBoolean(IFormatProvider provider) => throw new NotSupportedException(); - /// - public char ToChar(IFormatProvider provider) => throw new NotSupportedException(); - /// - public DateTime ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); + throw new ArgumentException("Object is not a ParticleDensity"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); + + /// + public bool ToBoolean(IFormatProvider provider) => throw new NotSupportedException(); + /// + public char ToChar(IFormatProvider provider) => throw new NotSupportedException(); + /// + public DateTime ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); } \ No newline at end of file diff --git a/Source/Meadow.Units/PotentialHydrogen.cs b/Source/Meadow.Units/PotentialHydrogen.cs index 83074ca..beffaa0 100644 --- a/Source/Meadow.Units/PotentialHydrogen.cs +++ b/Source/Meadow.Units/PotentialHydrogen.cs @@ -4,354 +4,354 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents potential hydrogen (ph) +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct PotentialHydrogen : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents potential hydrogen (ph) + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct PotentialHydrogen : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Potential Hydrogen value. + /// Potential Hydrogen unit. + public PotentialHydrogen(double value, UnitType type = UnitType.pH) { - /// - /// Creates a new `PotentialHydrogen` object. - /// - /// The Potential Hydrogen value. - /// Potential Hydrogen unit. - public PotentialHydrogen(double value, UnitType type = UnitType.pH) - { - pH = value; - } + pH = value; + } - /// - /// Creates a new `PotentialHydrogen` object from an existing PotentialHydrogen object - /// - /// - public PotentialHydrogen(PotentialHydrogen PotentialHydrogen) - { - pH = PotentialHydrogen.pH; - } + /// + /// Creates a new object from an existing PotentialHydrogen object + /// + /// + public PotentialHydrogen(PotentialHydrogen PotentialHydrogen) + { + pH = PotentialHydrogen.pH; + } - /// - /// The Potential Hydrogen expressed as pH. - /// - public double pH { get; private set; } - /// - /// The type of units available to describe the PotentialHydrogen. - /// - public enum UnitType - { - /// - /// Potential Hydrogen (pH) - /// - pH - } + /// + /// The Potential Hydrogen expressed as pH. + /// + public double pH { get; private set; } + /// + /// The type of units available to describe the PotentialHydrogen. + /// + public enum UnitType + { + /// Potential Hydrogen (pH) + pH + } - /// - /// Compare to another PotentialHydrogen object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((PotentialHydrogen)obj); - } + /// + /// Compare to another PotentialHydrogen object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => pH.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator PotentialHydrogen(ushort value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(short value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(uint value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(long value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(int value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(float value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(double value) => new PotentialHydrogen(value); + //[Pure] public static implicit operator PotentialHydrogen(decimal value) => new PotentialHydrogen((double)value); + + // Comparison + /// + /// Compare to another PotentialHydrogen object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(PotentialHydrogen other) => pH == other.pH; + + /// + /// Equals operator to compare two PotentialHydrogen objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(PotentialHydrogen left, PotentialHydrogen right) => Equals(left.pH, right.pH); + + /// + /// Not equals operator to compare two PotentialHydrogen objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(PotentialHydrogen left, PotentialHydrogen right) => !Equals(left.pH, right.pH); + + /// + /// Compare to another PotentialHydrogen object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(PotentialHydrogen other) => Equals(pH, other.pH) ? 0 : pH.CompareTo(other.pH); + + /// + /// Less than operator to compare two PotentialHydrogen objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) < 0; + + /// + /// Greater than operator to compare two PotentialHydrogen objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) > 0; + + /// + /// Less than or equal operator to compare two PotentialHydrogen objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) <= 0; + + /// + /// Greater than or equal operator to compare two PotentialHydrogen objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) >= 0; + + // Math + /// + /// Addition operator to add two PotentialHydrogen objects + /// + /// left value + /// right value + /// A new PotentialHydrogen object with a value of left + right + [Pure] public static PotentialHydrogen operator +(PotentialHydrogen left, PotentialHydrogen right) => new(left.pH + right.pH); + + /// + /// Subtraction operator to subtract two PotentialHydrogen objects + /// + /// left value + /// right value + /// A new PotentialHydrogen object with a value of left - right + [Pure] public static PotentialHydrogen operator -(PotentialHydrogen left, PotentialHydrogen right) => new(left.pH - right.pH); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new PotentialHydrogen object with a value of value multiplied by the operand + [Pure] public static PotentialHydrogen operator *(PotentialHydrogen value, double operand) => new(value.pH * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new PotentialHydrogen object with a value of value divided by the operand + [Pure] public static PotentialHydrogen operator /(PotentialHydrogen value, double operand) => new(value.pH / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public PotentialHydrogen Abs() { return new PotentialHydrogen(Math.Abs(pH)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => pH.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => pH.ToString(format, formatProvider); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => pH.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator PotentialHydrogen(ushort value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(short value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(uint value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(long value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(int value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(float value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(double value) => new PotentialHydrogen(value); - //[Pure] public static implicit operator PotentialHydrogen(decimal value) => new PotentialHydrogen((double)value); - - // Comparison - /// - /// Compare to another PotentialHydrogen object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(PotentialHydrogen other) => pH == other.pH; - - /// - /// Equals operator to compare two PotentialHydrogen objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(PotentialHydrogen left, PotentialHydrogen right) => Equals(left.pH, right.pH); - - /// - /// Not equals operator to compare two PotentialHydrogen objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(PotentialHydrogen left, PotentialHydrogen right) => !Equals(left.pH, right.pH); - - /// - /// Compare to another PotentialHydrogen object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(PotentialHydrogen other) => Equals(pH, other.pH) ? 0 : pH.CompareTo(other.pH); - - /// - /// Less than operator to compare two PotentialHydrogen objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) < 0; - - /// - /// Greater than operator to compare two PotentialHydrogen objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) > 0; - - /// - /// Less than or equal operator to compare two PotentialHydrogen objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) <= 0; - - /// - /// Greater than or equal operator to compare two PotentialHydrogen objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(PotentialHydrogen left, PotentialHydrogen right) => Comparer.Default.Compare(left.pH, right.pH) >= 0; - - // Math - /// - /// Addition operator to add two PotentialHydrogen objects - /// - /// left value - /// right value - /// A new PotentialHydrogen object with a value of left + right - [Pure] public static PotentialHydrogen operator +(PotentialHydrogen left, PotentialHydrogen right) => new(left.pH + right.pH); - - /// - /// Subtraction operator to subtract two PotentialHydrogen objects - /// - /// left value - /// right value - /// A new PotentialHydrogen object with a value of left - right - [Pure] public static PotentialHydrogen operator -(PotentialHydrogen left, PotentialHydrogen right) => new(left.pH - right.pH); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new PotentialHydrogen object with a value of value multiplied by the operand - [Pure] public static PotentialHydrogen operator *(PotentialHydrogen value, double operand) => new(value.pH * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new PotentialHydrogen object with a value of value divided by the operand - [Pure] public static PotentialHydrogen operator /(PotentialHydrogen value, double operand) => new(value.pH / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public PotentialHydrogen Abs() { return new PotentialHydrogen(Math.Abs(this.pH)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => pH.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => pH.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another PotentialHydrogen object - /// - /// The other PotentialHydrogen cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => pH.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => pH.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)pH).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)pH).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)pH).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)pH).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)pH).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => pH; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)pH).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)pH).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)pH).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)pH).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)pH).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => pH.ToString(provider); - - /// - /// Convert to type - /// - /// unit to convert - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)pH).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)pH).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)pH).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)pH).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) + // IComparable + /// + /// Compare to another PotentialHydrogen object + /// + /// The other PotentialHydrogen cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is PotentialHydrogen potentialHydrogen) { - return (other is null) ? -1 : (pH).CompareTo(other.Value); + return pH.CompareTo(potentialHydrogen.pH); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => pH.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => pH.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => pH.CompareTo(other); + throw new ArgumentException("Object is not a PotentialHydrogen"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => pH.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)pH).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)pH).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)pH).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)pH).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)pH).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => pH; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)pH).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)pH).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)pH).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)pH).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)pH).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => pH.ToString(provider); + + /// + /// Convert to type + /// + /// unit to convert + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)pH).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)pH).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)pH).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)pH).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (pH).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => pH.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => pH.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => pH.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Power.cs b/Source/Meadow.Units/Power.cs index 122c462..ad8e677 100644 --- a/Source/Meadow.Units/Power.cs +++ b/Source/Meadow.Units/Power.cs @@ -30,102 +30,68 @@ static Power() public static Power Zero => _zero; /// - /// Creates a new `Power` object. + /// Creates a new object. /// /// The Power value. /// kilometers meters per second by default. public Power(double value, UnitType type = UnitType.Watts) { - _value = PowerConversions.Convert(value, type, UnitType.Watts); + Value = PowerConversions.Convert(value, type, UnitType.Watts); } /// - /// Creates a new `Power` object from an existing Power object + /// Creates a new object from an existing Power object /// /// public Power(Power power) { - this._value = power._value; + Value = power.Value; } /// /// Internal canonical value. /// - private readonly double _value; + private readonly double Value; /// /// The type of units available to describe the Power. /// public enum UnitType { - /// - /// Gigawatts - /// + /// Gigawatts Gigawatts, - /// - /// Megawatts - /// + /// Megawatts Megawatts, - /// - /// Kilowatts - /// + /// Kilowatts Kilowatts, - /// - /// Watts - /// + /// Watts Watts, - /// - /// Milliwatts - /// + /// Milliwatts Milliwatts, - /// - /// Horsepower - metric - /// + /// Horsepower - metric HorsePowerMetric, - /// - /// Horsepower - IT - /// + /// Horsepower - IT HorsePowerIT, - /// - /// Calories per second - /// + /// Calories per second CaloriesPerSecond, - /// - /// Calories per minute - /// + /// Calories per minute CaloriesPerMinute, - /// - /// Calories per hour - /// + /// Calories per hour CaloriesPerHour, - /// - /// BTUs per second - /// + /// BTUs per second BTUsPerSecond, - /// - /// BTUs per minute - /// + /// BTUs per minute BTUsPerMinute, - /// - /// BTUs per hour - /// + /// BTUs per hour BTUsPerHour, - /// - /// Foot-pounds per second - /// + /// Foot-pounds per second FootPoundsPerSecond, - /// - /// Foot-pounds per minute - /// + /// Foot-pounds per minute FootPoundsPerMinute, - /// - /// Foot-pounds per hour - /// + /// Foot-pounds per hour FootPoundsPerHour, - /// - /// Tons refrigeration - /// - TonsRefridgeration + /// Tons refrigeration + TonsRefrigeration } /// @@ -195,7 +161,7 @@ public enum UnitType /// /// Power in tons refrigeration /// - public double TonsRefridgeration => From(UnitType.TonsRefridgeration); + public double TonsRefridgeration => From(UnitType.TonsRefrigeration); /// /// Get a double value for a specific unit @@ -205,7 +171,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return PowerConversions.Convert(_value, UnitType.Watts, convertTo); + return PowerConversions.Convert(Value, UnitType.Watts, convertTo); } /// @@ -213,19 +179,13 @@ public double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Power)obj); - } + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object /// /// int32 hash value - [Pure] public override int GetHashCode() => _value.GetHashCode(); + [Pure] public override int GetHashCode() => Value.GetHashCode(); // implicit conversions //[Pure] public static implicit operator Power(ushort value) => new Power(value); @@ -243,7 +203,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(Power other) => _value == other._value; + [Pure] public bool Equals(Power other) => Value == other.Value; /// /// Equals operator to compare two Power objects @@ -251,7 +211,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(Power left, Power right) => Equals(left._value, right._value); + [Pure] public static bool operator ==(Power left, Power right) => Equals(left.Value, right.Value); /// /// Not equals operator to compare two Power objects @@ -259,14 +219,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(Power left, Power right) => !Equals(left._value, right._value); + [Pure] public static bool operator !=(Power left, Power right) => !Equals(left.Value, right.Value); /// /// Compare to another Power object /// /// /// 0 if equal - [Pure] public int CompareTo(Power other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); + [Pure] public int CompareTo(Power other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); /// /// Less than operator to compare two Power objects @@ -274,7 +234,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) < 0; + [Pure] public static bool operator <(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) < 0; /// /// Greater than operator to compare two Power objects @@ -282,7 +242,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) > 0; + [Pure] public static bool operator >(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) > 0; /// /// Less than or equal operator to compare two Power objects @@ -290,7 +250,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) <= 0; + [Pure] public static bool operator <=(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; /// /// Greater than or equal operator to compare two Power objects @@ -298,7 +258,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Power left, Power right) => Comparer.Default.Compare(left._value, right._value) >= 0; + [Pure] public static bool operator >=(Power left, Power right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; // Math /// @@ -307,7 +267,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Power object with a value of left + right - [Pure] public static Power operator +(Power left, Power right) => new(left._value + right._value); + [Pure] public static Power operator +(Power left, Power right) => new(left.Value + right.Value); /// /// Subtraction operator to subtract two Power objects @@ -315,7 +275,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Power object with a value of left - right - [Pure] public static Power operator -(Power left, Power right) => new(left._value - right._value); + [Pure] public static Power operator -(Power left, Power right) => new(left.Value - right.Value); /// /// Multiplication operator to multiply by a double @@ -323,7 +283,7 @@ public override bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new Power object with a value of value multiplied by the operand - [Pure] public static Power operator *(Power value, double operand) => new(value._value * operand); + [Pure] public static Power operator *(Power value, double operand) => new(value.Value * operand); /// /// Division operator to divide by a double @@ -331,19 +291,19 @@ public override bool Equals(object obj) /// object to be divided /// operand to divide object /// A new Power object with a value of value divided by the operand - [Pure] public static Power operator /(Power value, double operand) => new(value._value / operand); + [Pure] public static Power operator /(Power value, double operand) => new(value.Value / operand); /// /// Returns the absolute value of the /// /// - [Pure] public Power Abs() { return new Power(Math.Abs(this._value)); } + [Pure] public Power Abs() { return new Power(Math.Abs(Value)); } /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => _value.ToString(); + [Pure] public override string ToString() => Value.ToString(); /// /// Get a string representation of the object @@ -351,7 +311,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => _value.ToString(format, formatProvider); + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); // IComparable /// @@ -359,97 +319,106 @@ public override bool Equals(object obj) /// /// The other Power cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is Power power) + { + return Value.CompareTo(power.Value); + } + + throw new ArgumentException("Object is not a Power"); + } /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => _value; + [Pure] public double ToDouble(IFormatProvider provider) => Value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); /// /// Convert to type @@ -457,28 +426,28 @@ public override bool Equals(object obj) /// conversion type /// format provider /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); /// /// Convert to uint64 /// /// format provider /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); /// /// Compare the default value to a double @@ -488,7 +457,7 @@ public override bool Equals(object obj) [Pure] public int CompareTo(double? other) { - return (other is null) ? -1 : (_value).CompareTo(other.Value); + return (other is null) ? -1 : (Value).CompareTo(other.Value); } /// @@ -496,19 +465,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => _value.Equals(other); + [Pure] public bool Equals(double? other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double other) => _value.Equals(other); + [Pure] public bool Equals(double other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public int CompareTo(double other) => _value.CompareTo(other); + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Pressure.cs b/Source/Meadow.Units/Pressure.cs index d4a3bfc..3960a71 100644 --- a/Source/Meadow.Units/Pressure.cs +++ b/Source/Meadow.Units/Pressure.cs @@ -5,413 +5,407 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents pressure; the force applied perpendicular to the surface of +/// an object per unit area over which that force is distributed. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Pressure : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents pressure; the force applied perpendicular to the surface of - /// an object per unit area over which that force is distributed. + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Pressure : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The pressure value. + /// _Bar_ (`Bar`), by default. + public Pressure(double value, UnitType type = UnitType.Bar) { - /// - /// Creates a new `Pressure` object. - /// - /// The pressure value. - /// _Bar_ (`Bar`), by default. - public Pressure(double value, UnitType type = UnitType.Bar) - { - Value = PressureConversions.Convert(value, type, UnitType.Bar); - } + Value = PressureConversions.Convert(value, type, UnitType.Bar); + } - /// - /// Creates a new `Pressure` object from an existing Pressure object - /// - /// - public Pressure(Pressure pressure) - { - Value = pressure.Value; - } + /// + /// Creates a new object from an existing Pressure object + /// + /// + public Pressure(Pressure pressure) + { + Value = pressure.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Pressure. - /// - public enum UnitType - { - /// - /// Bar - /// - Bar, - /// - /// Pascal - /// - Pascal, - /// - /// Psi - /// - Psi, - /// - /// Standard atmospheric pressure - /// - StandardAtmosphere, - /// - /// Millibar - /// - Millibar, - /// - /// Hectopascal - /// - Hectopascal, - /// - /// Kilopascal - /// - KiloPascal - } + /// + /// The type of units available to describe the Pressure. + /// + public enum UnitType + { + /// Bar + Bar, + /// Pascal + Pascal, + /// Psi + Psi, + /// Standard atmospheric pressure + StandardAtmosphere, + /// Millibar + Millibar, + /// Hectopascal + Hectopascal, + /// Kilopascal + KiloPascal + } - /// - /// Gets the pressure value expressed as a unit _Bar_ (`Bar`) - /// - public double Bar { get => Value; } - /// - /// Gets the pressure value expressed as a unit _Pascal_ (`Pa`). - /// - public double Pascal { get => From(UnitType.Pascal); } - /// - /// Gets the pressure value expressed as a unit _Pound-force per square inch_ (`Psi`). - /// - public double Psi { get => From(UnitType.Psi); } - /// - /// Gets the pressure value expressed as a unit _Standard Atmosphere_ (`At`). - /// - public double StandardAtmosphere { get => From(UnitType.StandardAtmosphere); } - /// - /// Gets the pressure value expressed as a unit _Bar_ (`Bar`) - /// - public double Millibar { get => From(UnitType.Millibar); } - /// - /// Gets the pressure value expressed as a unit _Bar_ (`Bar`) - /// - public double Hectopascal { get => From(UnitType.Hectopascal); } - - /// - /// Get pressure value for a specific unit - /// - /// the pressure unit - /// - [Pure] public double From(UnitType convertTo) - { - return PressureConversions.Convert(Value, UnitType.Bar, convertTo); - } + /// + /// Gets the pressure value expressed as a unit _Bar_ (`Bar`) + /// + public double Bar => Value; - /// - /// Compare to another Pressure object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Pressure)obj); - } + /// + /// Gets the pressure value expressed as a unit _Pascal_ (`Pa`). + /// + public double Pascal => From(UnitType.Pascal); + + /// + /// Gets the pressure value expressed as a unit _Pound-force per square inch_ (`Psi`). + /// + public double Psi => From(UnitType.Psi); + + /// + /// Gets the pressure value expressed as a unit _Standard Atmosphere_ (`At`). + /// + public double StandardAtmosphere => From(UnitType.StandardAtmosphere); + + /// + /// Gets the pressure value expressed as a unit _Bar_ (`Bar`) + /// + public double Millibar => From(UnitType.Millibar); + + /// + /// Gets the pressure value expressed as a unit _Bar_ (`Bar`) + /// + public double Hectopascal => From(UnitType.Hectopascal); + + /// + /// Get pressure value for a specific unit + /// + /// the pressure unit + /// + [Pure] public double From(UnitType convertTo) + { + return PressureConversions.Convert(Value, UnitType.Bar, convertTo); + } + + /// + /// Compare to another Pressure object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Pressure(ushort value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(short value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(uint value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(long value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(int value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(float value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(double value) => new Pressure(value); + //[Pure] public static implicit operator Pressure(decimal value) => new Pressure((double)value); + + // Comparison + /// + /// Compare to another Pressure object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Pressure other) => Value == other.Value; + + /// + /// Equals operator to compare two Pressure objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Pressure left, Pressure right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Pressure objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Pressure left, Pressure right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Pressure object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Pressure other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Pressure objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Pressure objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Pressure objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Pressure objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Pressure objects + /// + /// left value + /// right value + /// A new Pressure object with a value of left + right + [Pure] public static Pressure operator +(Pressure left, Pressure right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Pressure objects + /// + /// left value + /// right value + /// A new Pressure object with a value of left - right + [Pure] public static Pressure operator -(Pressure left, Pressure right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Pressure object with a value of value multiplied by the operand + [Pure] public static Pressure operator *(Pressure value, double operand) => new (value.Value * operand); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Pressure(ushort value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(short value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(uint value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(long value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(int value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(float value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(double value) => new Pressure(value); - //[Pure] public static implicit operator Pressure(decimal value) => new Pressure((double)value); - - // Comparison - /// - /// Compare to another Pressure object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Pressure other) => Value == other.Value; - - /// - /// Equals operator to compare two Pressure objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Pressure left, Pressure right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Pressure objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Pressure left, Pressure right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Pressure object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Pressure other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Pressure objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Pressure objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Pressure objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Pressure objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Pressure left, Pressure right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Pressure objects - /// - /// left value - /// right value - /// A new Pressure object with a value of left + right - [Pure] public static Pressure operator +(Pressure left, Pressure right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Pressure objects - /// - /// left value - /// right value - /// A new Pressure object with a value of left - right - [Pure] public static Pressure operator -(Pressure left, Pressure right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Pressure object with a value of value multiplied by the operand - [Pure] public static Pressure operator *(Pressure value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Pressure object with a value of value divided by the operand - [Pure] public static Pressure operator /(Pressure value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Pressure Abs() { return new Pressure(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Pressure object - /// - /// The other Pressure cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// conversion type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Pressure object with a value of value divided by the operand + [Pure] public static Pressure operator /(Pressure value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Pressure Abs() { return new Pressure(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Pressure object + /// + /// The other Pressure cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Pressure pressure) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(pressure.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Pressure"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// conversion type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/ReactiveEnergy.cs b/Source/Meadow.Units/ReactiveEnergy.cs index 4ecc98a..80272a0 100644 --- a/Source/Meadow.Units/ReactiveEnergy.cs +++ b/Source/Meadow.Units/ReactiveEnergy.cs @@ -5,408 +5,410 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Reactive Energy +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct ReactiveEnergy : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Reactive Energy + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct ReactiveEnergy : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The ReactiveEnergy value. + /// Volt Amphere Hours by default. + public ReactiveEnergy(double value, UnitType type = UnitType.VoltAmpereHours) { - /// - /// Creates a new `ReactiveEnergy` object. - /// - /// The ReactiveEnergy value. - /// Volt Amphere Hours by default. - public ReactiveEnergy(double value, UnitType type = UnitType.VoltAmpereHours) - { - Value = ReactiveEnergyConversions.Convert(value, type, UnitType.VoltAmpereHours); - } + Value = ReactiveEnergyConversions.Convert(value, type, UnitType.VoltAmpereHours); + } - /// - /// Creates a new `ReactiveEnergy` object from an existingReactiveEnergy object - /// - /// - public ReactiveEnergy(ReactiveEnergy reactiveEnergy) - { - this.Value = reactiveEnergy.Value; - } + /// + /// Creates a new object from an existingReactiveEnergy object + /// + /// + public ReactiveEnergy(ReactiveEnergy reactiveEnergy) + { + Value = reactiveEnergy.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; + /// + /// The type of units available to describe ReactiveEnergy. + /// + public enum UnitType + { /// - /// The type of units available to describe ReactiveEnergy. + /// Gigavolt Ampere hours /// - public enum UnitType - { - /// - /// Gigavolt Ampere hours - /// - GigavoltAmpereHours, - /// - /// Megavolt Ampere hours - /// - MegavoltAmpereHours, - /// - /// Kilovolt Ampere hours - /// - KilovoltAmpereHours, - /// - /// Volt Ampere hours - /// - VoltAmpereHours, - /// - /// Millivolt Ampere hours - /// - MillivoltAmpereHours, - } - + GigavoltAmpereHours, /// - ///ReactiveEnergy in Millovolt Ampere Hours + /// Megavolt Ampere hours /// - public double MillivoltAmpereHours => From(UnitType.MillivoltAmpereHours); - + MegavoltAmpereHours, /// - ///ReactiveEnergy in Volt Ampere Hours + /// Kilovolt Ampere hours /// - public double VoltAmpereHours => From(UnitType.VoltAmpereHours); - + KilovoltAmpereHours, /// - ///ReactiveEnergy in Kilvolt Ampere Hours + /// Volt Ampere hours /// - public double KilovoltAmpereHours => From(UnitType.KilovoltAmpereHours); - + VoltAmpereHours, /// - ///ReactiveEnergy in Megavolt Ampere Hours + /// Millivolt Ampere hours /// - public double MegavoltAmpereHours => From(UnitType.MegavoltAmpereHours); + MillivoltAmpereHours, + } - /// - ///ReactiveEnergy in Gigavolt Ampere Hours - /// - public double GigavoltAmpereHours => From(UnitType.GigavoltAmpereHours); + /// + ///ReactiveEnergy in Millovolt Ampere Hours + /// + public double MillivoltAmpereHours => From(UnitType.MillivoltAmpereHours); - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] - public double From(UnitType convertTo) - { - return ReactiveEnergyConversions.Convert(Value, UnitType.VoltAmpereHours, convertTo); - } + /// + ///ReactiveEnergy in Volt Ampere Hours + /// + public double VoltAmpereHours => From(UnitType.VoltAmpereHours); - /// - /// Compare to another ReactiveEnergy object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((ReactiveEnergy)obj); - } + /// + ///ReactiveEnergy in Kilvolt Ampere Hours + /// + public double KilovoltAmpereHours => From(UnitType.KilovoltAmpereHours); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator ReactiveEnergy(ushort value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(short value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(uint value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(long value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(int value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(float value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(double value) => new ReactiveEnergy(value); - //[Pure] public static implicit operator ReactiveEnergy(decimal value) => new ReactiveEnergy((double)value); - - // Comparison - /// - /// Compare to another Reactive Energy object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(ReactiveEnergy other) => Value == other.Value; + /// + ///ReactiveEnergy in Megavolt Ampere Hours + /// + public double MegavoltAmpereHours => From(UnitType.MegavoltAmpereHours); - /// - /// Equals operator to compare two ReactiveEnergy objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(ReactiveEnergy left, ReactiveEnergy right) => Equals(left.Value, right.Value); + /// + ///ReactiveEnergy in Gigavolt Ampere Hours + /// + public double GigavoltAmpereHours => From(UnitType.GigavoltAmpereHours); - /// - /// Not equals operator to compare twoReactiveEnergy objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(ReactiveEnergy left, ReactiveEnergy right) => !Equals(left.Value, right.Value); + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] + public double From(UnitType convertTo) + { + return ReactiveEnergyConversions.Convert(Value, UnitType.VoltAmpereHours, convertTo); + } - /// - /// Compare to anotherReactiveEnergy object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(ReactiveEnergy other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + /// + /// Compare to another ReactiveEnergy object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Less than operator to compare twoReactiveEnergy objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator ReactiveEnergy(ushort value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(short value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(uint value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(long value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(int value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(float value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(double value) => new ReactiveEnergy(value); + //[Pure] public static implicit operator ReactiveEnergy(decimal value) => new ReactiveEnergy((double)value); + + // Comparison + /// + /// Compare to another ReactiveEnergy object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(ReactiveEnergy other) => Value == other.Value; - /// - /// Greater than operator to compare twoReactiveEnergy objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + /// + /// Equals operator to compare two ReactiveEnergy objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(ReactiveEnergy left, ReactiveEnergy right) => Equals(left.Value, right.Value); - /// - /// Less than or equal operator to compare twoReactiveEnergy objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + /// + /// Not equals operator to compare two ReactiveEnergy objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(ReactiveEnergy left, ReactiveEnergy right) => !Equals(left.Value, right.Value); - /// - /// Greater than or equal operator to compare twoReactiveEnergy objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + /// + /// Compare to another ReactiveEnergy object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(ReactiveEnergy other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - // Math - /// - /// Addition operator to add twoReactiveEnergy objects - /// - /// left value - /// right value - /// A new ReactiveEnergy object with a value of left + right - [Pure] public static ReactiveEnergy operator +(ReactiveEnergy left, ReactiveEnergy right) => new(left.Value + right.Value); + /// + /// Less than operator to compare two ReactiveEnergy objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - /// - /// Subtraction operator to subtract twoReactiveEnergy objects - /// - /// left value - /// right value - /// A new ReactiveEnergy object with a value of left - right - [Pure] public static ReactiveEnergy operator -(ReactiveEnergy left, ReactiveEnergy right) => new(left.Value - right.Value); + /// + /// Greater than operator to compare two ReactiveEnergy objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - /// - /// Multipication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new ReactiveEnergy object with a value of value multiplied by the operand - [Pure] public static ReactiveEnergy operator *(ReactiveEnergy value, double operand) => new(value.Value * operand); + /// + /// Less than or equal operator to compare two ReactiveEnergy objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new ReactiveEnergy object with a value of value divided by the operand - [Pure] public static ReactiveEnergy operator /(ReactiveEnergy value, double operand) => new(value.Value / operand); + /// + /// Greater than or equal operator to compare two ReactiveEnergy objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(ReactiveEnergy left, ReactiveEnergy right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - /// - /// Returns the absolute length, that is, the length without regards to - /// negative polarity - /// - /// - [Pure] public ReactiveEnergy Abs() { return new ReactiveEnergy(Math.Abs(this.Value)); } + // Math + /// + /// Addition operator to add two ReactiveEnergy objects + /// + /// left value + /// right value + /// A new ReactiveEnergy object with a value of left + right + [Pure] public static ReactiveEnergy operator +(ReactiveEnergy left, ReactiveEnergy right) => new(left.Value + right.Value); - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); + /// + /// Subtraction operator to subtract two ReactiveEnergy objects + /// + /// left value + /// right value + /// A new ReactiveEnergy object with a value of left - right + [Pure] public static ReactiveEnergy operator -(ReactiveEnergy left, ReactiveEnergy right) => new(left.Value - right.Value); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new ReactiveEnergy object with a value of value multiplied by the operand + [Pure] public static ReactiveEnergy operator *(ReactiveEnergy value, double operand) => new(value.Value * operand); - // IComparable - /// - /// Compare to anotherReactiveEnergy object - /// - /// The otherReactiveEnergy cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new ReactiveEnergy object with a value of value divided by the operand + [Pure] public static ReactiveEnergy operator /(ReactiveEnergy value, double operand) => new(value.Value / operand); - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + /// + /// Returns the absolute length, that is, the length without regards to + /// negative polarity + /// + /// + [Pure] public ReactiveEnergy Abs() { return new ReactiveEnergy(Math.Abs(Value)); } - /// - /// Covert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); - /// - /// Covert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - /// - /// Covert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + // IComparable + /// + /// Compare to another ReactiveEnergy object + /// + /// The other ReactiveEnergy cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is ReactiveEnergy reactiveEnergy) + { + return Value.CompareTo(reactiveEnergy.Value); + } - /// - /// Covert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + throw new ArgumentException("Object is not a ReactiveEnergy"); + } - /// - /// Covert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - /// - /// Covert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; + /// + /// Covert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - /// - /// Covert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + /// + /// Covert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - /// - /// Covert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + /// + /// Covert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - /// - /// Covert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + /// + /// Covert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - /// - /// Covert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + /// + /// Covert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - /// - /// Covert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + /// + /// Covert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; - /// - /// Covert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + /// + /// Covert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - /// - /// Covert to type - /// - /// conversion type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + /// + /// Covert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - /// - /// Covert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + /// + /// Covert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - /// - /// Covert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + /// + /// Covert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - /// - /// Covert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + /// + /// Covert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) - { - return (other is null) ? -1 : (Value).CompareTo(other.Value); - } + /// + /// Covert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); + /// + /// Covert to type + /// + /// conversion type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); + /// + /// Covert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + /// + /// Covert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Covert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/ReactivePower.cs b/Source/Meadow.Units/ReactivePower.cs index 19fe46a..5a93940 100644 --- a/Source/Meadow.Units/ReactivePower.cs +++ b/Source/Meadow.Units/ReactivePower.cs @@ -5,408 +5,400 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Reactive Power +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct ReactivePower : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Reactive Power + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct ReactivePower : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The ReactivePower value. + /// Volt Amperes by default. + public ReactivePower(double value, UnitType type = UnitType.VoltAmperes) { - /// - /// Creates a new `ReactivePower` object. - /// - /// The ReactivePower value. - /// Volt Amperes by default. - public ReactivePower(double value, UnitType type = UnitType.VoltAmperes) - { - Value = ReactivePowerConversions.Convert(value, type, UnitType.VoltAmperes); - } + Value = ReactivePowerConversions.Convert(value, type, UnitType.VoltAmperes); + } - /// - /// Creates a new `ReactivePower` object from an existingReactivePower object - /// - /// - public ReactivePower(ReactivePower reactivePower) - { - this.Value = reactivePower.Value; - } + /// + /// Creates a new object from an existingReactivePower object + /// + /// + public ReactivePower(ReactivePower reactivePower) + { + Value = reactivePower.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe ReactivePower. - /// - public enum UnitType - { - /// - /// Gigavolt Ampere - /// - GigavoltAmperes, - /// - /// Megavolt Ampere - /// - MegavoltAmperes, - /// - /// Kilovolt Ampere - /// - KilovoltAmperes, - /// - /// Volt Ampere - /// - VoltAmperes, - /// - /// Millivolt Ampere - /// - MillivoltAmperes, - } + /// + /// The type of units available to describe ReactivePower. + /// + public enum UnitType + { + /// Gigavolt Ampere + GigavoltAmperes, + /// Megavolt Ampere + MegavoltAmperes, + /// Kilovolt Ampere + KilovoltAmperes, + /// Volt Ampere + VoltAmperes, + /// Millivolt Ampere + MillivoltAmperes, + } - /// - ///ReactivePower in Millovolt Amperes - /// - public double MillivoltAmperes => From(UnitType.MillivoltAmperes); - - /// - ///ReactivePower in Volt Amperes - /// - public double VoltAmperes => From(UnitType.VoltAmperes); - - /// - ///ReactivePower in Kilvolt Amperes - /// - public double KilovoltAmperes => From(UnitType.KilovoltAmperes); - - /// - ///ReactivePower in Megavolt Amperes - /// - public double MegavoltAmperes => From(UnitType.MegavoltAmperes); - - /// - ///ReactivePower in Gigavolt Amperes - /// - public double GigavoltAmperes => From(UnitType.GigavoltAmperes); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] - public double From(UnitType convertTo) - { - return ReactivePowerConversions.Convert(Value, UnitType.VoltAmperes, convertTo); - } + /// + ///ReactivePower in Millovolt Amperes + /// + public double MillivoltAmperes => From(UnitType.MillivoltAmperes); - /// - /// Compare to another ReactivePower object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((ReactivePower)obj); - } + /// + ///ReactivePower in Volt Amperes + /// + public double VoltAmperes => From(UnitType.VoltAmperes); + + /// + ///ReactivePower in Kilvolt Amperes + /// + public double KilovoltAmperes => From(UnitType.KilovoltAmperes); + + /// + ///ReactivePower in Megavolt Amperes + /// + public double MegavoltAmperes => From(UnitType.MegavoltAmperes); + + /// + ///ReactivePower in Gigavolt Amperes + /// + public double GigavoltAmperes => From(UnitType.GigavoltAmperes); + + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] + public double From(UnitType convertTo) + { + return ReactivePowerConversions.Convert(Value, UnitType.VoltAmperes, convertTo); + } - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator ReactivePower(ushort value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(short value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(uint value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(long value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(int value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(float value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(double value) => new ReactivePower(value); - //[Pure] public static implicit operator ReactivePower(decimal value) => new ReactivePower((double)value); - - // Comparison - /// - /// Compare to another Reactive Power object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(ReactivePower other) => Value == other.Value; - - /// - /// Equals operator to compare two ReactivePower objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(ReactivePower left, ReactivePower right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare twoReactivePower objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(ReactivePower left, ReactivePower right) => !Equals(left.Value, right.Value); - - /// - /// Compare to anotherReactivePower object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(ReactivePower other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare twoReactivePower objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare twoReactivePower objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare twoReactivePower objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare twoReactivePower objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add twoReactivePower objects - /// - /// left value - /// right value - /// A new ReactivePower object with a value of left + right - [Pure] public static ReactivePower operator +(ReactivePower left, ReactivePower right) => new(left.Value + right.Value); - - /// - /// Subtraction operator to subtract twoReactivePower objects - /// - /// left value - /// right value - /// A new ReactivePower object with a value of left - right - [Pure] public static ReactivePower operator -(ReactivePower left, ReactivePower right) => new(left.Value - right.Value); - - /// - /// Multipication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new ReactivePower object with a value of value multiplied by the operand - [Pure] public static ReactivePower operator *(ReactivePower value, double operand) => new(value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new ReactivePower object with a value of value divided by the operand - [Pure] public static ReactivePower operator /(ReactivePower value, double operand) => new(value.Value / operand); - - /// - /// Returns the absolute length, that is, the length without regards to - /// negative polarity - /// - /// - [Pure] public ReactivePower Abs() { return new ReactivePower(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to anotherReactivePower object - /// - /// The otherReactivePower cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Covert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Covert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Covert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Covert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Covert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Covert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Covert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Covert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Covert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Covert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Covert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Covert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Covert to type - /// - /// conversion type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Covert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Covert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Covert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) + /// + /// Compare to another ReactivePower object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator ReactivePower(ushort value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(short value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(uint value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(long value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(int value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(float value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(double value) => new ReactivePower(value); + //[Pure] public static implicit operator ReactivePower(decimal value) => new ReactivePower((double)value); + + // Comparison + /// + /// Compare to another ReactivePower object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(ReactivePower other) => Value == other.Value; + + /// + /// Equals operator to compare two ReactivePower objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(ReactivePower left, ReactivePower right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two ReactivePower objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(ReactivePower left, ReactivePower right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another ReactivePower object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(ReactivePower other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two ReactivePower objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two ReactivePower objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two ReactivePower objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two ReactivePower objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(ReactivePower left, ReactivePower right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two ReactivePower objects + /// + /// left value + /// right value + /// A new ReactivePower object with a value of left + right + [Pure] public static ReactivePower operator +(ReactivePower left, ReactivePower right) => new(left.Value + right.Value); + + /// + /// Subtraction operator to subtract two ReactivePower objects + /// + /// left value + /// right value + /// A new ReactivePower object with a value of left - right + [Pure] public static ReactivePower operator -(ReactivePower left, ReactivePower right) => new(left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new ReactivePower object with a value of value multiplied by the operand + [Pure] public static ReactivePower operator *(ReactivePower value, double operand) => new(value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new ReactivePower object with a value of value divided by the operand + [Pure] public static ReactivePower operator /(ReactivePower value, double operand) => new(value.Value / operand); + + /// + /// Returns the absolute length, that is, the length without regards to + /// negative polarity + /// + /// + [Pure] public ReactivePower Abs() { return new ReactivePower(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another ReactivePower object + /// + /// The otherReactivePower cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is ReactivePower reactivePower) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(reactivePower.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a ReactivePower"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Covert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Covert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Covert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Covert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Covert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Covert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Covert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Covert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Covert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Covert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Covert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Covert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Covert to type + /// + /// conversion type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Covert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Covert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Covert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/RelativeHumidity.cs b/Source/Meadow.Units/RelativeHumidity.cs index 72d2645..6ee58bc 100644 --- a/Source/Meadow.Units/RelativeHumidity.cs +++ b/Source/Meadow.Units/RelativeHumidity.cs @@ -4,354 +4,357 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents relative humidity expressed as a percentage, indicates a +/// present state of absolute humidity relative to a maximum humidity given +/// the same temperature. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct RelativeHumidity : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents relative humidity expressed as a percentage, indicates a - /// present state of absolute humidity relative to a maximum humidity given - /// the same temperature. + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct RelativeHumidity : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The relative humidity value. + /// Relative humidity unit. + public RelativeHumidity(double value, UnitType type = UnitType.Percent) { - /// - /// Creates a new `RelativeHumidity` object. - /// - /// The relative humidity value. - /// Relative humidity unit. - public RelativeHumidity(double value, UnitType type = UnitType.Percent) - { - Percent = value; - } + Percent = value; + } - /// - /// Creates a new `RelativeHumidity` object from an existing RelativeHumidity object - /// - /// - public RelativeHumidity(RelativeHumidity relativeHumidity) - { - Percent = relativeHumidity.Percent; - } + /// + /// Creates a new object from an existing RelativeHumidity object + /// + /// + public RelativeHumidity(RelativeHumidity relativeHumidity) + { + Percent = relativeHumidity.Percent; + } + /// + /// The relative expressed as a value percent. + /// + public double Percent { get; private set; } + /// + /// The type of units available to describe the RelativeHumidity. + /// + public enum UnitType + { /// - /// The relative expressed as a value percent. - /// - public double Percent { get; private set; } - /// - /// The type of units available to describe the RelativeHumidity. + /// Relative humidity as a percentage /// - public enum UnitType - { - /// - /// Relative humidity as a percentage - /// - Percent - } + Percent + } - /// - /// Compare to another RelativeHumidity object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((RelativeHumidity)obj); - } + /// + /// Compare to another RelativeHumidity object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Percent.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator RelativeHumidity(ushort value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(short value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(uint value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(long value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(int value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(float value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(double value) => new RelativeHumidity(value); - //[Pure] public static implicit operator RelativeHumidity(decimal value) => new RelativeHumidity((double)value); - - // Comparison - /// - /// Compare to another RelativeHumidity object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(RelativeHumidity other) => Percent == other.Percent; + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Percent.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator RelativeHumidity(ushort value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(short value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(uint value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(long value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(int value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(float value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(double value) => new RelativeHumidity(value); + //[Pure] public static implicit operator RelativeHumidity(decimal value) => new RelativeHumidity((double)value); + + // Comparison + /// + /// Compare to another RelativeHumidity object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(RelativeHumidity other) => Percent == other.Percent; - /// - /// Equals operator to compare two RelativeHumidity objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(RelativeHumidity left, RelativeHumidity right) => Equals(left.Percent, right.Percent); + /// + /// Equals operator to compare two RelativeHumidity objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(RelativeHumidity left, RelativeHumidity right) => Equals(left.Percent, right.Percent); - /// - /// Not equals operator to compare two RelativeHumidity objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(RelativeHumidity left, RelativeHumidity right) => !Equals(left.Percent, right.Percent); + /// + /// Not equals operator to compare two RelativeHumidity objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(RelativeHumidity left, RelativeHumidity right) => !Equals(left.Percent, right.Percent); - /// - /// Compare to another RelativeHumidity object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(RelativeHumidity other) => Equals(Percent, other.Percent) ? 0 : Percent.CompareTo(other.Percent); + /// + /// Compare to another RelativeHumidity object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(RelativeHumidity other) => Equals(Percent, other.Percent) ? 0 : Percent.CompareTo(other.Percent); - /// - /// Less than operator to compare two RelativeHumidity objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) < 0; + /// + /// Less than operator to compare two RelativeHumidity objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) < 0; - /// - /// Greater than operator to compare two RelativeHumidity objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) > 0; + /// + /// Greater than operator to compare two RelativeHumidity objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) > 0; - /// - /// Less than or equal operator to compare two RelativeHumidity objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) <= 0; + /// + /// Less than or equal operator to compare two RelativeHumidity objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) <= 0; - /// - /// Greater than or equal operator to compare two RelativeHumidity objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) >= 0; + /// + /// Greater than or equal operator to compare two RelativeHumidity objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(RelativeHumidity left, RelativeHumidity right) => Comparer.Default.Compare(left.Percent, right.Percent) >= 0; - // Math - /// - /// Addition operator to add two RelativeHumidity objects - /// - /// left value - /// right value - /// A new RelativeHumidity object with a value of left + right - [Pure] public static RelativeHumidity operator +(RelativeHumidity left, RelativeHumidity right) => new (left.Percent + right.Percent); + // Math + /// + /// Addition operator to add two RelativeHumidity objects + /// + /// left value + /// right value + /// A new RelativeHumidity object with a value of left + right + [Pure] public static RelativeHumidity operator +(RelativeHumidity left, RelativeHumidity right) => new (left.Percent + right.Percent); - /// - /// Subtraction operator to subtract two RelativeHumidity objects - /// - /// left value - /// right value - /// A new RelativeHumidity object with a value of left - right - [Pure] public static RelativeHumidity operator -(RelativeHumidity left, RelativeHumidity right) => new (left.Percent - right.Percent); + /// + /// Subtraction operator to subtract two RelativeHumidity objects + /// + /// left value + /// right value + /// A new RelativeHumidity object with a value of left - right + [Pure] public static RelativeHumidity operator -(RelativeHumidity left, RelativeHumidity right) => new (left.Percent - right.Percent); - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new RelativeHumidity object with a value of value multiplied by the operand - [Pure] public static RelativeHumidity operator *(RelativeHumidity value, double operand) => new (value.Percent * operand); + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new RelativeHumidity object with a value of value multiplied by the operand + [Pure] public static RelativeHumidity operator *(RelativeHumidity value, double operand) => new (value.Percent * operand); - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new RelativeHumidity object with a value of value divided by the operand - [Pure] public static RelativeHumidity operator /(RelativeHumidity value, double operand) => new (value.Percent / operand); + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new RelativeHumidity object with a value of value divided by the operand + [Pure] public static RelativeHumidity operator /(RelativeHumidity value, double operand) => new (value.Percent / operand); - /// - /// Returns the absolute value of the - /// - /// - [Pure] public RelativeHumidity Abs() { return new RelativeHumidity(Math.Abs(this.Percent)); } + /// + /// Returns the absolute value of the + /// + /// + [Pure] public RelativeHumidity Abs() { return new RelativeHumidity(Math.Abs(Percent)); } - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Percent.ToString(); + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Percent.ToString(); - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Percent.ToString(format, formatProvider); + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Percent.ToString(format, formatProvider); - // IComparable - /// - /// Compare to another RelativeHumidity object - /// - /// The other RelativeHumidity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Percent.CompareTo(obj); + // IComparable + /// + /// Compare to another RelativeHumidity object + /// + /// The other RelativeHumidity cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is RelativeHumidity relativeHumidity) + { + return Percent.CompareTo(relativeHumidity.Percent); + } - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Percent.GetTypeCode(); + throw new ArgumentException("Object is not a RelativeHumidity"); + } - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Percent).ToBoolean(provider); + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Percent.GetTypeCode(); - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Percent).ToByte(provider); + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Percent).ToBoolean(provider); - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Percent).ToChar(provider); + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Percent).ToByte(provider); - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Percent).ToDateTime(provider); + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Percent).ToChar(provider); - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Percent).ToDecimal(provider); + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Percent).ToDateTime(provider); - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Percent; + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Percent).ToDecimal(provider); - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Percent).ToInt16(provider); + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Percent; - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Percent).ToInt32(provider); + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Percent).ToInt16(provider); - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Percent).ToInt64(provider); + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Percent).ToInt32(provider); - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Percent).ToSByte(provider); + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Percent).ToInt64(provider); - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Percent).ToSingle(provider); + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Percent).ToSByte(provider); - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Percent.ToString(provider); + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Percent).ToSingle(provider); - /// - /// Convert to type - /// - /// unit to convert - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Percent).ToType(conversionType, provider); + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Percent.ToString(provider); - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Percent).ToUInt16(provider); + /// + /// Convert to type + /// + /// unit to convert + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Percent).ToType(conversionType, provider); - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Percent).ToUInt32(provider); + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Percent).ToUInt16(provider); - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Percent).ToUInt64(provider); + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Percent).ToUInt32(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) - { - return (other is null) ? -1 : (Percent).CompareTo(other.Value); - } + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Percent).ToUInt64(provider); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Percent.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Percent).CompareTo(other.Value); + } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Percent.Equals(other); + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Percent.Equals(other); - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Percent.CompareTo(other); - } + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Percent.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Percent.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Resistance.cs b/Source/Meadow.Units/Resistance.cs index 5d06f60..e8d213c 100644 --- a/Source/Meadow.Units/Resistance.cs +++ b/Source/Meadow.Units/Resistance.cs @@ -30,49 +30,41 @@ static Resistance() public static Resistance Zero => _zero; /// - /// Creates a new `Resistance` object. + /// Creates a new object. /// /// The Resistance value. /// Ohms by default. public Resistance(double value, UnitType type = UnitType.Ohms) { - _value = ResistanceConversions.Convert(value, type, UnitType.Ohms); + Value = ResistanceConversions.Convert(value, type, UnitType.Ohms); } /// - /// Creates a new `Resistance` object from an existing Resistance object + /// Creates a new object from an existing Resistance object /// /// public Resistance(Resistance resistance) { - _value = resistance._value; + Value = resistance.Value; } /// /// Internal canonical value. /// - private readonly double _value; + private readonly double Value; /// /// The type of units available to describe the Resistance. /// public enum UnitType { - /// - /// MilliOhms - /// + /// MilliOhms Milliohms, - /// - /// Ohms - /// + /// Ohms Ohms, - /// - /// Kiloohms - /// + /// Kiloohms Kiloohms, - /// - /// Megaohms - /// + /// Megaohms Megaohms } @@ -80,14 +72,17 @@ public enum UnitType /// Get resistance in MilliOhms /// public double Milliohms => From(UnitType.Milliohms); + /// /// Get resistance in Ohms /// public double Ohms => From(UnitType.Ohms); + /// /// Get resistance in kiloOhms /// public double Kiloohms => From(UnitType.Kiloohms); + /// /// Get resistance in megaOhms /// @@ -101,7 +96,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return ResistanceConversions.Convert(_value, Resistance.UnitType.Ohms, convertTo); + return ResistanceConversions.Convert(Value, UnitType.Ohms, convertTo); } /// @@ -109,19 +104,13 @@ public double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Resistance)obj); - } + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object /// /// int32 hash value - [Pure] public override int GetHashCode() => _value.GetHashCode(); + [Pure] public override int GetHashCode() => Value.GetHashCode(); // Comparison /// @@ -129,7 +118,7 @@ public override bool Equals(object obj) /// /// The object to compare /// true if equal - [Pure] public bool Equals(Resistance other) => _value == other._value; + [Pure] public bool Equals(Resistance other) => Value == other.Value; /// /// Equals operator to compare two Resistance objects @@ -137,7 +126,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if equal - [Pure] public static bool operator ==(Resistance left, Resistance right) => Equals(left._value, right._value); + [Pure] public static bool operator ==(Resistance left, Resistance right) => Equals(left.Value, right.Value); /// /// Not equals operator to compare two Resistance objects @@ -145,14 +134,14 @@ public override bool Equals(object obj) /// left value /// right value /// true if not equal - [Pure] public static bool operator !=(Resistance left, Resistance right) => !Equals(left._value, right._value); + [Pure] public static bool operator !=(Resistance left, Resistance right) => !Equals(left.Value, right.Value); /// /// Compare to another Resistance object /// /// /// 0 if equal - [Pure] public int CompareTo(Resistance other) => Equals(_value, other._value) ? 0 : _value.CompareTo(other._value); + [Pure] public int CompareTo(Resistance other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); /// /// Less than operator to compare two Resistance objects @@ -160,7 +149,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than right - [Pure] public static bool operator <(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) < 0; + [Pure] public static bool operator <(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) < 0; /// /// Greater than operator to compare two Resistance objects @@ -168,7 +157,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than right - [Pure] public static bool operator >(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) > 0; + [Pure] public static bool operator >(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) > 0; /// /// Less than or equal operator to compare two Resistance objects @@ -176,7 +165,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is less than or equal to right - [Pure] public static bool operator <=(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) <= 0; + [Pure] public static bool operator <=(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; /// /// Greater than or equal operator to compare two Resistance objects @@ -184,7 +173,7 @@ public override bool Equals(object obj) /// left value /// right value /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Resistance left, Resistance right) => Comparer.Default.Compare(left._value, right._value) >= 0; + [Pure] public static bool operator >=(Resistance left, Resistance right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; // Math /// @@ -193,7 +182,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Resistance object with a value of left + right - [Pure] public static Resistance operator +(Resistance left, Resistance right) => new(left._value + right._value); + [Pure] public static Resistance operator +(Resistance left, Resistance right) => new(left.Value + right.Value); /// /// Subtraction operator to subtract two Resistance objects @@ -201,7 +190,7 @@ public override bool Equals(object obj) /// left value /// right value /// A new Resistance object with a value of left - right - [Pure] public static Resistance operator -(Resistance left, Resistance right) => new(left._value - right._value); + [Pure] public static Resistance operator -(Resistance left, Resistance right) => new(left.Value - right.Value); /// /// Multiplication operator to multiply by a double @@ -209,7 +198,7 @@ public override bool Equals(object obj) /// object to multiply /// operand to multiply object /// A new Resistance object with a value of value multiplied by the operand - [Pure] public static Resistance operator *(Resistance value, double operand) => new(value._value * operand); + [Pure] public static Resistance operator *(Resistance value, double operand) => new(value.Value * operand); /// /// Division operator to divide by a double @@ -217,13 +206,13 @@ public override bool Equals(object obj) /// object to be divided /// operand to divide object /// A new Resistance object with a value of value divided by the operand - [Pure] public static Resistance operator /(Resistance value, double operand) => new(value._value / operand); + [Pure] public static Resistance operator /(Resistance value, double operand) => new(value.Value / operand); /// /// Get a string representation of the object /// /// A string representing the object - [Pure] public override string ToString() => _value.ToString(); + [Pure] public override string ToString() => Value.ToString(); /// /// Get a string representation of the object @@ -231,7 +220,7 @@ public override bool Equals(object obj) /// format /// format provider /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => _value.ToString(format, formatProvider); + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); // IComparable /// @@ -239,97 +228,106 @@ public override bool Equals(object obj) /// /// The other Resistance cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => _value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is Resistance resistance) + { + return Value.CompareTo(resistance.Value); + } + + throw new ArgumentException("Object is not a Resistance"); + } /// /// Get type code of object /// /// The TypeCode - [Pure] public TypeCode GetTypeCode() => _value.GetTypeCode(); + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); /// /// Convert to boolean /// /// format provider /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider); + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); /// /// Convert to byte /// /// format provider /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); /// /// Convert to char /// /// format provider /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider); + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); /// /// Convert to DateTime /// /// format provider /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider); + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); /// /// Convert to Decimal /// /// format provider /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); /// /// Convert to double /// /// format provider /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => _value; + [Pure] public double ToDouble(IFormatProvider provider) => Value; /// /// Convert to in16 /// /// format provider /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); /// /// Convert to int32 /// /// format provider /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); /// /// Convert to int64 /// /// format provider /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); /// /// Convert to sbyte /// /// format provider /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); /// /// Convert to float /// /// format provider /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); /// /// Convert to string /// /// format provider /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => _value.ToString(provider); + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); /// /// Convert to type @@ -337,28 +335,28 @@ public override bool Equals(object obj) /// type to convert to /// format provider /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); /// /// Convert to uint16 /// /// format provider /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); /// /// Convert to uint32 /// /// format provider /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); /// /// Convert to uint64 /// /// format provider /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); /// /// Compare the default value to a double @@ -368,7 +366,7 @@ public override bool Equals(object obj) [Pure] public int CompareTo(double? other) { - return (other is null) ? -1 : (_value).CompareTo(other.Value); + return (other is null) ? -1 : (Value).CompareTo(other.Value); } /// @@ -376,19 +374,19 @@ public int CompareTo(double? other) /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double? other) => _value.Equals(other); + [Pure] public bool Equals(double? other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public bool Equals(double other) => _value.Equals(other); + [Pure] public bool Equals(double other) => Value.Equals(other); /// /// Compare the default value to a double /// /// value to compare /// 0 if equal - [Pure] public int CompareTo(double other) => _value.CompareTo(other); + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Speed.cs b/Source/Meadow.Units/Speed.cs index 1acecdf..75be08e 100644 --- a/Source/Meadow.Units/Speed.cs +++ b/Source/Meadow.Units/Speed.cs @@ -5,464 +5,441 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Speed +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Speed : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Speed + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Speed : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Speed value. + /// kilometers meters per second by default. + public Speed(double value, UnitType type = UnitType.KilometersPerSecond) { - /// - /// Creates a new `Speed` object. - /// - /// The Speed value. - /// kilometers meters per second by default. - public Speed(double value, UnitType type = UnitType.KilometersPerSecond) - { - Value = SpeedConversions.Convert(value, type, UnitType.KilometersPerSecond); - } + Value = SpeedConversions.Convert(value, type, UnitType.KilometersPerSecond); + } - /// - /// Creates a new `Speed` object from an existing Speed object - /// - /// - public Speed(Speed speed) - { - Value = speed.Value; - } + /// + /// Creates a new object from an existing Speed object + /// + /// + public Speed(Speed speed) + { + Value = speed.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Speed. - /// - public enum UnitType - { - /// - /// Feet per minute - /// - FeetPerMinute, - /// - /// Feet per second - /// - FeetPerSecond, - /// - /// Kilometers per hour - /// - KilometersPerHour, - /// - /// Kilometers per minute - /// - KilometersPerMinute, - /// - /// Kilometers per second - /// - KilometersPerSecond, - /// - /// Knots - /// - Knots, - /// - /// Meters per minute - /// - MetersPerMinute, - /// - /// Meters per second - /// - MetersPerSecond, - /// - /// Miles per hour - /// - MilesPerHour, - /// - /// Miles per minute - /// - MilesPerMinute, - /// - /// Miles per second - /// - MilesPerSecond, - /// - /// Speed of light - /// - SpeedOfLight, - /// - /// Mach - /// - Mach, - } + /// + /// The type of units available to describe the Speed. + /// + public enum UnitType + { + /// Feet per minute + FeetPerMinute, + /// Feet per second + FeetPerSecond, + /// Kilometers per hour + KilometersPerHour, + /// Kilometers per minute + KilometersPerMinute, + /// Kilometers per second + KilometersPerSecond, + /// Knots + Knots, + /// Meters per minute + MetersPerMinute, + /// Meters per second + MetersPerSecond, + /// Miles per hour + MilesPerHour, + /// Miles per minute + MilesPerMinute, + /// Miles per second + MilesPerSecond, + /// Speed of light + SpeedOfLight, + /// Mach + Mach, + } - /// - /// Get speed in feet per second - /// - public double FeetPerSecond => From(UnitType.FeetPerSecond); - /// - /// Get speed in feet per minute - /// - public double FeetPerMinute => From(UnitType.FeetPerMinute); - /// - /// Get speed in kilometers per hour - /// - public double KilometersPerHour => From(UnitType.KilometersPerHour); - /// - /// Get speed in kilometers per minute - /// - public double KilometersPerMinute => From(UnitType.KilometersPerMinute); - /// - /// Get speed in kilometers per second - /// - public double KilometersPerSecond => From(UnitType.KilometersPerSecond); - /// - /// Get speed in knots - /// - public double Knots => From(UnitType.Knots); - /// - /// Get speed in meters per minute - /// - public double MetersPerMinute => From(UnitType.MetersPerMinute); - /// - /// Get speed in meters per second - /// - public double MetersPerSecond => From(UnitType.MetersPerSecond); - /// - /// Get speed in miles per hour - /// - public double MilesPerHour => From(UnitType.MilesPerHour); - /// - /// Get speed in miles per minute - /// - public double MilesPerMinute => From(UnitType.MilesPerMinute); - /// - /// Get speed in miles per second - /// - public double MilesPerSecond => From(UnitType.MilesPerSecond); - /// - /// Get speed as a multiple of the speed of light - 299792458m/s - /// - public double SpeedOfLight => From(UnitType.SpeedOfLight); - /// - /// Get speed as a multiple of mach - /// - public double Mach => From(UnitType.Mach); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return SpeedConversions.Convert(Value, UnitType.KilometersPerSecond, convertTo); - } + /// + /// Get speed in feet per second + /// + public double FeetPerSecond => From(UnitType.FeetPerSecond); + /// + /// Get speed in feet per minute + /// + public double FeetPerMinute => From(UnitType.FeetPerMinute); + /// + /// Get speed in kilometers per hour + /// + public double KilometersPerHour => From(UnitType.KilometersPerHour); + /// + /// Get speed in kilometers per minute + /// + public double KilometersPerMinute => From(UnitType.KilometersPerMinute); + /// + /// Get speed in kilometers per second + /// + public double KilometersPerSecond => From(UnitType.KilometersPerSecond); + /// + /// Get speed in knots + /// + public double Knots => From(UnitType.Knots); + /// + /// Get speed in meters per minute + /// + public double MetersPerMinute => From(UnitType.MetersPerMinute); + /// + /// Get speed in meters per second + /// + public double MetersPerSecond => From(UnitType.MetersPerSecond); + /// + /// Get speed in miles per hour + /// + public double MilesPerHour => From(UnitType.MilesPerHour); + /// + /// Get speed in miles per minute + /// + public double MilesPerMinute => From(UnitType.MilesPerMinute); + /// + /// Get speed in miles per second + /// + public double MilesPerSecond => From(UnitType.MilesPerSecond); + /// + /// Get speed as a multiple of the speed of light - 299792458m/s + /// + public double SpeedOfLight => From(UnitType.SpeedOfLight); + /// + /// Get speed as a multiple of mach + /// + public double Mach => From(UnitType.Mach); - /// - /// Compare to another Speed object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Speed)obj); - } + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return SpeedConversions.Convert(Value, UnitType.KilometersPerSecond, convertTo); + } + + /// + /// Compare to another Speed object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Speed(ushort value) => new Speed(value); + //[Pure] public static implicit operator Speed(short value) => new Speed(value); + //[Pure] public static implicit operator Speed(uint value) => new Speed(value); + //[Pure] public static implicit operator Speed(long value) => new Speed(value); + //[Pure] public static implicit operator Speed(int value) => new Speed(value); + //[Pure] public static implicit operator Speed(float value) => new Speed(value); + //[Pure] public static implicit operator Speed(double value) => new Speed(value); + //[Pure] public static implicit operator Speed(decimal value) => new Speed((double)value); + + // Comparison + /// + /// Compare to another Speed object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Speed other) => Value == other.Value; + + /// + /// Equals operator to compare two Speed objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Speed left, Speed right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Speed objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Speed left, Speed right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Speed object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Speed other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Speed(ushort value) => new Speed(value); - //[Pure] public static implicit operator Speed(short value) => new Speed(value); - //[Pure] public static implicit operator Speed(uint value) => new Speed(value); - //[Pure] public static implicit operator Speed(long value) => new Speed(value); - //[Pure] public static implicit operator Speed(int value) => new Speed(value); - //[Pure] public static implicit operator Speed(float value) => new Speed(value); - //[Pure] public static implicit operator Speed(double value) => new Speed(value); - //[Pure] public static implicit operator Speed(decimal value) => new Speed((double)value); - - // Comparison - /// - /// Compare to another Speed object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Speed other) => Value == other.Value; - - /// - /// Equals operator to compare two Speed objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Speed left, Speed right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Speed objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Speed left, Speed right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Speed object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Speed other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Speed objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Speed objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Speed objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Speed objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Speed objects - /// - /// left value - /// right value - /// A new Speed object with a value of left + right - [Pure] public static Speed operator +(Speed left, Speed right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Speed objects - /// - /// left value - /// right value - /// A new Speed object with a value of left - right - [Pure] public static Speed operator -(Speed left, Speed right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Speed object with a value of value multiplied by the operand - [Pure] public static Speed operator *(Speed value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Speed object with a value of value divided by the operand - [Pure] public static Speed operator /(Speed value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Speed Abs() { return new Speed(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Speed object - /// - /// The other Speed cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// conversion unit type - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Less than operator to compare two Speed objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Speed objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Speed objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Speed objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Speed left, Speed right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Speed objects + /// + /// left value + /// right value + /// A new Speed object with a value of left + right + [Pure] public static Speed operator +(Speed left, Speed right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Speed objects + /// + /// left value + /// right value + /// A new Speed object with a value of left - right + [Pure] public static Speed operator -(Speed left, Speed right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Speed object with a value of value multiplied by the operand + [Pure] public static Speed operator *(Speed value, double operand) => new (value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Speed object with a value of value divided by the operand + [Pure] public static Speed operator /(Speed value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Speed Abs() { return new Speed(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Speed object + /// + /// The other Speed cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Speed speed) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(speed.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Speed"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// conversion unit type + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Temperature.cs b/Source/Meadow.Units/Temperature.cs index 3cafd2f..8a3e70e 100644 --- a/Source/Meadow.Units/Temperature.cs +++ b/Source/Meadow.Units/Temperature.cs @@ -5,417 +5,413 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents temperature; the physical quantity that expresses hot and cold. +/// It is the manifestation of thermal energy, present in all matter, which +/// is the source of the occurrence of heat, a flow of energy, when a body +/// is in contact with another that is colder or hotter. +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Temperature : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents temperature; the physical quantity that expresses hot and cold. - /// It is the manifestation of thermal energy, present in all matter, which - /// is the source of the occurrence of heat, a flow of energy, when a body - /// is in contact with another that is colder or hotter. - /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Temperature : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// Absolute Zero temperature. + /// + public static Temperature AbsoluteZero = new (0, UnitType.Kelvin); + + /// + /// Creates a new object. + /// + /// The temperature value. + /// _Celsius_ (`°C`), by default. + public Temperature(double value, UnitType type = UnitType.Celsius) { - /// - /// Absolute Zero temperature. - /// - public static Temperature AbsoluteZero = new (0, UnitType.Kelvin); - - /// - /// Creates a new `Temperature` object. - /// - /// The temperature value. - /// _Celsius_ (`°C`), by default. - public Temperature(double value, UnitType type = UnitType.Celsius) + Value = 0; + switch (type) { - Value = 0; - switch (type) - { - case UnitType.Celsius: - Value = value; - break; - case UnitType.Fahrenheit: - Value = TempConversions.FToC(value); - break; - case UnitType.Kelvin: - Value = TempConversions.KToC(value); - break; - } - - if (this.Kelvin < 0) - { - throw new ArgumentOutOfRangeException("Temperature cannot be less than 0 Kelvin"); - } + case UnitType.Celsius: + Value = value; + break; + case UnitType.Fahrenheit: + Value = TempConversions.FToC(value); + break; + case UnitType.Kelvin: + Value = TempConversions.KToC(value); + break; } - /// - /// Creates a new `Temperature` object from an existing Temperature object. - /// - /// - public Temperature(Temperature temperature) + if (Kelvin < 0) { - Value = temperature.Value; + throw new ArgumentOutOfRangeException(nameof(value), "Temperature cannot be less than 0 Kelvin"); } + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Creates a new object from an existing Temperature object. + /// + /// + public Temperature(Temperature temperature) + { + Value = temperature.Value; + } - /// - /// The type of units available to describe the Temperature. - /// - public enum UnitType - { - /// - /// Celsius - /// - Celsius, - /// - /// Fahrenheit - /// - Fahrenheit, - /// - /// Kelvin - /// - Kelvin, - } + /// + /// Internal canonical value. + /// + private readonly double Value; - //======================== - // TO property conversions - - /// - /// Gets the temperature value expressed as a unit _Celsius/Centrigrade_ (`C°`). - /// - public double Celsius { get => Value; } - - /// - /// Gets the temperature value expressed as a unit _Fahrenheit_ (`F°`). - /// - public double Fahrenheit { get => TempConversions.CToF(Value); } - - /// - /// Gets the temperature value expressed as a unit _Kelvin_ (`K`). - /// - public double Kelvin { get => TempConversions.CToK(Value); } - - /// - /// Get temperature value for a given unit - /// - /// unit to convert to - /// - [Pure] - public double From(UnitType convertTo) - { - return TempConversions.Convert(Value, UnitType.Celsius, convertTo); - } + /// + /// The type of units available to describe the Temperature. + /// + public enum UnitType + { + /// Celsius + Celsius, + /// Fahrenheit + Fahrenheit, + /// Kelvin + Kelvin, + } - /// - /// Compare to another Temperature object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Temperature)obj); - } + //======================== + // TO property conversions + + /// + /// Gets the temperature value expressed as a unit _Celsius/Centrigrade_ (`°C`). + /// + public double Celsius => Value; + + /// + /// Gets the temperature value expressed as a unit _Fahrenheit_ (`°F`). + /// + public double Fahrenheit => TempConversions.CToF(Value); + + /// + /// Gets the temperature value expressed as a unit _Kelvin_ (`K`). + /// + public double Kelvin => TempConversions.CToK(Value); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Temperature(ushort value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(short value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(uint value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(long value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(int value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(float value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(double value) => new Temperature(value); - //[Pure] public static implicit operator Temperature(decimal value) => new Temperature((double)value); - - // Comparison - /// - /// Compare to another Temperature object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Temperature other) => Value == other.Value; - - /// - /// Equals operator to compare two Temperature objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Temperature left, Temperature right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Temperature objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Temperature left, Temperature right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Temperature object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Temperature other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Temperature objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Temperature objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Temperature objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Temperature objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Temperature objects - /// - /// left value - /// right value - /// A new Temperature object with a value of left + right - [Pure] public static Temperature operator +(Temperature left, Temperature right) => new(left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Temperature objects - /// - /// left value - /// right value - /// A new Temperature object with a value of left - right - [Pure] public static Temperature operator -(Temperature left, Temperature right) => new(left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Temperature object with a value of value multiplied by the operand - [Pure] public static Temperature operator *(Temperature value, double operand) => new(value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Temperature object with a value of value divided by the operand - [Pure] public static Temperature operator /(Temperature value, double operand) => new(value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Temperature Abs() { return new Temperature(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Temperature object - /// - /// The other Temperature cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) + /// + /// Get temperature value for a given unit + /// + /// unit to convert to + /// + [Pure] + public double From(UnitType convertTo) + { + return TempConversions.Convert(Value, UnitType.Celsius, convertTo); + } + + /// + /// Compare to another Temperature object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Temperature(ushort value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(short value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(uint value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(long value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(int value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(float value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(double value) => new Temperature(value); + //[Pure] public static implicit operator Temperature(decimal value) => new Temperature((double)value); + + // Comparison + /// + /// Compare to another Temperature object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Temperature other) => Value == other.Value; + + /// + /// Equals operator to compare two Temperature objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Temperature left, Temperature right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Temperature objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Temperature left, Temperature right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Temperature object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Temperature other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Temperature objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Temperature objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Temperature objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Temperature objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Temperature left, Temperature right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Temperature objects + /// + /// left value + /// right value + /// A new Temperature object with a value of left + right + [Pure] public static Temperature operator +(Temperature left, Temperature right) => new(left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Temperature objects + /// + /// left value + /// right value + /// A new Temperature object with a value of left - right + [Pure] public static Temperature operator -(Temperature left, Temperature right) => new(left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Temperature object with a value of value multiplied by the operand + [Pure] public static Temperature operator *(Temperature value, double operand) => new(value.Value * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Temperature object with a value of value divided by the operand + [Pure] public static Temperature operator /(Temperature value, double operand) => new(value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Temperature Abs() { return new Temperature(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Temperature object + /// + /// The other Temperature cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Temperature temperature) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(temperature.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Temperature"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Torque.cs b/Source/Meadow.Units/Torque.cs index 1366a3c..51f4ba8 100644 --- a/Source/Meadow.Units/Torque.cs +++ b/Source/Meadow.Units/Torque.cs @@ -5,424 +5,418 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Torque +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Torque : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Torque + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Torque : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Torque value. + /// kilometers meters per second by default. + public Torque(double value, UnitType type = UnitType.NewtonMeter) { - /// - /// Creates a new `Torque` object. - /// - /// The Torque value. - /// kilometers meters per second by default. - public Torque(double value, UnitType type = UnitType.NewtonMeter) - { - Value = TorqueConversions.Convert(value, type, UnitType.NewtonMeter); - } + Value = TorqueConversions.Convert(value, type, UnitType.NewtonMeter); + } - /// - /// Creates a new `Torque` object from an existing Torque object - /// - /// - public Torque(Torque torque) - { - Value = torque.Value; - } + /// + /// Creates a new object from an existing Torque object + /// + /// + public Torque(Torque torque) + { + Value = torque.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Torque. - /// - public enum UnitType - { - /// - /// Newton meters - /// - NewtonMeter, - /// - /// Foot pounds - /// - FootPound, - /// - /// Kilogram meters - /// - KilogramMeter, - /// - /// Kilogram centimeters - /// - KilogramCentimeter, - /// - /// Gram centimeters - /// - GramCentimeter, - /// - /// Inch pounds - /// - InchPound, - /// - /// Inch ounces - /// - InchOunce, - /// - /// Dyne centimeters - /// - DyneCentimeter - } + /// + /// The type of units available to describe the Torque. + /// + public enum UnitType + { + /// Newton meters + NewtonMeter, + /// Foot pounds + FootPound, + /// Kilogram meters + KilogramMeter, + /// Kilogram centimeters + KilogramCentimeter, + /// Gram centimeters + GramCentimeter, + /// Inch pounds + InchPound, + /// Inch ounces + InchOunce, + /// Dyne centimeters + DyneCentimeter + } - /// - /// Get torque in newton meters - /// - public double NewtonMeter => From(UnitType.NewtonMeter); - /// - /// Get torque in foot pounds - /// - public double FootPound => From(UnitType.FootPound); - /// - /// Get torque in kilogram meters - /// - public double KilogramMeter => From(UnitType.KilogramMeter); - /// - /// Get torque in kilogram centimeters - /// - public double KilogramCentimeter => From(UnitType.KilogramCentimeter); - /// - /// Get torque in gram centimeters - /// - public double GramCentimeter => From(UnitType.GramCentimeter); - /// - /// Get torque in inch pounds - /// - public double InchPound => From(UnitType.InchPound); - /// - /// Get torque in inch ounces - /// - public double InchOunce => From(UnitType.InchOunce); - /// - /// Get torque in dyne centimeters - /// - public double DyneCentimeter => From(UnitType.DyneCentimeter); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return TorqueConversions.Convert(Value, UnitType.NewtonMeter, convertTo); - } + /// + /// Get torque in newton meters + /// + public double NewtonMeter => From(UnitType.NewtonMeter); - /// - /// Compare to another Torque object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Torque)obj); - } + /// + /// Get torque in foot pounds + /// + public double FootPound => From(UnitType.FootPound); + + /// + /// Get torque in kilogram meters + /// + public double KilogramMeter => From(UnitType.KilogramMeter); + + /// + /// Get torque in kilogram centimeters + /// + public double KilogramCentimeter => From(UnitType.KilogramCentimeter); + + /// + /// Get torque in gram centimeters + /// + public double GramCentimeter => From(UnitType.GramCentimeter); + + /// + /// Get torque in inch pounds + /// + public double InchPound => From(UnitType.InchPound); + + /// + /// Get torque in inch ounces + /// + public double InchOunce => From(UnitType.InchOunce); + + /// + /// Get torque in dyne centimeters + /// + public double DyneCentimeter => From(UnitType.DyneCentimeter); + + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return TorqueConversions.Convert(Value, UnitType.NewtonMeter, convertTo); + } + + /// + /// Compare to another Torque object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Torque(ushort value) => new Torque(value); + //[Pure] public static implicit operator Torque(short value) => new Torque(value); + //[Pure] public static implicit operator Torque(uint value) => new Torque(value); + //[Pure] public static implicit operator Torque(long value) => new Torque(value); + //[Pure] public static implicit operator Torque(int value) => new Torque(value); + //[Pure] public static implicit operator Torque(float value) => new Torque(value); + //[Pure] public static implicit operator Torque(double value) => new Torque(value); + //[Pure] public static implicit operator Torque(decimal value) => new Torque((double)value); + + // Comparison + /// + /// Compare to another Torque object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Torque other) => Value == other.Value; + + /// + /// Equals operator to compare two Torque objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Torque left, Torque right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Torque objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Torque left, Torque right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Torque object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Torque other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Torque objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Torque objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Torque objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Torque objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Torque objects + /// + /// left value + /// right value + /// A new Torque object with a value of left + right + [Pure] public static Torque operator +(Torque left, Torque right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Torque objects + /// + /// left value + /// right value + /// A new Torque object with a value of left - right + [Pure] public static Torque operator -(Torque left, Torque right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Torque object with a value of value multiplied by the operand + [Pure] public static Torque operator *(Torque value, double operand) => new (value.Value * operand); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Torque(ushort value) => new Torque(value); - //[Pure] public static implicit operator Torque(short value) => new Torque(value); - //[Pure] public static implicit operator Torque(uint value) => new Torque(value); - //[Pure] public static implicit operator Torque(long value) => new Torque(value); - //[Pure] public static implicit operator Torque(int value) => new Torque(value); - //[Pure] public static implicit operator Torque(float value) => new Torque(value); - //[Pure] public static implicit operator Torque(double value) => new Torque(value); - //[Pure] public static implicit operator Torque(decimal value) => new Torque((double)value); - - // Comparison - /// - /// Compare to another Torque object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Torque other) => Value == other.Value; - - /// - /// Equals operator to compare two Torque objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Torque left, Torque right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Torque objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Torque left, Torque right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Torque object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Torque other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Torque objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Torque objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Torque objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Torque objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Torque left, Torque right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Torque objects - /// - /// left value - /// right value - /// A new Torque object with a value of left + right - [Pure] public static Torque operator +(Torque left, Torque right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Torque objects - /// - /// left value - /// right value - /// A new Torque object with a value of left - right - [Pure] public static Torque operator -(Torque left, Torque right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Torque object with a value of value multiplied by the operand - [Pure] public static Torque operator *(Torque value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Torque object with a value of value divided by the operand - [Pure] public static Torque operator /(Torque value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Torque Abs() { return new Torque(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Torque object - /// - /// The other Torque cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Torque object with a value of value divided by the operand + [Pure] public static Torque operator /(Torque value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Torque Abs() { return new Torque(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Torque object + /// + /// The other Torque cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Torque torque) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(torque.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Torque"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Turbidity.cs b/Source/Meadow.Units/Turbidity.cs index 6116296..1dc4c42 100644 --- a/Source/Meadow.Units/Turbidity.cs +++ b/Source/Meadow.Units/Turbidity.cs @@ -4,356 +4,356 @@ using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Turbidity (NTU) +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Turbidity : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Turbidity (NTU) + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Turbidity : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Turbidity value. + /// Turbidity unit. + public Turbidity(double value, UnitType type = UnitType.NTU) { - /// - /// Creates a new `Turbidity` object. - /// - /// The Turbidity value. - /// Turbidity unit. - public Turbidity(double value, UnitType type = UnitType.NTU) - { - if(value < 0) throw new ArgumentOutOfRangeException($"Turbidity cannot be less than 0"); - NTU = value; - } + if(value < 0) throw new ArgumentOutOfRangeException($"Turbidity cannot be less than 0"); + NTU = value; + } - /// - /// Creates a new `Turbidity` object from an existing Turbidity object - /// - /// - public Turbidity(Turbidity Turbidity) - { - NTU = Turbidity.NTU; - } + /// + /// Creates a new object from an existing Turbidity object + /// + /// + public Turbidity(Turbidity Turbidity) + { + NTU = Turbidity.NTU; + } - /// - /// The Turbidity expressed as Nephelometric Turbidity Units (NTU) - /// - public double NTU { get; private set; } + /// + /// The Turbidity expressed as Nephelometric Turbidity Units (NTU) + /// + public double NTU { get; private set; } - /// - /// The type of units available to describe the Turbidity. - /// - public enum UnitType - { - /// - /// Turbidity (NTU) - /// - NTU - } + /// + /// The type of units available to describe the Turbidity. + /// + public enum UnitType + { + /// Turbidity (NTU) + NTU + } - /// - /// Compare to another Turbidity object - /// - /// The object to compare - /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Turbidity)obj); - } + /// + /// Compare to another Turbidity object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => NTU.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Turbidity(ushort value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(short value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(uint value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(long value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(int value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(float value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(double value) => new Turbidity(value); + //[Pure] public static implicit operator Turbidity(decimal value) => new Turbidity((double)value); + + // Comparison + /// + /// Compare to another Turbidity object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Turbidity other) => NTU == other.NTU; + + /// + /// Equals operator to compare two Turbidity objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Turbidity left, Turbidity right) => Equals(left.NTU, right.NTU); + + /// + /// Not equals operator to compare two Turbidity objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Turbidity left, Turbidity right) => !Equals(left.NTU, right.NTU); + + /// + /// Compare to another Turbidity object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Turbidity other) => Equals(NTU, other.NTU) ? 0 : NTU.CompareTo(other.NTU); + + /// + /// Less than operator to compare two Turbidity objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) < 0; + + /// + /// Greater than operator to compare two Turbidity objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) > 0; + + /// + /// Less than or equal operator to compare two Turbidity objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) <= 0; + + /// + /// Greater than or equal operator to compare two Turbidity objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) >= 0; + + // Math + /// + /// Addition operator to add two Turbidity objects + /// + /// left value + /// right value + /// A new Turbidity object with a value of left + right + [Pure] public static Turbidity operator +(Turbidity left, Turbidity right) => new(left.NTU + right.NTU); + + /// + /// Subtraction operator to subtract two Turbidity objects + /// + /// left value + /// right value + /// A new Turbidity object with a value of left - right + [Pure] public static Turbidity operator -(Turbidity left, Turbidity right) => new(left.NTU - right.NTU); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Turbidity object with a value of value multiplied by the operand + [Pure] public static Turbidity operator *(Turbidity value, double operand) => new(value.NTU * operand); + + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Turbidity object with a value of value divided by the operand + [Pure] public static Turbidity operator /(Turbidity value, double operand) => new(value.NTU / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Turbidity Abs() { return new Turbidity(Math.Abs(NTU)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => NTU.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => NTU.ToString(format, formatProvider); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => NTU.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Turbidity(ushort value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(short value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(uint value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(long value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(int value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(float value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(double value) => new Turbidity(value); - //[Pure] public static implicit operator Turbidity(decimal value) => new Turbidity((double)value); - - // Comparison - /// - /// Compare to another Turbidity object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Turbidity other) => NTU == other.NTU; - - /// - /// Equals operator to compare two Turbidity objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Turbidity left, Turbidity right) => Equals(left.NTU, right.NTU); - - /// - /// Not equals operator to compare two Turbidity objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Turbidity left, Turbidity right) => !Equals(left.NTU, right.NTU); - - /// - /// Compare to another Turbidity object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Turbidity other) => Equals(NTU, other.NTU) ? 0 : NTU.CompareTo(other.NTU); - - /// - /// Less than operator to compare two Turbidity objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) < 0; - - /// - /// Greater than operator to compare two Turbidity objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) > 0; - - /// - /// Less than or equal operator to compare two Turbidity objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) <= 0; - - /// - /// Greater than or equal operator to compare two Turbidity objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Turbidity left, Turbidity right) => Comparer.Default.Compare(left.NTU, right.NTU) >= 0; - - // Math - /// - /// Addition operator to add two Turbidity objects - /// - /// left value - /// right value - /// A new Turbidity object with a value of left + right - [Pure] public static Turbidity operator +(Turbidity left, Turbidity right) => new(left.NTU + right.NTU); - - /// - /// Subtraction operator to subtract two Turbidity objects - /// - /// left value - /// right value - /// A new Turbidity object with a value of left - right - [Pure] public static Turbidity operator -(Turbidity left, Turbidity right) => new(left.NTU - right.NTU); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Turbidity object with a value of value multiplied by the operand - [Pure] public static Turbidity operator *(Turbidity value, double operand) => new(value.NTU * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Turbidity object with a value of value divided by the operand - [Pure] public static Turbidity operator /(Turbidity value, double operand) => new(value.NTU / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Turbidity Abs() { return new Turbidity(Math.Abs(this.NTU)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => NTU.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => NTU.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Turbidity object - /// - /// The other Turbidity cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => NTU.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => NTU.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)NTU).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)NTU).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)NTU).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)NTU).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)NTU).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => NTU; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)NTU).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)NTU).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)NTU).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)NTU).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)NTU).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => NTU.ToString(provider); - - /// - /// Convert to type - /// - /// unit to convert - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)NTU).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)NTU).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)NTU).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)NTU).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] - public int CompareTo(double? other) + // IComparable + /// + /// Compare to another Turbidity object + /// + /// The other Turbidity cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Turbidity turbidity) { - return (other is null) ? -1 : (NTU).CompareTo(other.Value); + return NTU.CompareTo(turbidity.NTU); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => NTU.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => NTU.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => NTU.CompareTo(other); + throw new ArgumentException("Object is not a Turbidity"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => NTU.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)NTU).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)NTU).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)NTU).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)NTU).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)NTU).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => NTU; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)NTU).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)NTU).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)NTU).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)NTU).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)NTU).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => NTU.ToString(provider); + + /// + /// Convert to type + /// + /// unit to convert + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)NTU).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)NTU).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)NTU).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)NTU).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] + public int CompareTo(double? other) + { + return (other is null) ? -1 : (NTU).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => NTU.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => NTU.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => NTU.CompareTo(other); } \ No newline at end of file diff --git a/Source/Meadow.Units/Voltage.cs b/Source/Meadow.Units/Voltage.cs index 91100c4..fe18ea3 100644 --- a/Source/Meadow.Units/Voltage.cs +++ b/Source/Meadow.Units/Voltage.cs @@ -30,7 +30,7 @@ static Voltage() public static Voltage Zero => _zero; /// - /// Creates a new `Voltage` object. + /// Creates a new object. /// /// The Voltage value. /// Volts by default. @@ -40,7 +40,7 @@ public Voltage(double value, UnitType type = UnitType.Volts) } /// - /// Creates a new `Voltage` object from an existing Voltage object + /// Creates a new object from an existing Voltage object /// /// public Voltage(Voltage voltage) @@ -101,7 +101,7 @@ public enum UnitType [Pure] public double From(UnitType convertTo) { - return VoltageConversions.Convert(Value, Voltage.UnitType.Volts, convertTo); + return VoltageConversions.Convert(Value, UnitType.Volts, convertTo); } /// @@ -109,13 +109,7 @@ public double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] - public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Voltage)obj); - } + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; /// /// Get hash of object @@ -233,7 +227,7 @@ public override bool Equals(object obj) /// Returns the absolute value of the /// /// - [Pure] public Voltage Abs() { return new Voltage(Math.Abs(this.Value)); } + [Pure] public Voltage Abs() { return new Voltage(Math.Abs(Value)); } /// /// Get a string representation of the object @@ -255,7 +249,16 @@ public override bool Equals(object obj) /// /// The other Voltage cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is Voltage voltage) + { + return Value.CompareTo(voltage.Value); + } + + throw new ArgumentException("Object is not a Voltage"); + } /// /// Get type code of object diff --git a/Source/Meadow.Units/Volume.cs b/Source/Meadow.Units/Volume.cs index ce7bb73..49dc4d4 100644 --- a/Source/Meadow.Units/Volume.cs +++ b/Source/Meadow.Units/Volume.cs @@ -5,424 +5,418 @@ using System.Runtime.InteropServices; using Meadow.Units.Conversions; -namespace Meadow.Units +namespace Meadow.Units; + +/// +/// Represents Volume +/// +[Serializable] +[ImmutableObject(true)] +[StructLayout(LayoutKind.Sequential)] +public struct Volume : + IComparable, IFormattable, IConvertible, + IEquatable, IComparable { /// - /// Represents Volume + /// Creates a new object. /// - [Serializable] - [ImmutableObject(true)] - [StructLayout(LayoutKind.Sequential)] - public struct Volume : - IComparable, IFormattable, IConvertible, - IEquatable, IComparable + /// The Volume value. + /// kilometers meters per second by default. + public Volume(double value, UnitType type = UnitType.Liters) { - /// - /// Creates a new `Volume` object. - /// - /// The Volume value. - /// kilometers meters per second by default. - public Volume(double value, UnitType type = UnitType.Liters) - { - Value = VolumeConversions.Convert(value, type, UnitType.Liters); - } + Value = VolumeConversions.Convert(value, type, UnitType.Liters); + } - /// - /// Creates a new `Volume` object from an existing Volume object - /// - /// - public Volume(Volume volume) - { - Value = volume.Value; - } + /// + /// Creates a new object from an existing Volume object + /// + /// + public Volume(Volume volume) + { + Value = volume.Value; + } - /// - /// Internal canonical value. - /// - private readonly double Value; + /// + /// Internal canonical value. + /// + private readonly double Value; - /// - /// The type of units available to describe the Volume. - /// - public enum UnitType - { - /// - /// US Gallons - /// - Gallons, - /// - /// Fluid ounces - /// - Ounces, - /// - /// Cubic feet - /// - CubicFeet, - /// - /// Cubic inches - /// - CubicInches, - /// - /// Liters - /// - Liters, - /// - /// Centiliters - /// - Centiliters, - /// - /// Milliliters - /// - Milliliters, - /// - /// Cubic meters - /// - CubicMeters, - } + /// + /// The type of units available to describe the Volume. + /// + public enum UnitType + { + /// US Gallons + Gallons, + /// Fluid ounces + Ounces, + /// Cubic feet + CubicFeet, + /// Cubic inches + CubicInches, + /// Liters + Liters, + /// Centiliters + Centiliters, + /// Milliliters + Milliliters, + /// Cubic meters + CubicMeters, + } - /// - /// Get volume in US gallons - /// - public double Gallons => From(UnitType.Gallons); - /// - /// Get volume in fluid ounces - /// - public double Ounces => From(UnitType.Ounces); - /// - /// Get volume in cubic feet - /// - public double CubicFeet => From(UnitType.CubicFeet); - /// - /// Get volume in cubic inches - /// - public double CubicInches => From(UnitType.CubicInches); - /// - /// Get volume in liters - /// - public double Liters => From(UnitType.Liters); - /// - /// Get volume in centiliters - /// - public double Centiliters => From(UnitType.Centiliters); - /// - /// Get volume in milliliters - /// - public double Milliliters => From(UnitType.Milliliters); - /// - /// Get volume in cubic meters - /// - public double CubicMeters => From(UnitType.CubicMeters); - - /// - /// Get a double value for a specific unit - /// - /// unit to covert to - /// the converted value - [Pure] public double From(UnitType convertTo) - { - return VolumeConversions.Convert(Value, UnitType.Liters, convertTo); - } + /// + /// Get volume in US gallons + /// + public double Gallons => From(UnitType.Gallons); - /// - /// Compare to another Volume object - /// - /// The object to compare - /// true if equal - [Pure] public override bool Equals(object obj) - { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Volume)obj); - } + /// + /// Get volume in fluid ounces + /// + public double Ounces => From(UnitType.Ounces); + + /// + /// Get volume in cubic feet + /// + public double CubicFeet => From(UnitType.CubicFeet); + + /// + /// Get volume in cubic inches + /// + public double CubicInches => From(UnitType.CubicInches); + + /// + /// Get volume in liters + /// + public double Liters => From(UnitType.Liters); + + /// + /// Get volume in centiliters + /// + public double Centiliters => From(UnitType.Centiliters); + + /// + /// Get volume in milliliters + /// + public double Milliliters => From(UnitType.Milliliters); + + /// + /// Get volume in cubic meters + /// + public double CubicMeters => From(UnitType.CubicMeters); + + /// + /// Get a double value for a specific unit + /// + /// unit to covert to + /// the converted value + [Pure] public double From(UnitType convertTo) + { + return VolumeConversions.Convert(Value, UnitType.Liters, convertTo); + } + + /// + /// Compare to another Volume object + /// + /// The object to compare + /// true if equal + [Pure] public override bool Equals(object obj) => CompareTo(obj) == 0; + + /// + /// Get hash of object + /// + /// int32 hash value + [Pure] public override int GetHashCode() => Value.GetHashCode(); + + // implicit conversions + //[Pure] public static implicit operator Volume(ushort value) => new Volume(value); + //[Pure] public static implicit operator Volume(short value) => new Volume(value); + //[Pure] public static implicit operator Volume(uint value) => new Volume(value); + //[Pure] public static implicit operator Volume(long value) => new Volume(value); + //[Pure] public static implicit operator Volume(int value) => new Volume(value); + //[Pure] public static implicit operator Volume(float value) => new Volume(value); + //[Pure] public static implicit operator Volume(double value) => new Volume(value); + //[Pure] public static implicit operator Volume(decimal value) => new Volume((double)value); + + // Comparison + /// + /// Compare to another Volume object + /// + /// The object to compare + /// true if equal + [Pure] public bool Equals(Volume other) => Value == other.Value; + + /// + /// Equals operator to compare two Volume objects + /// + /// left value + /// right value + /// true if equal + [Pure] public static bool operator ==(Volume left, Volume right) => Equals(left.Value, right.Value); + + /// + /// Not equals operator to compare two Volume objects + /// + /// left value + /// right value + /// true if not equal + [Pure] public static bool operator !=(Volume left, Volume right) => !Equals(left.Value, right.Value); + + /// + /// Compare to another Volume object + /// + /// + /// 0 if equal + [Pure] public int CompareTo(Volume other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); + + /// + /// Less than operator to compare two Volume objects + /// + /// left value + /// right value + /// true if left is less than right + [Pure] public static bool operator <(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) < 0; + + /// + /// Greater than operator to compare two Volume objects + /// + /// left value + /// right value + /// true if left is greater than right + [Pure] public static bool operator >(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) > 0; + + /// + /// Less than or equal operator to compare two Volume objects + /// + /// left value + /// right value + /// true if left is less than or equal to right + [Pure] public static bool operator <=(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; + + /// + /// Greater than or equal operator to compare two Volume objects + /// + /// left value + /// right value + /// true if left is greater than or equal to right + [Pure] public static bool operator >=(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; + + // Math + /// + /// Addition operator to add two Volume objects + /// + /// left value + /// right value + /// A new Volume object with a value of left + right + [Pure] public static Volume operator +(Volume left, Volume right) => new (left.Value + right.Value); + + /// + /// Subtraction operator to subtract two Volume objects + /// + /// left value + /// right value + /// A new Volume object with a value of left - right + [Pure] public static Volume operator -(Volume left, Volume right) => new (left.Value - right.Value); + + /// + /// Multiplication operator to multiply by a double + /// + /// object to multiply + /// operand to multiply object + /// A new Volume object with a value of value multiplied by the operand + [Pure] public static Volume operator *(Volume value, double operand) => new (value.Value * operand); - /// - /// Get hash of object - /// - /// int32 hash value - [Pure] public override int GetHashCode() => Value.GetHashCode(); - - // implicit conversions - //[Pure] public static implicit operator Volume(ushort value) => new Volume(value); - //[Pure] public static implicit operator Volume(short value) => new Volume(value); - //[Pure] public static implicit operator Volume(uint value) => new Volume(value); - //[Pure] public static implicit operator Volume(long value) => new Volume(value); - //[Pure] public static implicit operator Volume(int value) => new Volume(value); - //[Pure] public static implicit operator Volume(float value) => new Volume(value); - //[Pure] public static implicit operator Volume(double value) => new Volume(value); - //[Pure] public static implicit operator Volume(decimal value) => new Volume((double)value); - - // Comparison - /// - /// Compare to another Volume object - /// - /// The object to compare - /// true if equal - [Pure] public bool Equals(Volume other) => Value == other.Value; - - /// - /// Equals operator to compare two Volume objects - /// - /// left value - /// right value - /// true if equal - [Pure] public static bool operator ==(Volume left, Volume right) => Equals(left.Value, right.Value); - - /// - /// Not equals operator to compare two Volume objects - /// - /// left value - /// right value - /// true if not equal - [Pure] public static bool operator !=(Volume left, Volume right) => !Equals(left.Value, right.Value); - - /// - /// Compare to another Volume object - /// - /// - /// 0 if equal - [Pure] public int CompareTo(Volume other) => Equals(Value, other.Value) ? 0 : Value.CompareTo(other.Value); - - /// - /// Less than operator to compare two Volume objects - /// - /// left value - /// right value - /// true if left is less than right - [Pure] public static bool operator <(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) < 0; - - /// - /// Greater than operator to compare two Volume objects - /// - /// left value - /// right value - /// true if left is greater than right - [Pure] public static bool operator >(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) > 0; - - /// - /// Less than or equal operator to compare two Volume objects - /// - /// left value - /// right value - /// true if left is less than or equal to right - [Pure] public static bool operator <=(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) <= 0; - - /// - /// Greater than or equal operator to compare two Volume objects - /// - /// left value - /// right value - /// true if left is greater than or equal to right - [Pure] public static bool operator >=(Volume left, Volume right) => Comparer.Default.Compare(left.Value, right.Value) >= 0; - - // Math - /// - /// Addition operator to add two Volume objects - /// - /// left value - /// right value - /// A new Volume object with a value of left + right - [Pure] public static Volume operator +(Volume left, Volume right) => new (left.Value + right.Value); - - /// - /// Subtraction operator to subtract two Volume objects - /// - /// left value - /// right value - /// A new Volume object with a value of left - right - [Pure] public static Volume operator -(Volume left, Volume right) => new (left.Value - right.Value); - - /// - /// Multiplication operator to multiply by a double - /// - /// object to multiply - /// operand to multiply object - /// A new Volume object with a value of value multiplied by the operand - [Pure] public static Volume operator *(Volume value, double operand) => new (value.Value * operand); - - /// - /// Division operator to divide by a double - /// - /// object to be divided - /// operand to divide object - /// A new Volume object with a value of value divided by the operand - [Pure] public static Volume operator /(Volume value, double operand) => new (value.Value / operand); - - /// - /// Returns the absolute value of the - /// - /// - [Pure] public Volume Abs() { return new Volume(Math.Abs(this.Value)); } - - /// - /// Get a string representation of the object - /// - /// A string representing the object - [Pure] public override string ToString() => Value.ToString(); - - /// - /// Get a string representation of the object - /// - /// format - /// format provider - /// A string representing the object - [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); - - // IComparable - /// - /// Compare to another Volume object - /// - /// The other Volume cast to object - /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); - - /// - /// Get type code of object - /// - /// The TypeCode - [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); - - /// - /// Convert to boolean - /// - /// format provider - /// bool representation of the object - [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); - - /// - /// Convert to byte - /// - /// format provider - /// byte representation of the object - [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); - - /// - /// Convert to char - /// - /// format provider - /// char representation of the object - [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); - - /// - /// Convert to DateTime - /// - /// format provider - /// DateTime representation of the object - [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); - - /// - /// Convert to Decimal - /// - /// format provider - /// Decimal representation of the object - [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); - - /// - /// Convert to double - /// - /// format provider - /// double representation of the object - [Pure] public double ToDouble(IFormatProvider provider) => Value; - - /// - /// Convert to in16 - /// - /// format provider - /// int16 representation of the object - [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); - - /// - /// Convert to int32 - /// - /// format provider - /// int32 representation of the object - [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); - - /// - /// Convert to int64 - /// - /// format provider - /// int64 representation of the object - [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); - - /// - /// Convert to sbyte - /// - /// format provider - /// sbyte representation of the object - [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); - - /// - /// Convert to float - /// - /// format provider - /// float representation of the object - [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); - - /// - /// Convert to string - /// - /// format provider - /// string representation of the object - [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); - - /// - /// Convert to type - /// - /// type to convert to - /// format provider - /// type representation of the object - [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); - - /// - /// Convert to uint16 - /// - /// format provider - /// uint16 representation of the object - [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); - - /// - /// Convert to uint32 - /// - /// format provider - /// uint32 representation of the object - [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); - - /// - /// Convert to uint64 - /// - /// format provider - /// uint64 representation of the object - [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double? other) + /// + /// Division operator to divide by a double + /// + /// object to be divided + /// operand to divide object + /// A new Volume object with a value of value divided by the operand + [Pure] public static Volume operator /(Volume value, double operand) => new (value.Value / operand); + + /// + /// Returns the absolute value of the + /// + /// + [Pure] public Volume Abs() { return new Volume(Math.Abs(Value)); } + + /// + /// Get a string representation of the object + /// + /// A string representing the object + [Pure] public override string ToString() => Value.ToString(); + + /// + /// Get a string representation of the object + /// + /// format + /// format provider + /// A string representing the object + [Pure] public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider); + + // IComparable + /// + /// Compare to another Volume object + /// + /// The other Volume cast to object + /// 0 if equal + [Pure] + public int CompareTo(object obj) + { + if (obj is Volume volume) { - return (other is null) ? -1 : (Value).CompareTo(other.Value); + return Value.CompareTo(volume.Value); } - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double? other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public bool Equals(double other) => Value.Equals(other); - - /// - /// Compare the default value to a double - /// - /// value to compare - /// 0 if equal - [Pure] public int CompareTo(double other) => Value.CompareTo(other); + throw new ArgumentException("Object is not a Volume"); + } + + /// + /// Get type code of object + /// + /// The TypeCode + [Pure] public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + /// Convert to boolean + /// + /// format provider + /// bool representation of the object + [Pure] public bool ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + /// Convert to byte + /// + /// format provider + /// byte representation of the object + [Pure] public byte ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + /// Convert to char + /// + /// format provider + /// char representation of the object + [Pure] public char ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + /// Convert to DateTime + /// + /// format provider + /// DateTime representation of the object + [Pure] public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + /// Convert to Decimal + /// + /// format provider + /// Decimal representation of the object + [Pure] public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + /// Convert to double + /// + /// format provider + /// double representation of the object + [Pure] public double ToDouble(IFormatProvider provider) => Value; + + /// + /// Convert to in16 + /// + /// format provider + /// int16 representation of the object + [Pure] public short ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + /// Convert to int32 + /// + /// format provider + /// int32 representation of the object + [Pure] public int ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + /// Convert to int64 + /// + /// format provider + /// int64 representation of the object + [Pure] public long ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + /// Convert to sbyte + /// + /// format provider + /// sbyte representation of the object + [Pure] public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + /// Convert to float + /// + /// format provider + /// float representation of the object + [Pure] public float ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + /// Convert to string + /// + /// format provider + /// string representation of the object + [Pure] public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + /// Convert to type + /// + /// type to convert to + /// format provider + /// type representation of the object + [Pure] public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); + + /// + /// Convert to uint16 + /// + /// format provider + /// uint16 representation of the object + [Pure] public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + /// Convert to uint32 + /// + /// format provider + /// uint32 representation of the object + [Pure] public uint ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + /// Convert to uint64 + /// + /// format provider + /// uint64 representation of the object + [Pure] public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double? other) + { + return (other is null) ? -1 : (Value).CompareTo(other.Value); } + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double? other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public bool Equals(double other) => Value.Equals(other); + + /// + /// Compare the default value to a double + /// + /// value to compare + /// 0 if equal + [Pure] public int CompareTo(double other) => Value.CompareTo(other); } \ No newline at end of file