Big changes
This commit is contained in:
342
Source/TSRealLib/Common/Raytheon.Common/Units/Current.cs
Normal file
342
Source/TSRealLib/Common/Raytheon.Common/Units/Current.cs
Normal file
@@ -0,0 +1,342 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents an electrical current.
|
||||
///
|
||||
/// It has no constructors. Instead, it provides multiple methods for creating objects of this
|
||||
/// type. Those methods are of the form FromXXX, where XXX is a unit (e.g.
|
||||
/// FromAmps).
|
||||
///
|
||||
/// Properties (e.g. Amps) are provided to convert the value to other units and return as type
|
||||
/// double.
|
||||
///
|
||||
/// Methods (operator overloads) are provided to perform calculations with related types (e.g.
|
||||
/// Voltage, Resistance).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Current
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Amps,
|
||||
Milliamps
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Amps, new List<string>(){ "A" } },
|
||||
{Unit.Milliamps, new List<string>(){ "mA" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Amps;
|
||||
private const int MilliampsPerAmp = 1000;
|
||||
|
||||
[DataMember(Name = "Current", IsRequired = true)]
|
||||
private double _current; // amps
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Current>> _unitInfo = new Dictionary<Unit, UnitInfo<Current>>()
|
||||
{
|
||||
{ Unit.Amps, new UnitInfo<Current>(UnitAbbreviations[Unit.Amps], FromAmps, typeof(Current).GetProperty("Amps").GetGetMethod()) },
|
||||
{ Unit.Milliamps, new UnitInfo<Current>(UnitAbbreviations[Unit.Milliamps], FromMilliamps, typeof(Current).GetProperty("Milliamps").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Current"/> class from being created.
|
||||
/// Use FromXXX methods to create objects of this type.
|
||||
/// </summary>
|
||||
/// <exception cref="System.Exception">No one should be calling this, it is only here to prevent users from creating default object</exception>
|
||||
private Current()
|
||||
{
|
||||
throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ctor used by this class to create objects in FromXXX methods
|
||||
/// </summary>
|
||||
/// <param name="current">The current - amps.</param>
|
||||
private Current(double current)
|
||||
{
|
||||
_current = current;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns amps as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// amps
|
||||
/// </value>
|
||||
public double Amps
|
||||
{
|
||||
get { return _current; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns milliamps as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// milliamps
|
||||
/// </value>
|
||||
public double Milliamps
|
||||
{
|
||||
get { return _current * MilliampsPerAmp; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Current object interpreting passed value as amps
|
||||
/// </summary>
|
||||
/// <param name="amps">amps</param>
|
||||
/// <returns></returns>
|
||||
public static Current FromAmps(double amps)
|
||||
{
|
||||
return new Current(amps);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Current object interpreting passed value as milliamps
|
||||
/// </summary>
|
||||
/// <param name="milliamps">milliamps</param>
|
||||
/// <returns></returns>
|
||||
public static Current FromMilliamps(double milliamps)
|
||||
{
|
||||
return new Current(milliamps / MilliampsPerAmp);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Current c = obj as Current;
|
||||
|
||||
return (c == null) ? false : (this._current == c._current);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _current.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Current operator +(Current left, Current right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Current");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Current");
|
||||
|
||||
return new Current(left._current + right._current);
|
||||
}
|
||||
|
||||
|
||||
public static Current operator -(Current left, Current right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Current");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Current");
|
||||
|
||||
return new Current(left._current - right._current);
|
||||
}
|
||||
|
||||
|
||||
public static Current operator *(Current current, double scalar)
|
||||
{
|
||||
if (null == current)
|
||||
throw new ArgumentNullException("current", "Cannot multiply a null Current");
|
||||
|
||||
return new Current(current._current * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Current operator *(double scalar, Current current)
|
||||
{
|
||||
if (null == current)
|
||||
throw new ArgumentNullException("current", "Cannot multiply a null Current");
|
||||
|
||||
return new Current(scalar * current._current);
|
||||
}
|
||||
|
||||
|
||||
public static Current operator /(Current current, double scalar)
|
||||
{
|
||||
if (null == current)
|
||||
throw new ArgumentNullException("current", "Cannot divide a null Current");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Current by 0");
|
||||
|
||||
return new Current(current._current / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Current left, Current right)
|
||||
{
|
||||
bool bReturn = false;
|
||||
if (object.ReferenceEquals(left, null))
|
||||
{
|
||||
if (object.ReferenceEquals(right, null))
|
||||
{
|
||||
//both are null, so we are ==
|
||||
bReturn = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!object.ReferenceEquals(left, null) &&
|
||||
!object.ReferenceEquals(right, null))
|
||||
{
|
||||
bReturn = left._current == right._current;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Current left, Current right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Current left, Current right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Current");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Current");
|
||||
|
||||
return (left._current < right._current);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Current left, Current right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Current");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Current");
|
||||
|
||||
return (left._current > right._current);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Current left, Current right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Current");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Current");
|
||||
|
||||
return (left._current <= right._current);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Current left, Current right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Current");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Current");
|
||||
|
||||
return (left._current >= right._current);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Voltage = Current * Resistance
|
||||
/// </summary>
|
||||
/// <param name="current">The current.</param>
|
||||
/// <param name="resistance">The resistance.</param>
|
||||
/// <returns>
|
||||
/// Voltage
|
||||
/// </returns>
|
||||
|
||||
public static Voltage operator *(Current current, Resistance resistance)
|
||||
{
|
||||
if (null == current)
|
||||
throw new ArgumentNullException("current", "Cannot multiply a null Current");
|
||||
|
||||
if (null == resistance)
|
||||
throw new ArgumentNullException("resistance", "Cannot multiply null Resistance");
|
||||
|
||||
return Voltage.FromVolts(current.Amps * resistance.Ohms);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Current Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Current value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Current.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string appended with default units (e.g. "1.23 m/sec^2").
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A string that represents this instance
|
||||
/// </returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return ToString(DefaultUnits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// overload that returns a string appended with specified units (e.g. "1.23 ft/sec^2").
|
||||
/// </summary>
|
||||
/// <param name="units"></param>
|
||||
/// <returns></returns>
|
||||
public string ToString(Unit units)
|
||||
{
|
||||
double value = (double)_unitInfo[units].PropertyGetter.Invoke(this, null);
|
||||
|
||||
return $"{value} {_unitInfo[units].Abbreviation[0]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user