diff --git a/Source/Meadow.Units/Illuminance.cs b/Source/Meadow.Units/Illuminance.cs index d8cc85e..bc9d08a 100644 --- a/Source/Meadow.Units/Illuminance.cs +++ b/Source/Meadow.Units/Illuminance.cs @@ -1,9 +1,9 @@ -using System; +using Meadow.Units.Conversions; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.InteropServices; -using Meadow.Units.Conversions; namespace Meadow.Units { @@ -17,6 +17,18 @@ 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; + /// /// Creates a new `Illuminance` object. /// @@ -78,7 +90,8 @@ public enum UnitType /// /// unit to convert to /// - [Pure] public double From(UnitType convertTo) + [Pure] + public double From(UnitType convertTo) { return IlluminanceConversions.Convert(Value, UnitType.Lux, convertTo); } @@ -88,11 +101,10 @@ [Pure] public double From(UnitType convertTo) /// /// The object to compare /// true if equal - [Pure] public override bool Equals(object obj) + [Pure] + public override bool Equals(object obj) { - if (obj is null) { return false; } - if (Equals(this, obj)) { return true; } - return obj.GetType() == GetType() && Equals((Illuminance)obj); + return this.CompareTo(obj) == 0; } /// @@ -101,16 +113,6 @@ [Pure] public override bool Equals(object obj) /// int32 hash value [Pure] public override int GetHashCode() => Value.GetHashCode(); - // implicit conversions - //[Pure] public static implicit operator Illuminance(ushort value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(short value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(uint value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(long value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(int value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(float value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(double value) => new Illuminance(value); - //[Pure] public static implicit operator Illuminance(decimal value) => new Illuminance((double)value); - // Comparison /// /// Compare to another Illuminance object @@ -181,7 +183,7 @@ [Pure] public override bool Equals(object obj) /// 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); + [Pure] public static Illuminance operator +(Illuminance left, Illuminance right) => new(left.Value + right.Value); /// /// Subtraction operator to subtract two Illuminance objects @@ -189,7 +191,7 @@ [Pure] public override bool Equals(object obj) /// 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); + [Pure] public static Illuminance operator -(Illuminance left, Illuminance right) => new(left.Value - right.Value); /// /// Multiplication operator to multiply by a double @@ -197,7 +199,7 @@ [Pure] public override bool Equals(object obj) /// 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); + [Pure] public static Illuminance operator *(Illuminance value, double operand) => new(value.Value * operand); /// /// Division operator to divide by a double @@ -205,13 +207,13 @@ [Pure] public override bool Equals(object obj) /// 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); + [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)); + [Pure] public Illuminance Abs() => new(Math.Abs(Value)); /// /// Get a string representation of the object @@ -229,11 +231,20 @@ [Pure] public override bool Equals(object obj) // IComparable /// - /// Compare to another AbsoluteHumidity object + /// Compare to another Illuminance object /// - /// The other AbsoluteHumity cast to object + /// The other Illuminance cast to object /// 0 if equal - [Pure] public int CompareTo(object obj) => Value.CompareTo(obj); + [Pure] + public int CompareTo(object obj) + { + if (obj is Illuminance illuminance) + { + return Value.CompareTo(illuminance.Value); + } + + throw new ArgumentException("Object is not an Illuminance"); + } /// /// Get type code of object @@ -359,7 +370,8 @@ [Pure] public override bool Equals(object obj) /// /// value to compare /// 0 if equal - [Pure] public int CompareTo(double? other) + [Pure] + public int CompareTo(double? other) { return (other is null) ? -1 : (Value).CompareTo(other.Value); }