Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates and fixes for Illuminance #59

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions Source/Meadow.Units/Illuminance.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -17,6 +17,18 @@ public struct Illuminance :
IComparable, IFormattable, IConvertible,
IEquatable<double>, IComparable<double>
{
private static Illuminance _zero;

static Illuminance()
{
_zero = new Illuminance(0, UnitType.Lux);
}

/// <summary>
/// Gets a voltage of 0 Volts
/// </summary>
public static Illuminance Zero => _zero;

/// <summary>
/// Creates a new `Illuminance` object.
/// </summary>
Expand Down Expand Up @@ -78,7 +90,8 @@ public enum UnitType
/// </summary>
/// <param name="convertTo">unit to convert to</param>
/// <returns></returns>
[Pure] public double From(UnitType convertTo)
[Pure]
public double From(UnitType convertTo)
{
return IlluminanceConversions.Convert(Value, UnitType.Lux, convertTo);
}
Expand All @@ -88,11 +101,10 @@ [Pure] public double From(UnitType convertTo)
/// </summary>
/// <param name="obj">The object to compare</param>
/// <returns>true if equal</returns>
[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;
}

/// <summary>
Expand All @@ -101,16 +113,6 @@ [Pure] public override bool Equals(object obj)
/// <returns>int32 hash value</returns>
[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
/// <summary>
/// Compare to another Illuminance object
Expand Down Expand Up @@ -181,37 +183,37 @@ [Pure] public override bool Equals(object obj)
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>A new Illuminance object with a value of left + right</returns>
[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);

/// <summary>
/// Subtraction operator to subtract two Illuminance objects
/// </summary>
/// <param name="left">left value</param>
/// <param name="right">right value</param>
/// <returns>A new Illuminance object with a value of left - right</returns>
[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);

/// <summary>
/// Multiplication operator to multiply by a double
/// </summary>
/// <param name="value">object to multiply</param>
/// <param name="operand">operand to multiply object</param>
/// <returns>A new Illuminance object with a value of value multiplied by the operand</returns>
[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);

/// <summary>
/// Division operator to divide by a double
/// </summary>
/// <param name="value">object to be divided</param>
/// <param name="operand">operand to divide object</param>
/// <returns>A new Illuminance object with a value of value divided by the operand</returns>
[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);

/// <summary>
/// Returns the absolute value of the <see cref="Illuminance"/>
/// </summary>
/// <returns></returns>
[Pure] public Illuminance Abs() => new (Math.Abs(Value));
[Pure] public Illuminance Abs() => new(Math.Abs(Value));

/// <summary>
/// Get a string representation of the object
Expand All @@ -229,11 +231,20 @@ [Pure] public override bool Equals(object obj)

// IComparable
/// <summary>
/// Compare to another AbsoluteHumidity object
/// Compare to another Illuminance object
/// </summary>
/// <param name="obj">The other AbsoluteHumity cast to object</param>
/// <param name="obj">The other Illuminance cast to object</param>
/// <returns>0 if equal</returns>
[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");
}

/// <summary>
/// Get type code of object
Expand Down Expand Up @@ -359,7 +370,8 @@ [Pure] public override bool Equals(object obj)
/// </summary>
/// <param name="other">value to compare</param>
/// <returns>0 if equal</returns>
[Pure] public int CompareTo(double? other)
[Pure]
public int CompareTo(double? other)
{
return (other is null) ? -1 : (Value).CompareTo(other.Value);
}
Expand Down
Loading