Big changes
This commit is contained in:
382
Source/TSRealLib/Common/Raytheon.Common/Units/Acceleration.cs
Normal file
382
Source/TSRealLib/Common/Raytheon.Common/Units/Acceleration.cs
Normal file
@@ -0,0 +1,382 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents an acceleration.
|
||||
///
|
||||
/// 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.
|
||||
/// FromMetersPerSecSqrd).
|
||||
///
|
||||
/// Properties (e.g. FeetPerSecSqrd) 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.
|
||||
/// Velocity, Length).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Acceleration
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
FeetPerSecSqrd,
|
||||
MetersPerSecSqrd,
|
||||
Gs
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.FeetPerSecSqrd, new List<string>(){ "ft/sec^2" } },
|
||||
{Unit.MetersPerSecSqrd, new List<string>(){ "m/sec^2" } },
|
||||
{Unit.Gs, new List<string>(){ "g" } }
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.MetersPerSecSqrd;
|
||||
private const double FeetPerMeter = 3.280839895013123;
|
||||
private const double Gravity = 9.80665; // m/sec^2
|
||||
|
||||
[DataMember(Name = "Acceleration", IsRequired = true)]
|
||||
private double _acceleration; // meters/seconds^2
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Acceleration>> _unitInfo = new Dictionary<Unit, UnitInfo<Acceleration>>()
|
||||
{
|
||||
{ Unit.FeetPerSecSqrd, new UnitInfo<Acceleration>(UnitAbbreviations[Unit.FeetPerSecSqrd], FromFeetPerSecSqrd, typeof(Acceleration).GetProperty("FeetPerSecSqrd").GetGetMethod()) },
|
||||
{ Unit.MetersPerSecSqrd, new UnitInfo<Acceleration>(UnitAbbreviations[Unit.MetersPerSecSqrd], FromMetersPerSecSqrd, typeof(Acceleration).GetProperty("MetersPerSecSqrd").GetGetMethod()) },
|
||||
{ Unit.Gs, new UnitInfo<Acceleration>(UnitAbbreviations[Unit.Gs], FromGs, typeof(Acceleration).GetProperty("Gs").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Acceleration"/> 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 Acceleration()
|
||||
{
|
||||
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="acceleration">The acceleration - m/sec^2.</param>
|
||||
private Acceleration(double acceleration)
|
||||
{
|
||||
_acceleration = acceleration;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns feet/sec^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// feet/sec^2
|
||||
/// </value>
|
||||
public double FeetPerSecSqrd
|
||||
{
|
||||
get { return _acceleration * FeetPerMeter; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns Gs as type double
|
||||
/// </summary>
|
||||
public double Gs
|
||||
{
|
||||
get { return _acceleration / Gravity; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns m/sec^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// m/sec^2
|
||||
/// </value>
|
||||
public double MetersPerSecSqrd
|
||||
{
|
||||
get { return _acceleration; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Acceleration object interpreting passed value as ft/sec^2
|
||||
/// </summary>
|
||||
/// <param name="feetPerSecSqrd">ft/sec^2</param>
|
||||
/// <returns></returns>
|
||||
public static Acceleration FromFeetPerSecSqrd(double feetPerSecSqrd)
|
||||
{
|
||||
return new Acceleration(feetPerSecSqrd / FeetPerMeter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Acceleration object interpreting passed value as m/sec^2
|
||||
/// </summary>
|
||||
/// <param name="metersPerSecSqrd">m/sec^2</param>
|
||||
/// <returns></returns>
|
||||
public static Acceleration FromMetersPerSecSqrd(double metersPerSecSqrd)
|
||||
{
|
||||
return new Acceleration(metersPerSecSqrd);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Acceleration object interpreting passed value as m/sec^2
|
||||
/// </summary>
|
||||
/// <param name="metersPerSecSqrd">m/sec^2</param>
|
||||
/// <returns></returns>
|
||||
public static Acceleration FromGs(double g)
|
||||
{
|
||||
return FromMetersPerSecSqrd(g * Gravity);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Acceleration Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Acceleration value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Acceleration.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Acceleration c = obj as Acceleration;
|
||||
|
||||
return (c == null) ? false : (this._acceleration == c._acceleration);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _acceleration.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Acceleration operator +(Acceleration left, Acceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Acceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Acceleration");
|
||||
|
||||
return new Acceleration(left._acceleration + right._acceleration);
|
||||
}
|
||||
|
||||
|
||||
public static Acceleration operator -(Acceleration left, Acceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Acceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Acceleration");
|
||||
|
||||
return new Acceleration(left._acceleration - right._acceleration);
|
||||
}
|
||||
|
||||
|
||||
public static Acceleration operator *(Acceleration acceleration, double scalar)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply a null Acceleration");
|
||||
|
||||
return new Acceleration(acceleration._acceleration * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Acceleration operator *(double scalar, Acceleration acceleration)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply a null Acceleration");
|
||||
|
||||
return new Acceleration(scalar * acceleration._acceleration);
|
||||
}
|
||||
|
||||
|
||||
public static Acceleration operator /(Acceleration acceleration, double scalar)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot divide a null Acceleration");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Acceleration by 0");
|
||||
|
||||
return new Acceleration(acceleration._acceleration / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Acceleration left, Acceleration 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._acceleration == right._acceleration;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Acceleration left, Acceleration right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Acceleration left, Acceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Acceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Acceleration");
|
||||
|
||||
return left._acceleration < right._acceleration;
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Acceleration left, Acceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Acceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Acceleration");
|
||||
|
||||
return left._acceleration > right._acceleration;
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Acceleration left, Acceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Acceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Acceleration");
|
||||
|
||||
return left._acceleration <= right._acceleration;
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Acceleration left, Acceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Acceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Acceleration");
|
||||
|
||||
return left._acceleration >= right._acceleration;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Velocity = Acceleration * Time
|
||||
/// </summary>
|
||||
/// <param name="acceleration">The acceleration.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Velocity
|
||||
/// </returns>
|
||||
|
||||
public static Velocity operator *(Acceleration acceleration, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply with null Acceleration");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");
|
||||
|
||||
return Velocity.FromMetersPerSec(acceleration.MetersPerSecSqrd * time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Velocity = Time * Acceleration
|
||||
/// </summary>
|
||||
/// <param name="acceleration">The acceleration.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Velocity
|
||||
/// </returns>
|
||||
|
||||
public static Velocity operator *(PrecisionTimeSpan time, Acceleration acceleration)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply with null Acceleration");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");
|
||||
|
||||
return Velocity.FromMetersPerSec(acceleration.MetersPerSecSqrd * time.Seconds);
|
||||
}
|
||||
|
||||
#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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
392
Source/TSRealLib/Common/Raytheon.Common/Units/Angle.cs
Normal file
392
Source/TSRealLib/Common/Raytheon.Common/Units/Angle.cs
Normal file
@@ -0,0 +1,392 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents an angle.
|
||||
///
|
||||
/// 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.
|
||||
/// FromDegrees).
|
||||
///
|
||||
/// Properties (e.g. Radians) are provided to convert the value to other units and return
|
||||
/// as type double.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Angle
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Degrees,
|
||||
Radians,
|
||||
Milliradians
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Degrees, new List<string>(){ "deg" } },
|
||||
{Unit.Radians, new List<string>(){ "rad" } },
|
||||
{Unit.Milliradians, new List<string>(){ "mrad" } }
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Degrees;
|
||||
private const double DegreesPerRadian = 180 / Math.PI;
|
||||
private const int MilliradiansPerRadian = 1000;
|
||||
|
||||
[DataMember(Name = "Angle", IsRequired = true)]
|
||||
private double _angle; // radians
|
||||
|
||||
private static IDictionary<Unit, UnitInfo<Angle>> _unitInfo = new Dictionary<Unit, UnitInfo<Angle>>()
|
||||
{
|
||||
{ Unit.Degrees, new UnitInfo<Angle>(UnitAbbreviations[Unit.Degrees], FromDegrees, typeof(Angle).GetProperty("Degrees").GetGetMethod()) },
|
||||
{ Unit.Radians, new UnitInfo<Angle>(UnitAbbreviations[Unit.Radians], FromRadians, typeof(Angle).GetProperty("Radians").GetGetMethod()) },
|
||||
{ Unit.Milliradians, new UnitInfo<Angle>(UnitAbbreviations[Unit.Milliradians], FromMilliradians, typeof(Angle).GetProperty("Milliradians").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Angle"/> 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 Angle()
|
||||
{
|
||||
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="angle">The angle - radians.</param>
|
||||
private Angle(double angle)
|
||||
{
|
||||
_angle = angle;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns degrees as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// degrees
|
||||
/// </value>
|
||||
public double Degrees
|
||||
{
|
||||
get
|
||||
{
|
||||
return _angle * DegreesPerRadian;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns milliradians as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// milliradians
|
||||
/// </value>
|
||||
public double Milliradians
|
||||
{
|
||||
get
|
||||
{
|
||||
return _angle * MilliradiansPerRadian;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns radians as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// radians
|
||||
/// </value>
|
||||
public double Radians
|
||||
{
|
||||
get
|
||||
{
|
||||
return _angle;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Angle object interpreting passed value as degrees
|
||||
/// </summary>
|
||||
/// <param name="degrees">degrees</param>
|
||||
/// <returns></returns>
|
||||
public static Angle FromDegrees(double degrees)
|
||||
{
|
||||
return new Angle(degrees / DegreesPerRadian);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Angle object interpreting passed value as milliradians
|
||||
/// </summary>
|
||||
/// <param name="milliradians">milliradians</param>
|
||||
/// <returns></returns>
|
||||
public static Angle FromMilliradians(double milliradians)
|
||||
{
|
||||
return new Angle(milliradians / MilliradiansPerRadian);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Angle object interpreting passed value as radians
|
||||
/// </summary>
|
||||
/// <param name="radians">radians</param>
|
||||
/// <returns></returns>
|
||||
public static Angle FromRadians(double radians)
|
||||
{
|
||||
return new Angle(radians);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Angle a = obj as Angle;
|
||||
|
||||
return (a == null) ? false : (this._angle == a._angle);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _angle.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Angle operator +(Angle left, Angle right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Angle");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Angle");
|
||||
|
||||
return new Angle(left._angle + right._angle);
|
||||
}
|
||||
|
||||
|
||||
public static Angle operator -(Angle left, Angle right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Angle");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Angle");
|
||||
|
||||
return new Angle(left._angle - right._angle);
|
||||
}
|
||||
|
||||
|
||||
public static Angle operator *(Angle angle, double scalar)
|
||||
{
|
||||
if (null == angle)
|
||||
throw new ArgumentNullException("angle", "Cannot multiply a null Angle");
|
||||
|
||||
return new Angle(angle._angle * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Angle operator *(double scalar, Angle angle)
|
||||
{
|
||||
if (null == angle)
|
||||
throw new ArgumentNullException("angle", "Cannot multiply a null Angle");
|
||||
|
||||
return new Angle(scalar * angle._angle);
|
||||
}
|
||||
|
||||
|
||||
public static Angle operator /(Angle angle, double scalar)
|
||||
{
|
||||
if (null == angle)
|
||||
throw new ArgumentNullException("angle", "Cannot divide a null Angle");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Angle by 0");
|
||||
|
||||
return new Angle(angle._angle / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Angle left, Angle 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._angle == right._angle;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Angle left, Angle right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static bool operator <(Angle left, Angle right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
||||
|
||||
return (left._angle < right._angle);
|
||||
}
|
||||
|
||||
public static bool operator >(Angle left, Angle right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
||||
|
||||
return (left._angle > right._angle);
|
||||
}
|
||||
|
||||
public static bool operator <=(Angle left, Angle right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
||||
|
||||
return (left._angle <= right._angle);
|
||||
}
|
||||
|
||||
public static bool operator >=(Angle left, Angle right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Angle");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Angle");
|
||||
|
||||
return (left._angle >= right._angle);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator / for AngularVelocity = Angle / PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="velocity">The angle.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// AngularVelocity
|
||||
/// </returns>
|
||||
|
||||
public static AngularVelocity operator /(Angle angle, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == angle)
|
||||
throw new ArgumentNullException("angle", "Cannot divide with null Angle");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot divide with null PrecisionTimeSpan");
|
||||
|
||||
if (0.0 == time.Milliseconds)
|
||||
throw new DivideByZeroException("Cannot divide angle by 0 time");
|
||||
|
||||
return AngularVelocity.FromRadiansPerSec(angle.Radians / time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator / for PrecisionTimeSpan = Angle / AngularVelocity
|
||||
/// </summary>
|
||||
/// <param name="velocity">The angle.</param>
|
||||
/// <param name="time">The velocity.</param>
|
||||
/// <returns>
|
||||
/// PrecisionTimeSpan
|
||||
/// </returns>
|
||||
|
||||
public static PrecisionTimeSpan operator /(Angle angle, AngularVelocity velocity)
|
||||
{
|
||||
if (null == angle)
|
||||
throw new ArgumentNullException("angle", "Cannot divide with null Angle");
|
||||
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide with null AngularVelocity");
|
||||
|
||||
if (0.0 == velocity.RadiansPerSec)
|
||||
throw new DivideByZeroException("Cannot divide angle by 0 angular velocity");
|
||||
|
||||
return PrecisionTimeSpan.FromSeconds(angle.Radians / velocity.RadiansPerSec);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Angle Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Angle value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Angle.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,383 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a velocity.
|
||||
///
|
||||
/// 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. FromRadiansPerSecSqrd).
|
||||
///
|
||||
/// Properties (e.g. RadiansPerSecSqrd) 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.
|
||||
/// Angle, AngularVelocity).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class AngularAcceleration
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
DegreesPerSecSqrd,
|
||||
RadiansPerSecSqrd,
|
||||
MilliradiansPerSecSqrd
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.DegreesPerSecSqrd, new List<string>(){ "deg/sec^2" } },
|
||||
{Unit.MilliradiansPerSecSqrd, new List<string>(){ "mrad/sec^2" } },
|
||||
{Unit.RadiansPerSecSqrd, new List<string>(){ "rad/sec^2" } }
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.RadiansPerSecSqrd;
|
||||
private const double DegreesPerRadian = 180 / Math.PI;
|
||||
|
||||
[DataMember(Name = "AngularAcceleration", IsRequired = true)]
|
||||
private double _acceleration; // radians/seconds^2
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<AngularAcceleration>> _unitInfo = new Dictionary<Unit, UnitInfo<AngularAcceleration>>()
|
||||
{
|
||||
{ Unit.DegreesPerSecSqrd, new UnitInfo<AngularAcceleration>(UnitAbbreviations[Unit.DegreesPerSecSqrd], FromDegreesPerSecSqrd, typeof(AngularAcceleration).GetProperty("DegreesPerSecSqrd").GetGetMethod()) },
|
||||
{ Unit.MilliradiansPerSecSqrd, new UnitInfo<AngularAcceleration>(UnitAbbreviations[Unit.MilliradiansPerSecSqrd], FromMilliradiansPerSecSqrd, typeof(AngularAcceleration).GetProperty("MilliradiansPerSecSqrd").GetGetMethod()) },
|
||||
{ Unit.RadiansPerSecSqrd, new UnitInfo<AngularAcceleration>(UnitAbbreviations[Unit.RadiansPerSecSqrd], FromRadiansPerSecSqrd, typeof(AngularAcceleration).GetProperty("RadiansPerSecSqrd").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="AngularAcceleration"/> 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 AngularAcceleration()
|
||||
{
|
||||
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="acceleration">The acceleration - radians/sec^2.</param>
|
||||
private AngularAcceleration(double acceleration)
|
||||
{
|
||||
_acceleration = acceleration;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns degrees/sec^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// degrees/sec^2
|
||||
/// </value>
|
||||
public double DegreesPerSecSqrd
|
||||
{
|
||||
get { return _acceleration * DegreesPerRadian; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns milliradians/sec^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// milliradians/sec^2
|
||||
/// </value>
|
||||
public double MilliradiansPerSecSqrd
|
||||
{
|
||||
get { return _acceleration * 1000; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns radians/sec^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// radians/sec^2
|
||||
/// </value>
|
||||
public double RadiansPerSecSqrd
|
||||
{
|
||||
get { return _acceleration; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns AngularAcceleration object interpreting passed value as degrees/sec
|
||||
/// </summary>
|
||||
/// <param name="degreesPerSecSqrd">degrees/sec</param>
|
||||
/// <returns></returns>
|
||||
public static AngularAcceleration FromDegreesPerSecSqrd(double degreesPerSecSqrd)
|
||||
{
|
||||
return new AngularAcceleration(degreesPerSecSqrd / DegreesPerRadian);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns AngularAcceleration object interpreting passed value as radians/sec^2
|
||||
/// </summary>
|
||||
/// <param name="milliradiansPerSecSqrd">radians/sec^2</param>
|
||||
/// <returns></returns>
|
||||
public static AngularAcceleration FromMilliradiansPerSecSqrd(double milliradiansPerSecSqrd)
|
||||
{
|
||||
return new AngularAcceleration(milliradiansPerSecSqrd / 1000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns AngularAcceleration object interpreting passed value as radians/sec^2
|
||||
/// </summary>
|
||||
/// <param name="radiansPerSecSqrd">radians/sec^2</param>
|
||||
/// <returns></returns>
|
||||
public static AngularAcceleration FromRadiansPerSecSqrd(double radiansPerSecSqrd)
|
||||
{
|
||||
return new AngularAcceleration(radiansPerSecSqrd);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
AngularAcceleration a = obj as AngularAcceleration;
|
||||
|
||||
return (a == null) ? false : (this._acceleration == a._acceleration);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _acceleration.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static AngularAcceleration operator +(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null AngularAcceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null AngularAcceleration");
|
||||
|
||||
return new AngularAcceleration(left._acceleration + right._acceleration);
|
||||
}
|
||||
|
||||
|
||||
public static AngularAcceleration operator -(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null AngularAcceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract null AngularAcceleration");
|
||||
|
||||
return new AngularAcceleration(left._acceleration - right._acceleration);
|
||||
}
|
||||
|
||||
|
||||
public static AngularAcceleration operator *(AngularAcceleration acceleration, double scalar)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply a null AngularAcceleration");
|
||||
|
||||
return new AngularAcceleration(acceleration._acceleration * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static AngularAcceleration operator *(double scalar, AngularAcceleration acceleration)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply a null AngularAcceleration");
|
||||
|
||||
return new AngularAcceleration(scalar * acceleration._acceleration);
|
||||
}
|
||||
|
||||
|
||||
public static AngularAcceleration operator /(AngularAcceleration acceleration, double scalar)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot divide a null AngularAcceleration");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Angular Acceleration by 0");
|
||||
|
||||
return new AngularAcceleration(acceleration._acceleration / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(AngularAcceleration left, AngularAcceleration 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._acceleration == right._acceleration;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");
|
||||
|
||||
return left._acceleration < right._acceleration;
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");
|
||||
|
||||
return left._acceleration > right._acceleration;
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");
|
||||
|
||||
return left._acceleration <= right._acceleration;
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(AngularAcceleration left, AngularAcceleration right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularAcceleration");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularAcceleration");
|
||||
|
||||
return left._acceleration >= right._acceleration;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for AngularVelocity = AngularAcceleration * PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="acceleration">The velocity.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Angle
|
||||
/// </returns>
|
||||
|
||||
public static AngularVelocity operator *(AngularAcceleration acceleration, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply with null AngularAcceleration");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");
|
||||
|
||||
return AngularVelocity.FromRadiansPerSec(acceleration.RadiansPerSecSqrd * time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for AngularVelocity = PrecisionTimeSpan * AngularAcceleration
|
||||
/// </summary>
|
||||
/// <param name="acceleration">The acceleration.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// AngularVelocity
|
||||
/// </returns>
|
||||
|
||||
public static AngularVelocity operator *(PrecisionTimeSpan time, AngularAcceleration acceleration)
|
||||
{
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot multiply with null AngularAcceleration");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");
|
||||
|
||||
return AngularVelocity.FromRadiansPerSec(time.Seconds * acceleration.RadiansPerSecSqrd);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static AngularAcceleration Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out AngularAcceleration value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = AngularAcceleration.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string appended with default units (e.g. "1.23 deg/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 deg/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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
430
Source/TSRealLib/Common/Raytheon.Common/Units/AngularVelocity.cs
Normal file
430
Source/TSRealLib/Common/Raytheon.Common/Units/AngularVelocity.cs
Normal file
@@ -0,0 +1,430 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a velocity.
|
||||
///
|
||||
/// 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. FromRadiansPerSec).
|
||||
///
|
||||
/// Properties (e.g. RadiansPerSec) 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.
|
||||
/// Angle, AngularAcceleration).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class AngularVelocity
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
DegreesPerSec,
|
||||
RadiansPerSec,
|
||||
MilliradiansPerSec
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.DegreesPerSec, new List<string>(){ "deg/sec" } },
|
||||
{Unit.RadiansPerSec, new List<string>(){ "rad/sec" } },
|
||||
{Unit.MilliradiansPerSec, new List<string>(){ "mrad/sec" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.RadiansPerSec;
|
||||
private const double DegreesPerRadian = 180 / Math.PI;
|
||||
|
||||
[DataMember(Name = "AngularVelocity", IsRequired = true)]
|
||||
private double _velocity; // radians/second
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<AngularVelocity>> _unitInfo = new Dictionary<Unit, UnitInfo<AngularVelocity>>()
|
||||
{
|
||||
{ Unit.DegreesPerSec, new UnitInfo<AngularVelocity>(UnitAbbreviations[Unit.DegreesPerSec], FromDegreesPerSec, typeof(AngularVelocity).GetProperty("DegreesPerSec").GetGetMethod()) },
|
||||
{ Unit.MilliradiansPerSec, new UnitInfo<AngularVelocity>(UnitAbbreviations[Unit.MilliradiansPerSec], FromMilliradiansPerSec, typeof(AngularVelocity).GetProperty("MilliradiansPerSec").GetGetMethod()) },
|
||||
{ Unit.RadiansPerSec, new UnitInfo<AngularVelocity>(UnitAbbreviations[Unit.RadiansPerSec], FromRadiansPerSec, typeof(AngularVelocity).GetProperty("RadiansPerSec").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="AngularVelocity"/> 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 AngularVelocity()
|
||||
{
|
||||
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="velocity">The velocity - radians/sec.</param>
|
||||
private AngularVelocity(double velocity)
|
||||
{
|
||||
_velocity = velocity;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns degrees/sec as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// degrees/sec
|
||||
/// </value>
|
||||
public double DegreesPerSec
|
||||
{
|
||||
get { return _velocity * DegreesPerRadian; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns degrees/sec as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// degrees/sec
|
||||
/// </value>
|
||||
public double MilliradiansPerSec
|
||||
{
|
||||
get { return _velocity * 1000; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns radians/sec as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// radians/sec
|
||||
/// </value>
|
||||
public double RadiansPerSec
|
||||
{
|
||||
get { return _velocity; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns AngularVelocity object interpreting passed value as degrees/sec
|
||||
/// </summary>
|
||||
/// <param name="degreesPerSec">degrees/sec</param>
|
||||
/// <returns></returns>
|
||||
public static AngularVelocity FromDegreesPerSec(double degreesPerSec)
|
||||
{
|
||||
return new AngularVelocity(degreesPerSec / DegreesPerRadian);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns AngularVelocity object interpreting passed value as milliradians/sec
|
||||
/// </summary>
|
||||
/// <param name="milliradiansPerSec">milliradians/sec</param>
|
||||
/// <returns></returns>
|
||||
public static AngularVelocity FromMilliradiansPerSec(double milliradiansPerSec)
|
||||
{
|
||||
return new AngularVelocity(milliradiansPerSec / 1000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns AngularVelocity object interpreting passed value as radians/sec
|
||||
/// </summary>
|
||||
/// <param name="radiansPerSec">radians/sec</param>
|
||||
/// <returns></returns>
|
||||
public static AngularVelocity FromRadiansPerSec(double radiansPerSec)
|
||||
{
|
||||
return new AngularVelocity(radiansPerSec);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
AngularVelocity v = obj as AngularVelocity;
|
||||
|
||||
return (v == null) ? false : (this._velocity == v._velocity);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _velocity.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static AngularVelocity operator +(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null AngularVelocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null AngularVelocity");
|
||||
|
||||
return new AngularVelocity(left._velocity + right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static AngularVelocity operator -(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null AngularVelocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null AngularVelocity");
|
||||
|
||||
return new AngularVelocity(left._velocity - right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static AngularVelocity operator *(AngularVelocity velocity, double scalar)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply a null AngularVelocity");
|
||||
|
||||
return new AngularVelocity(velocity._velocity * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static AngularVelocity operator *(double scalar, AngularVelocity velocity)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply a null AngularVelocity");
|
||||
|
||||
return new AngularVelocity(scalar * velocity._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static AngularVelocity operator /(AngularVelocity velocity, double scalar)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide a null AngularVelocity");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Angular Velocity by 0");
|
||||
|
||||
return new AngularVelocity(velocity._velocity / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(AngularVelocity left, AngularVelocity 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._velocity == right._velocity;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularVelocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularVelocity");
|
||||
|
||||
return (left._velocity < right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularVelocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularVelocity");
|
||||
|
||||
return (left._velocity > right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularVelocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularVelocity");
|
||||
|
||||
return (left._velocity <= right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(AngularVelocity left, AngularVelocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null AngularVelocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null AngularVelocity");
|
||||
|
||||
return (left._velocity >= right._velocity);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Angle = AngularVelocity * PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Angle
|
||||
/// </returns>
|
||||
|
||||
public static Angle operator *(AngularVelocity velocity, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply with null AngularVelocity");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");
|
||||
|
||||
return Angle.FromRadians(velocity.RadiansPerSec * time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Angle = PrecisionTimeSpan * AngularVelocity
|
||||
/// </summary>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <returns>
|
||||
/// Angle
|
||||
/// </returns>
|
||||
|
||||
public static Angle operator *(PrecisionTimeSpan time, AngularVelocity velocity)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply with null AngularVelocity");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply with null PrecisionTimeSpan");
|
||||
|
||||
return Angle.FromRadians(time.Seconds * velocity.RadiansPerSec);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator / for AngularAcceleration = AngularVelocity / PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// AngularAcceleration
|
||||
/// </returns>
|
||||
|
||||
public static AngularAcceleration operator /(AngularVelocity velocity, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide with null AngularVelocity");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot divide with null PrecisionTimeSpan");
|
||||
|
||||
if (0.0 == time.Milliseconds)
|
||||
throw new DivideByZeroException("Cannot divide Angular Velocity by 0 time");
|
||||
|
||||
return AngularAcceleration.FromRadiansPerSecSqrd(velocity.RadiansPerSec / time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator / for PrecisionTimeSpan = AngularVelocity / AngularAcceleration
|
||||
/// </summary>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <param name="acceleration">The acceleration.</param>
|
||||
/// <returns>
|
||||
/// Angle
|
||||
/// </returns>
|
||||
|
||||
public static PrecisionTimeSpan operator /(AngularVelocity velocity, AngularAcceleration acceleration)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide with null AngularVelocity");
|
||||
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot divide with null AngularAcceleration");
|
||||
|
||||
if (0.0 == acceleration.RadiansPerSecSqrd)
|
||||
throw new DivideByZeroException("Cannot divide Angular Velocity by 0 Angular Acceleration");
|
||||
|
||||
return PrecisionTimeSpan.FromSeconds(velocity.RadiansPerSec / acceleration.RadiansPerSecSqrd);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static AngularVelocity Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out AngularVelocity value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = AngularVelocity.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
83
Source/TSRealLib/Common/Raytheon.Common/Units/Common.cs
Normal file
83
Source/TSRealLib/Common/Raytheon.Common/Units/Common.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains info related to a unit: to parse, print, and convert values
|
||||
/// </summary>
|
||||
internal class UnitInfo<T>
|
||||
{
|
||||
public UnitInfo( List<string> abbreviation,
|
||||
Func<double, T> conversionMethod,
|
||||
MethodInfo propertyGetter )
|
||||
{
|
||||
Abbreviation = abbreviation;
|
||||
ConversionMethod = conversionMethod;
|
||||
PropertyGetter = propertyGetter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// standard text abbreviation for this unit
|
||||
/// </summary>
|
||||
public List<string> Abbreviation { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// method for converting string to this unit
|
||||
/// </summary>
|
||||
public Func<double, T> ConversionMethod { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// get method for the property associated with this unit
|
||||
/// </summary>
|
||||
public MethodInfo PropertyGetter { get; set; }
|
||||
}
|
||||
|
||||
internal class Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a string that represents a number with a unit to the actual
|
||||
/// unit type.
|
||||
/// </summary>
|
||||
/// <typeparam name="E">Enumeration of the Unit abbreviation</typeparam>
|
||||
/// <typeparam name="T">The actual unit type, i.e Current</typeparam>
|
||||
/// <param name="s">String that represents a unit value, i.e 1.23 Amps</param>
|
||||
/// <param name="unitInfo">Look up table for how to convert the units</param>
|
||||
/// <returns>
|
||||
/// The converted number to the correct unit type. If unable to parse, will return the first
|
||||
/// <see cref="UnitInfo{T}.ConversionMethod"/> with a value of <see cref="double.NaN"/>
|
||||
/// </returns>
|
||||
public static T Parse<E, T>( string s, IDictionary<E, UnitInfo<T>> unitInfo )
|
||||
{
|
||||
const string FloatingPointPattern = @"([-+]?[0-9]+(\.[0-9]+)?([Ee][+-][0-9]+)?)";
|
||||
const string AbbreviationPattern = @"([\w/\^]*)";
|
||||
string pattern = FloatingPointPattern + @"\s*" + AbbreviationPattern;
|
||||
|
||||
Regex regEx = new Regex( pattern );
|
||||
Match match = regEx.Match( s );
|
||||
|
||||
if(!match.Success || match.Groups.Count == 0 )
|
||||
{
|
||||
throw new FormatException( $"UUnknown value : {s}" );
|
||||
}
|
||||
|
||||
string abbreviation = match.Groups[match.Groups.Count - 1].Value;
|
||||
|
||||
IEnumerable<UnitInfo<T>> x = unitInfo.Where( kvp => kvp.Value.Abbreviation.Contains( abbreviation, StringComparer.CurrentCultureIgnoreCase ) )
|
||||
.Select( kvp => kvp.Value );
|
||||
|
||||
// abbreviation not found?
|
||||
if ( x.Count( ) == 0 )
|
||||
{
|
||||
throw new FormatException( $"Unknown units: '{abbreviation}'" );
|
||||
}
|
||||
|
||||
double value = double.Parse( match.Groups[1].Value );
|
||||
|
||||
return x.First( ).ConversionMethod( value );
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Source/TSRealLib/Common/Raytheon.Common/Units/Constants.cs
Normal file
14
Source/TSRealLib/Common/Raytheon.Common/Units/Constants.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
public class Constants
|
||||
{
|
||||
// SI Prefixes decimal value
|
||||
public const double GIGA = 1e9;
|
||||
public const double MEGA = 1e6;
|
||||
public const double KILO = 1e3;
|
||||
public const double CENTI = 1e-2;
|
||||
public const double MILLI = 1e-3;
|
||||
public const double MICRO = 1e-6;
|
||||
public const double NANO = 1e-9;
|
||||
}
|
||||
}
|
||||
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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
322
Source/TSRealLib/Common/Raytheon.Common/Units/Energy.cs
Normal file
322
Source/TSRealLib/Common/Raytheon.Common/Units/Energy.cs
Normal file
@@ -0,0 +1,322 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents energy.
|
||||
///
|
||||
/// 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. FromJoulesPerCmSqrd).
|
||||
///
|
||||
/// Properties (e.g. JoulesPerCmSqrd) are provided to convert the value to other units and return
|
||||
/// as type double.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Energy
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
JoulesPerCmSqrd,
|
||||
MillijoulesPerCmSqrd
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.JoulesPerCmSqrd, new List<string>(){ "J/cm^2" } },
|
||||
{Unit.MillijoulesPerCmSqrd, new List<string>(){ "mJ/cm^2" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.JoulesPerCmSqrd;
|
||||
|
||||
[DataMember(Name = "Energy", IsRequired = true)]
|
||||
private double _energy; // joules/centimeter^2
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Energy>> _unitInfo = new Dictionary<Unit, UnitInfo<Energy>>()
|
||||
{
|
||||
{ Unit.JoulesPerCmSqrd, new UnitInfo<Energy>(UnitAbbreviations[Unit.JoulesPerCmSqrd], FromJoulesPerCmSqrd, typeof(Energy).GetProperty("JoulesPerCmSqrd").GetGetMethod()) },
|
||||
{ Unit.MillijoulesPerCmSqrd, new UnitInfo<Energy>(UnitAbbreviations[Unit.MillijoulesPerCmSqrd], FromMillijoulesPerCmSqrd, typeof(Energy).GetProperty("MillijoulesPerCmSqrd").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Energy"/> 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 Energy()
|
||||
{
|
||||
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="energy">The energy - joules/cm^2.</param>
|
||||
private Energy(double energy)
|
||||
{
|
||||
_energy = energy;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns joules/cm^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// joules/cm^2
|
||||
/// </value>
|
||||
|
||||
public double JoulesPerCmSqrd
|
||||
{
|
||||
get { return _energy; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns joules/cm^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// joules/cm^2
|
||||
/// </value>
|
||||
|
||||
public double MillijoulesPerCmSqrd
|
||||
{
|
||||
get { return _energy * 1000; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Energy object interpreting passed value as joules/cm^2
|
||||
/// </summary>
|
||||
/// <param name="joulesPerCmSqrd">joules/cm^2</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static Energy FromJoulesPerCmSqrd(double joulesPerCmSqrd)
|
||||
{
|
||||
return new Energy(joulesPerCmSqrd);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Energy object interpreting passed value as joules/cm^2
|
||||
/// </summary>
|
||||
/// <param name="joulesPerCmSqrd">joules/cm^2</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static Energy FromMillijoulesPerCmSqrd(double joulesPerCmSqrd)
|
||||
{
|
||||
return new Energy(joulesPerCmSqrd / 1000);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Energy e = obj as Energy;
|
||||
|
||||
return (e == null) ? false : (this._energy == e._energy);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _energy.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Energy operator +(Energy left, Energy right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Energy");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Energy");
|
||||
|
||||
return new Energy(left._energy + right._energy);
|
||||
}
|
||||
|
||||
|
||||
public static Energy operator -(Energy left, Energy right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Energy");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Energy");
|
||||
|
||||
return new Energy(left._energy - right._energy);
|
||||
}
|
||||
|
||||
|
||||
public static Energy operator *(Energy energy, double scalar)
|
||||
{
|
||||
if (null == energy)
|
||||
throw new ArgumentNullException("energy", "Cannot multiply a null Energy");
|
||||
|
||||
return new Energy(energy._energy * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Energy operator *(double scalar, Energy energy)
|
||||
{
|
||||
if (null == energy)
|
||||
throw new ArgumentNullException("energy", "Cannot multiply a null Energy");
|
||||
|
||||
return new Energy(scalar * energy._energy);
|
||||
}
|
||||
|
||||
|
||||
public static Energy operator /(Energy energy, double scalar)
|
||||
{
|
||||
if (null == energy)
|
||||
throw new ArgumentNullException("energy", "Cannot divide a null Energy");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Energy by 0");
|
||||
|
||||
return new Energy(energy._energy / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Energy left, Energy 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._energy == right._energy;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Energy left, Energy right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Energy left, Energy right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Energy");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Energy");
|
||||
|
||||
return (left._energy < right._energy);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Energy left, Energy right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Energy");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Energy");
|
||||
|
||||
return (left._energy > right._energy);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Energy left, Energy right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Energy");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Energy");
|
||||
|
||||
return (left._energy <= right._energy);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Energy left, Energy right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Energy");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Energy");
|
||||
|
||||
return (left._energy >= right._energy);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
// no operators right now
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Energy Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Energy value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Energy.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
356
Source/TSRealLib/Common/Raytheon.Common/Units/FlowRate.cs
Normal file
356
Source/TSRealLib/Common/Raytheon.Common/Units/FlowRate.cs
Normal file
@@ -0,0 +1,356 @@
|
||||
//############################################################################//
|
||||
// RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
|
||||
// PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
|
||||
// AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
|
||||
// UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
|
||||
// RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
|
||||
// CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
|
||||
// OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
|
||||
// COMPANY.
|
||||
//
|
||||
// THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. GOVERNMENT
|
||||
//
|
||||
// UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
|
||||
//
|
||||
// WARNING: THIS DOCUMENT CONTAINS TECHNICAL DATA AND / OR TECHNOLOGY WHOSE
|
||||
// EXPORT OR DISCLOSURE TO NON-U.S. PERSONS, WHEREVER LOCATED, IS RESTRICTED
|
||||
// BY THE INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) (22 C.F.R. SECTION
|
||||
// 120-130) OR THE EXPORT ADMINISTRATION REGULATIONS (EAR) (15 C.F.R. SECTION
|
||||
// 730-774). THIS DOCUMENT CANNOT BE EXPORTED (E.G., PROVIDED TO A SUPPLIER
|
||||
// OUTSIDE OF THE UNITED STATES) OR DISCLOSED TO A NON-U.S. PERSON, WHEREVER
|
||||
// LOCATED, UNTIL A FINAL JURISDICTION AND CLASSIFICATION DETERMINATION HAS
|
||||
// BEEN COMPLETED AND APPROVED BY RAYTHEON, AND ANY REQUIRED U.S. GOVERNMENT
|
||||
// APPROVALS HAVE BEEN OBTAINED. VIOLATIONS ARE SUBJECT TO SEVERE CRIMINAL
|
||||
// PENALTIES.
|
||||
//
|
||||
// DOD 5220.22-M, INDUSTRIAL SECURITY MANUAL, CHAPTER 5, SECTION 1 THROUGH 9 :
|
||||
// FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN OR DOD 5200.1-R,
|
||||
// INFORMATION SECURITY PROGRAM, CHAPTER 6. FOR UNCLASSIFIED, LIMITED DOCUMENTS
|
||||
// DESTROY BY ANY METHOD THAT WILL PREVENT DISCLOSURE OF CONTENTS OR
|
||||
// RECONSTRUCTION OF THE DOCUMENT.
|
||||
//############################################################################//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a Flow Rate.
|
||||
///
|
||||
/// 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. FromCubicMetersPerSecond).
|
||||
///
|
||||
/// Properties (e.g. CubicMetersPerSecond) are provided to convert the value to other units and return
|
||||
/// as type double.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class FlowRate
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
CubicMetersPerSecond,
|
||||
CubicMetersPerMinute,
|
||||
CubicMetersPerHour,
|
||||
GallonsPerMinute
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.CubicMetersPerSecond, new List<string>(){ "m^3/s" } },
|
||||
{Unit.CubicMetersPerMinute, new List<string>(){ "m^3/min" } },
|
||||
{Unit.CubicMetersPerHour, new List<string>(){ "m^3/h" } },
|
||||
{Unit.GallonsPerMinute, new List<string>(){ "gal/min" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.CubicMetersPerSecond;
|
||||
private const double CubicMetersToGalPerSecMultipler = 15850;
|
||||
private const int SecondsPerMinute = 60;
|
||||
private const int SecondsPerHour = 3600;
|
||||
|
||||
[DataMember(Name = "CubicMetersPerSecond", IsRequired = true)]
|
||||
private readonly double _flowRate;
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static readonly IDictionary<Unit, UnitInfo<FlowRate>> _unitInfo = new Dictionary<Unit, UnitInfo<FlowRate>>()
|
||||
{
|
||||
{ Unit.CubicMetersPerSecond, new UnitInfo<FlowRate>(UnitAbbreviations[Unit.CubicMetersPerSecond], FromCubicMetersPerSecond, typeof(FlowRate).GetProperty("CubicMetersPerSecond").GetGetMethod()) },
|
||||
{ Unit.CubicMetersPerMinute, new UnitInfo<FlowRate>(UnitAbbreviations[Unit.CubicMetersPerMinute], FromCubicMetersPerMinute, typeof(FlowRate).GetProperty("CubicMetersPerMinute").GetGetMethod()) },
|
||||
{ Unit.CubicMetersPerHour, new UnitInfo<FlowRate>(UnitAbbreviations[Unit.CubicMetersPerHour], FromCubicMetersPerHour, typeof(FlowRate).GetProperty("CubicMetersPerHour").GetGetMethod()) },
|
||||
{ Unit.GallonsPerMinute, new UnitInfo<FlowRate>(UnitAbbreviations[Unit.GallonsPerMinute], FromGallonsPerMinute, typeof(FlowRate).GetProperty("GallonsPerMinute").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="FlowRate"/> class from being created
|
||||
/// Use FromXXX methods to create objects of this type.
|
||||
/// </summary>
|
||||
private FlowRate()
|
||||
{
|
||||
throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor used by this class to create objects in FromXXX methods.
|
||||
/// </summary>
|
||||
/// <param name="flowRate">The flow rate - Cubic Meters per second</param>
|
||||
private FlowRate(double cubicMetersPerSecond)
|
||||
{
|
||||
_flowRate = cubicMetersPerSecond;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
/// <summary>
|
||||
/// Returns Cubic Meters Per Second as type double
|
||||
/// </summary>
|
||||
public double CubicMetersPerSecond { get { return _flowRate; } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns Cubic Meters Per Minutes as type double
|
||||
/// </summary>
|
||||
public double CubicMetersPerMinute { get { return _flowRate * SecondsPerMinute; } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns Cubic Meters Per Hour as type double
|
||||
/// </summary>
|
||||
public double CubicMetersPerHour { get { return _flowRate * SecondsPerHour; } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns Gallons Per Minute
|
||||
/// </summary>
|
||||
public double GallonsPerMinute { get { return _flowRate * CubicMetersToGalPerSecMultipler; } }
|
||||
#endregion
|
||||
|
||||
#region Creation Methods
|
||||
/// <summary>
|
||||
/// Returns FlowRate object interpreting passed value as Cubic Meters Per Second
|
||||
/// </summary>
|
||||
/// <param name="flowRate">Cubic Meters Per Second</param>
|
||||
/// <returns></returns>
|
||||
public static FlowRate FromCubicMetersPerSecond(double m3PerSec)
|
||||
{
|
||||
return new FlowRate(m3PerSec);
|
||||
}
|
||||
|
||||
public static FlowRate FromCubicMetersPerMinute(double m3PerMin)
|
||||
{
|
||||
return new FlowRate(m3PerMin / SecondsPerMinute);
|
||||
}
|
||||
|
||||
public static FlowRate FromCubicMetersPerHour(double m3PerHr)
|
||||
{
|
||||
return new FlowRate(m3PerHr / SecondsPerHour);
|
||||
}
|
||||
|
||||
public static FlowRate FromGallonsPerMinute(double gallonPerMin)
|
||||
{
|
||||
return new FlowRate(gallonPerMin / CubicMetersToGalPerSecMultipler);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
FlowRate flowRate = obj as FlowRate;
|
||||
|
||||
return (flowRate == null) ? false : (this._flowRate == flowRate._flowRate);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _flowRate.GetHashCode();
|
||||
}
|
||||
|
||||
#region Binary Operators
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
public static FlowRate operator +(FlowRate left, FlowRate right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot add a null Flow Rate");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot add null Flow Rate");
|
||||
}
|
||||
|
||||
return new FlowRate(left._flowRate + right._flowRate);
|
||||
}
|
||||
|
||||
public static FlowRate operator *(FlowRate flowRate, double scalar)
|
||||
{
|
||||
if (null == flowRate)
|
||||
{
|
||||
throw new ArgumentNullException("flowRate", "Cannot multiply a null Flow Rate");
|
||||
}
|
||||
|
||||
return new FlowRate(flowRate._flowRate * scalar);
|
||||
}
|
||||
|
||||
public static FlowRate operator *(double scalar, FlowRate flowRate)
|
||||
{
|
||||
if (null == flowRate)
|
||||
{
|
||||
throw new ArgumentNullException("flowRate", "Cannot multiply a null Flow Rate");
|
||||
}
|
||||
|
||||
return new FlowRate(scalar * flowRate._flowRate);
|
||||
}
|
||||
|
||||
public static FlowRate operator /(FlowRate flowRate, double scalar)
|
||||
{
|
||||
if (null == flowRate)
|
||||
{
|
||||
throw new ArgumentNullException("flowRate", "Cannot divide a null Flow Rate");
|
||||
}
|
||||
|
||||
if (0.0 == scalar)
|
||||
{
|
||||
throw new DivideByZeroException("Cannot divide Flow Rate by 0");
|
||||
}
|
||||
|
||||
return new FlowRate(flowRate._flowRate / scalar);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
public static bool operator ==(FlowRate left, FlowRate 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._flowRate == right._flowRate;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(FlowRate left, FlowRate right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static bool operator <(FlowRate left, FlowRate right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
return (left._flowRate < right._flowRate);
|
||||
}
|
||||
|
||||
public static bool operator >(FlowRate left, FlowRate right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
return (left._flowRate > right._flowRate);
|
||||
}
|
||||
|
||||
public static bool operator <=(FlowRate left, FlowRate right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
return (left._flowRate <= right._flowRate);
|
||||
}
|
||||
|
||||
public static bool operator >=(FlowRate left, FlowRate right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Flow Rate");
|
||||
}
|
||||
|
||||
return (left._flowRate >= right._flowRate);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static FlowRate Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out FlowRate value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = FlowRate.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="string" /> appended with units symbol (e.g. "1.23 M^3/s").
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
340
Source/TSRealLib/Common/Raytheon.Common/Units/Frequency.cs
Normal file
340
Source/TSRealLib/Common/Raytheon.Common/Units/Frequency.cs
Normal file
@@ -0,0 +1,340 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents energy.
|
||||
///
|
||||
/// 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. FromHertz).
|
||||
///
|
||||
/// Properties (e.g. Hertz) are provided to convert the value to other units and return as type
|
||||
/// double.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Frequency
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Hertz,
|
||||
KiloHertz,
|
||||
MegaHertz,
|
||||
GigaHertz
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Hertz, new List<string>(){ "Hz" } },
|
||||
{Unit.KiloHertz, new List<string>(){ "kHz" } },
|
||||
{Unit.MegaHertz, new List<string>(){ "MHz" } },
|
||||
{Unit.GigaHertz, new List<string>(){ "GHz" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Hertz;
|
||||
|
||||
[DataMember(Name = "Frequency", IsRequired = true)]
|
||||
private double _frequency; // Hertz
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Frequency>> _unitInfo = new Dictionary<Unit, UnitInfo<Frequency>>()
|
||||
{
|
||||
{ Unit.Hertz, new UnitInfo<Frequency>(UnitAbbreviations[Unit.Hertz], FromHertz, typeof(Frequency).GetProperty("Hertz").GetGetMethod()) },
|
||||
{ Unit.KiloHertz, new UnitInfo<Frequency>(UnitAbbreviations[Unit.KiloHertz], FromKiloHertz, typeof(Frequency).GetProperty("KiloHertz").GetGetMethod()) },
|
||||
{ Unit.MegaHertz, new UnitInfo<Frequency>(UnitAbbreviations[Unit.MegaHertz], FromMegaHertz, typeof(Frequency).GetProperty("MegaHertz").GetGetMethod()) },
|
||||
{ Unit.GigaHertz, new UnitInfo<Frequency>(UnitAbbreviations[Unit.GigaHertz], FromGigaHertz, typeof(Frequency).GetProperty("GigaHertz").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Frequency"/> 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 Frequency()
|
||||
{
|
||||
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="frequency">The frequency - Hertz.</param>
|
||||
private Frequency(double frequency)
|
||||
{
|
||||
_frequency = frequency;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
public double GigaHertz
|
||||
{
|
||||
get { return _frequency / Constants.GIGA; }
|
||||
}
|
||||
|
||||
public double Hertz
|
||||
{
|
||||
get { return _frequency; }
|
||||
}
|
||||
|
||||
public double KiloHertz
|
||||
{
|
||||
get { return _frequency / Constants.KILO; }
|
||||
}
|
||||
|
||||
public double MegaHertz
|
||||
{
|
||||
get { return _frequency / Constants.MEGA; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Frequency object interpreting passed value as Hertz
|
||||
/// </summary>
|
||||
/// <param name="frequency">Hertz</param>
|
||||
/// <returns></returns>
|
||||
public static Frequency FromGigaHertz(double frequency)
|
||||
{
|
||||
return new Frequency(frequency * Constants.GIGA);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Frequency object interpreting passed value as Hertz
|
||||
/// </summary>
|
||||
/// <param name="frequency">Hertz</param>
|
||||
/// <returns></returns>
|
||||
public static Frequency FromHertz(double frequency)
|
||||
{
|
||||
return new Frequency(frequency);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Frequency object interpreting passed value as Hertz
|
||||
/// </summary>
|
||||
/// <param name="frequency">Hertz</param>
|
||||
/// <returns></returns>
|
||||
public static Frequency FromKiloHertz(double frequency)
|
||||
{
|
||||
return new Frequency(frequency * Constants.KILO);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Frequency object interpreting passed value as Hertz
|
||||
/// </summary>
|
||||
/// <param name="frequency">Hertz</param>
|
||||
/// <returns></returns>
|
||||
public static Frequency FromMegaHertz(double frequency)
|
||||
{
|
||||
return new Frequency(frequency * Constants.MEGA);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Frequency e = obj as Frequency;
|
||||
|
||||
return (e == null) ? false : (this._frequency == e._frequency);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _frequency.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Frequency operator +(Frequency left, Frequency right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Frequency");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Frequency");
|
||||
|
||||
return new Frequency(left._frequency + right._frequency);
|
||||
}
|
||||
|
||||
|
||||
public static Frequency operator -(Frequency left, Frequency right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Frequency");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Frequency");
|
||||
|
||||
return new Frequency(left._frequency - right._frequency);
|
||||
}
|
||||
|
||||
|
||||
public static Frequency operator *(Frequency frequency, double scalar)
|
||||
{
|
||||
if (null == frequency)
|
||||
throw new ArgumentNullException("frequency", "Cannot multiply a null Frequency");
|
||||
|
||||
return new Frequency(frequency._frequency * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Frequency operator *(double scalar, Frequency frequency)
|
||||
{
|
||||
if (null == frequency)
|
||||
throw new ArgumentNullException("frequency", "Cannot multiply a null Frequency");
|
||||
|
||||
return new Frequency(scalar * frequency._frequency);
|
||||
}
|
||||
|
||||
|
||||
public static Frequency operator /(Frequency frequency, double scalar)
|
||||
{
|
||||
if (null == frequency)
|
||||
throw new ArgumentNullException("frequency", "Cannot divide a null Frequency");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Frequency by 0");
|
||||
|
||||
return new Frequency(frequency._frequency / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Frequency left, Frequency 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._frequency == right._frequency;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Frequency left, Frequency right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Frequency left, Frequency right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Frequency");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Frequency");
|
||||
|
||||
return (left._frequency < right._frequency);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Frequency left, Frequency right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Frequency");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Frequency");
|
||||
|
||||
return (left._frequency > right._frequency);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Frequency left, Frequency right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Frequency");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Frequency");
|
||||
|
||||
return (left._frequency <= right._frequency);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Frequency left, Frequency right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Frequency");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Frequency");
|
||||
|
||||
return (left._frequency >= right._frequency);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
// no operators right now
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Frequency Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Frequency value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Frequency.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
412
Source/TSRealLib/Common/Raytheon.Common/Units/Length.cs
Normal file
412
Source/TSRealLib/Common/Raytheon.Common/Units/Length.cs
Normal file
@@ -0,0 +1,412 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a length.
|
||||
///
|
||||
/// 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. FromMeters).
|
||||
///
|
||||
/// Properties (e.g. Meters) 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.
|
||||
/// Velocity).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Length
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Meters,
|
||||
Centimeters,
|
||||
Millimeters,
|
||||
Micrometers,
|
||||
Nanometers,
|
||||
Inches,
|
||||
Feet
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Meters, new List<string>(){ "m" } },
|
||||
{Unit.Centimeters, new List<string>(){ "cm" } },
|
||||
{Unit.Millimeters, new List<string>(){ "mm" } },
|
||||
{Unit.Micrometers, new List<string>(){ "um" } },
|
||||
{Unit.Nanometers, new List<string>(){ "nm" } },
|
||||
{Unit.Inches, new List<string>(){ "in" } },
|
||||
{Unit.Feet, new List<string>(){ "ft" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Meters;
|
||||
private const double FeetPerMeter = 3.280839895013123;
|
||||
private const double InchesPerMeter = 12 * FeetPerMeter;
|
||||
private const double MilesPerMeter = FeetPerMeter / 5280;
|
||||
|
||||
[DataMember(Name = "Length", IsRequired = true)]
|
||||
private double _length; // meters
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Length>> _unitInfo = new Dictionary<Unit, UnitInfo<Length>>()
|
||||
{
|
||||
{ Unit.Meters, new UnitInfo<Length>(UnitAbbreviations[Unit.Meters], FromMeters, typeof(Length).GetProperty("Meters").GetGetMethod()) },
|
||||
{ Unit.Centimeters, new UnitInfo<Length>(UnitAbbreviations[Unit.Centimeters], FromCentimeters, typeof(Length).GetProperty("Centimeters").GetGetMethod()) },
|
||||
{ Unit.Millimeters, new UnitInfo<Length>(UnitAbbreviations[Unit.Millimeters], FromMillimeters, typeof(Length).GetProperty("Millimeters").GetGetMethod()) },
|
||||
{ Unit.Micrometers, new UnitInfo<Length>(UnitAbbreviations[Unit.Micrometers], FromMicrometers, typeof(Length).GetProperty("Micrometers").GetGetMethod()) },
|
||||
{ Unit.Nanometers, new UnitInfo<Length>(UnitAbbreviations[Unit.Nanometers], FromNanometers, typeof(Length).GetProperty("Nanometers").GetGetMethod()) },
|
||||
{ Unit.Feet, new UnitInfo<Length>(UnitAbbreviations[Unit.Feet], FromFeet, typeof(Length).GetProperty("Feet").GetGetMethod()) },
|
||||
{ Unit.Inches, new UnitInfo<Length>(UnitAbbreviations[Unit.Inches], FromInches, typeof(Length).GetProperty("Inches").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Length"/> 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 Length()
|
||||
{
|
||||
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="length">The length - meters</param>
|
||||
private Length(double length)
|
||||
{
|
||||
_length = length;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
public double Centimeters
|
||||
{
|
||||
get { return _length * 100; }
|
||||
}
|
||||
|
||||
public double Feet
|
||||
{
|
||||
get { return _length * FeetPerMeter; }
|
||||
}
|
||||
|
||||
public double Inches
|
||||
{
|
||||
get { return _length * InchesPerMeter; }
|
||||
}
|
||||
|
||||
public double Meters
|
||||
{
|
||||
get { return _length; }
|
||||
}
|
||||
|
||||
public double Micrometers
|
||||
{
|
||||
get { return _length * 1000000; }
|
||||
}
|
||||
|
||||
public double Millimeters
|
||||
{
|
||||
get { return _length * 1000; }
|
||||
}
|
||||
|
||||
public double Nanometers
|
||||
{
|
||||
get { return _length * 1000000000; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
public static Length FromCentimeters(double centimeters)
|
||||
{
|
||||
return new Length(centimeters / 100);
|
||||
}
|
||||
|
||||
public static Length FromFeet(double feet)
|
||||
{
|
||||
return new Length(feet / FeetPerMeter);
|
||||
}
|
||||
|
||||
public static Length FromInches(double inches)
|
||||
{
|
||||
return new Length(inches / InchesPerMeter);
|
||||
}
|
||||
|
||||
public static Length FromMeters(double meters)
|
||||
{
|
||||
return new Length(meters);
|
||||
}
|
||||
|
||||
public static Length FromMicrometers(double micrometers)
|
||||
{
|
||||
return new Length(micrometers / 1000000);
|
||||
}
|
||||
|
||||
public static Length FromMillimeters(double millimeters)
|
||||
{
|
||||
return new Length(millimeters / 1000);
|
||||
}
|
||||
|
||||
public static Length FromNanometers(double nanometers)
|
||||
{
|
||||
return new Length(nanometers / 1000000000);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Length l = obj as Length;
|
||||
|
||||
return (l == null) ? false : (this._length == l._length);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _length.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Length operator +(Length left, Length right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Length");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Length");
|
||||
|
||||
return new Length(left._length + right._length);
|
||||
}
|
||||
|
||||
|
||||
public static Length operator -(Length left, Length right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Length");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Length");
|
||||
|
||||
return new Length(left._length - right._length);
|
||||
}
|
||||
|
||||
|
||||
public static Length operator *(Length length, double scalar)
|
||||
{
|
||||
if (null == length)
|
||||
throw new ArgumentNullException("length", "Cannot multiply a null Length");
|
||||
|
||||
return new Length(length._length * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Length operator *(double scalar, Length length)
|
||||
{
|
||||
if (null == length)
|
||||
throw new ArgumentNullException("length", "Cannot multiply a null Length");
|
||||
|
||||
return new Length(scalar * length._length);
|
||||
}
|
||||
|
||||
|
||||
public static Length operator /(Length length, double scalar)
|
||||
{
|
||||
if (null == length)
|
||||
throw new ArgumentNullException("length", "Cannot divide a null Length");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Length by 0");
|
||||
|
||||
return new Length(length._length / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Length left, Length 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._length == right._length;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Length left, Length right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Length left, Length right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Length");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Length");
|
||||
|
||||
return (left._length < right._length);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Length left, Length right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Length");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Length");
|
||||
|
||||
return (left._length > right._length);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Length left, Length right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Length");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Length");
|
||||
|
||||
return (left._length <= right._length);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Length left, Length right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Length");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Length");
|
||||
|
||||
return (left._length >= right._length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with non-length classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator / for Velocity = Length / PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="length">The length.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Velocity
|
||||
/// </returns>
|
||||
|
||||
public static Velocity operator /(Length length, PrecisionTimeSpan time)
|
||||
//public static Velocity operator /(Length length, TimeSpan time)
|
||||
{
|
||||
if (null == length)
|
||||
throw new ArgumentNullException("length", "Cannot divide a null Length");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot divide by a null PrecisionTimeSpan");
|
||||
|
||||
if (0.0 == time.Milliseconds)
|
||||
throw new DivideByZeroException("Cannot divide Length by 0 time");
|
||||
|
||||
return Velocity.FromMetersPerSec(length.Meters / time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator / for PrecisionTimeSpan = Length / Velocity
|
||||
/// </summary>
|
||||
/// <param name="length">The length.</param>
|
||||
/// <param name="time">The velocity.</param>
|
||||
/// <returns>
|
||||
/// PrecisionTimeSpan
|
||||
/// </returns>
|
||||
|
||||
public static PrecisionTimeSpan operator /(Length length, Velocity velocity)
|
||||
{
|
||||
if (null == length)
|
||||
throw new ArgumentNullException("length", "Cannot divide a null Length");
|
||||
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide by a null Velocity");
|
||||
|
||||
if (0.0 == velocity.MetersPerSec)
|
||||
throw new DivideByZeroException("Cannot divide Length by 0 velocity");
|
||||
|
||||
return PrecisionTimeSpan.FromSeconds(length.Meters / velocity.MetersPerSec);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Length Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Length value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Length.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
333
Source/TSRealLib/Common/Raytheon.Common/Units/Power.cs
Normal file
333
Source/TSRealLib/Common/Raytheon.Common/Units/Power.cs
Normal file
@@ -0,0 +1,333 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents energy.
|
||||
///
|
||||
/// 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. FromWatts).
|
||||
///
|
||||
/// Properties (e.g. Watts) are provided to convert the value to other units and return
|
||||
/// as type double.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Power
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Watts,
|
||||
Milliwatts,
|
||||
DecibelMilliWatt
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Watts, new List<string>(){ "W", "Watts", "Watt" } },
|
||||
{Unit.Milliwatts, new List<string>(){ "mW", "MilliWatt", "MilliWatts" } },
|
||||
{Unit.DecibelMilliWatt, new List<string>(){ "dBm", "DecibelMilliWatt", "DecibelMilliWatts" } }
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Watts;
|
||||
|
||||
[DataMember(Name = "Power", IsRequired = true)]
|
||||
private readonly double _power; // watts
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static readonly IDictionary<Unit, UnitInfo<Power>> _unitInfo = new Dictionary<Unit, UnitInfo<Power>>()
|
||||
{
|
||||
{ Unit.Watts, new UnitInfo<Power>(UnitAbbreviations[Unit.Watts], FromWatts, typeof(Power).GetProperty("Watts").GetGetMethod()) },
|
||||
{ Unit.Milliwatts, new UnitInfo<Power>(UnitAbbreviations[Unit.Milliwatts], FromMilliwatts, typeof(Power).GetProperty("Milliwatts").GetGetMethod()) },
|
||||
{ Unit.DecibelMilliWatt, new UnitInfo<Power>(UnitAbbreviations[Unit.DecibelMilliWatt], FromDBM, typeof(Power).GetProperty("DBM").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Power"/> 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 Power()
|
||||
{
|
||||
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="energy">The energy - watts/cm^2.</param>
|
||||
private Power(double energy)
|
||||
{
|
||||
_power = energy;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
public double Milliwatts
|
||||
{
|
||||
get { return _power * 1000; }
|
||||
}
|
||||
|
||||
public double Watts
|
||||
{
|
||||
get { return _power; }
|
||||
}
|
||||
|
||||
public double DBM
|
||||
{
|
||||
get { return (10 * Math.Log10(Math.Abs(_power))) + 30; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns watts/cm^2 as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// watts/cm^2
|
||||
/// </value>
|
||||
|
||||
[Obsolete("WattsPerCmSqrd is deprecated, please use Watts instead. This will be removed in a future version.", false)]
|
||||
public double WattsPerCmSqrd
|
||||
{
|
||||
get
|
||||
{
|
||||
return Double.NaN;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// </summary>
|
||||
/// <param name="wattsPerCmSqrd">watts/cm^2</param>
|
||||
/// <returns></returns>
|
||||
|
||||
[Obsolete("FromWattsPerCmSqrd is deprecated, please use FromWatts instead. This will be removed in a future version.", false)]
|
||||
public static Power FromWattsPerCmSqrd(double wattsPerCmSqrd)
|
||||
{
|
||||
return new Power(Double.NaN);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Power object interpreting passed value as milliwatts
|
||||
/// </summary>
|
||||
/// <param name="milliwatts">milliwatts</param>
|
||||
/// <returns></returns>
|
||||
public static Power FromMilliwatts(double milliwatts)
|
||||
{
|
||||
return new Power(milliwatts / 1000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Power object interpreting passed value as watts
|
||||
/// </summary>
|
||||
/// <param name="watts">watts</param>
|
||||
/// <returns></returns>
|
||||
public static Power FromWatts(double watts)
|
||||
{
|
||||
return new Power(watts);
|
||||
}
|
||||
|
||||
public static Power FromDBM(double dbm)
|
||||
{
|
||||
return new Power(Math.Pow(10, (dbm - 30) / 10));
|
||||
}
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Power e = obj as Power;
|
||||
|
||||
return (e == null) ? false : (this._power == e._power);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _power.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
public static Power operator +(Power left, Power right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Power");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Power");
|
||||
|
||||
return new Power(left._power + right._power);
|
||||
}
|
||||
|
||||
public static Power operator -(Power left, Power right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Power");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Power");
|
||||
|
||||
return new Power(left._power - right._power);
|
||||
}
|
||||
|
||||
public static Power operator *(Power power, double scalar)
|
||||
{
|
||||
if (null == power)
|
||||
throw new ArgumentNullException("power", "Cannot multiply a null Power");
|
||||
|
||||
return new Power(power._power * scalar);
|
||||
}
|
||||
|
||||
public static Power operator *(double scalar, Power power)
|
||||
{
|
||||
if (null == power)
|
||||
throw new ArgumentNullException("power", "Cannot multiply a null Power");
|
||||
|
||||
return new Power(scalar * power._power);
|
||||
}
|
||||
|
||||
public static Power operator /(Power power, double scalar)
|
||||
{
|
||||
if (null == power)
|
||||
throw new ArgumentNullException("power", "Cannot divide a null Power");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Power by 0");
|
||||
|
||||
return new Power(power._power / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Power left, Power 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._power == right._power;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Power left, Power right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static bool operator <(Power left, Power right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Power");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Power");
|
||||
|
||||
return (left._power < right._power);
|
||||
}
|
||||
|
||||
public static bool operator >(Power left, Power right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Power");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Power");
|
||||
|
||||
return (left._power > right._power);
|
||||
}
|
||||
|
||||
public static bool operator <=(Power left, Power right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Power");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Power");
|
||||
|
||||
return (left._power <= right._power);
|
||||
}
|
||||
|
||||
public static bool operator >=(Power left, Power right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Power");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Power");
|
||||
|
||||
return (left._power >= right._power);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
// no operators right now
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Power Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Power value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Power.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents precision time spans where the .NET TimeSpan class just does
|
||||
/// not cut the mustard
|
||||
///
|
||||
/// 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. FromSeconds).
|
||||
///
|
||||
/// Properties (e.g. Seconds) 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
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class PrecisionTimeSpan
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Seconds,
|
||||
Milliseconds,
|
||||
Microseconds,
|
||||
Nanoseconds
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Seconds, new List<string>(){ "sec" } },
|
||||
{Unit.Milliseconds, new List<string>(){ "msec" } },
|
||||
{Unit.Microseconds, new List<string>(){ "usec" } },
|
||||
{Unit.Nanoseconds, new List<string>(){ "nsec" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Seconds;
|
||||
|
||||
[DataMember(Name = "BaseTimeSpan", IsRequired = true)]
|
||||
private Decimal _totalns; //base will be nano seconds... max will be double.MaxValue
|
||||
//min will be 0
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<PrecisionTimeSpan>> _unitInfo = new Dictionary<Unit, UnitInfo<PrecisionTimeSpan>>()
|
||||
{
|
||||
{ Unit.Seconds, new UnitInfo<PrecisionTimeSpan>(UnitAbbreviations[Unit.Seconds], FromSeconds, typeof(PrecisionTimeSpan).GetProperty("Seconds").GetGetMethod()) },
|
||||
{ Unit.Milliseconds, new UnitInfo<PrecisionTimeSpan>(UnitAbbreviations[Unit.Milliseconds], FromMilliseconds, typeof(PrecisionTimeSpan).GetProperty("Milliseconds").GetGetMethod()) },
|
||||
{ Unit.Microseconds, new UnitInfo<PrecisionTimeSpan>(UnitAbbreviations[Unit.Microseconds], FromMicroseconds, typeof(PrecisionTimeSpan).GetProperty("Microseconds").GetGetMethod()) },
|
||||
{ Unit.Nanoseconds, new UnitInfo<PrecisionTimeSpan>(UnitAbbreviations[Unit.Nanoseconds], FromNanoseconds, typeof(PrecisionTimeSpan).GetProperty("Nanoseconds").GetGetMethod()) }
|
||||
};
|
||||
|
||||
internal const Decimal _nanoPerMicro = 1e3M;
|
||||
internal const Decimal _nanoPerMilli = 1e6M;
|
||||
internal const Decimal _nanoPerSec = 1e9M;
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="PrecisionTimeSpan"/> 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 PrecisionTimeSpan()
|
||||
{
|
||||
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="totalns">total number of nanoseconds.</param>
|
||||
private PrecisionTimeSpan(Decimal totalns)
|
||||
{
|
||||
if (totalns < 0.0M)
|
||||
throw new ArgumentOutOfRangeException("totalns", "Cannot create a negative timespan");
|
||||
|
||||
_totalns = totalns;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
public double Nanoseconds
|
||||
{
|
||||
get { return (double)_totalns; }
|
||||
}
|
||||
|
||||
public double Microseconds
|
||||
{
|
||||
get { return (double)(_totalns / _nanoPerMicro); }
|
||||
}
|
||||
|
||||
public double Milliseconds
|
||||
{
|
||||
get { return (double)(_totalns / _nanoPerMilli); }
|
||||
}
|
||||
|
||||
public double Seconds
|
||||
{
|
||||
get { return (double)(_totalns / _nanoPerSec); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns PrecisionTimeSpan object interpreting passed value as nanoseconds
|
||||
/// </summary>
|
||||
/// <param name="nanoseconds">total nanoseconds in the desired timespan</param>
|
||||
/// <returns>the precision time span object</returns>
|
||||
public static PrecisionTimeSpan FromNanoseconds(double nanoseconds)
|
||||
{
|
||||
return new PrecisionTimeSpan((Decimal)nanoseconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns PrecisionTimeSpan object interpreting passed value as microseconds
|
||||
/// </summary>
|
||||
/// <param name="nanoseconds">total microseconds in the desired timespan</param>
|
||||
/// <returns>the precision time span object</returns>
|
||||
public static PrecisionTimeSpan FromMicroseconds(double microseconds)
|
||||
{
|
||||
return new PrecisionTimeSpan((Decimal)microseconds * _nanoPerMicro);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns PrecisionTimeSpan object interpreting passed value as milliseconds
|
||||
/// </summary>
|
||||
/// <param name="nanoseconds">total milliseconds in the desired timespan</param>
|
||||
/// <returns>the precision time span object</returns>
|
||||
public static PrecisionTimeSpan FromMilliseconds(double milliseconds)
|
||||
{
|
||||
return new PrecisionTimeSpan((Decimal)milliseconds * _nanoPerMilli);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns PrecisionTimeSpan object interpreting passed value as seconds
|
||||
/// </summary>
|
||||
/// <param name="nanoseconds">total seconds in the desired timespan</param>
|
||||
/// <returns>the precision time span object</returns>
|
||||
public static PrecisionTimeSpan FromSeconds(double seconds)
|
||||
{
|
||||
return new PrecisionTimeSpan((Decimal)seconds * _nanoPerSec);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
PrecisionTimeSpan ts = obj as PrecisionTimeSpan;
|
||||
|
||||
return (ts == null) ? false : (this._totalns == ts._totalns);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _totalns.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static PrecisionTimeSpan operator +(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null PrecisionTimeSpan");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null PrecisionTimeSpan");
|
||||
|
||||
return new PrecisionTimeSpan(left._totalns + right._totalns);
|
||||
}
|
||||
|
||||
|
||||
public static PrecisionTimeSpan operator -(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null PrecisionTimeSpan");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null PrecisionTimeSpan");
|
||||
|
||||
if (right > left)
|
||||
throw new ArgumentOutOfRangeException("right", "Cannot subtract a larger span from a smaller one");
|
||||
|
||||
return new PrecisionTimeSpan(left._totalns - right._totalns);
|
||||
}
|
||||
|
||||
|
||||
public static PrecisionTimeSpan operator *(PrecisionTimeSpan precisionTimeSpan, double scalar)
|
||||
{
|
||||
if (null == precisionTimeSpan)
|
||||
throw new ArgumentNullException("precisionTimeSpan", "Cannot multiply a null PrecisionTimeSpan");
|
||||
|
||||
return new PrecisionTimeSpan(precisionTimeSpan._totalns * (Decimal)scalar);
|
||||
}
|
||||
|
||||
|
||||
public static PrecisionTimeSpan operator *(double scalar, PrecisionTimeSpan precisionTimeSpan)
|
||||
{
|
||||
if (null == precisionTimeSpan)
|
||||
throw new ArgumentNullException("precisionTimeSpan", "Cannot multiply a null PrecisionTimeSpan");
|
||||
|
||||
return new PrecisionTimeSpan((Decimal)scalar * precisionTimeSpan._totalns);
|
||||
}
|
||||
|
||||
|
||||
public static PrecisionTimeSpan operator /(PrecisionTimeSpan precisionTimeSpan, double scalar)
|
||||
{
|
||||
if (null == precisionTimeSpan)
|
||||
throw new ArgumentNullException("precisionTimeSpan", "Cannot divide a null PrecisionTimeSpan");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Precision Time Span by 0");
|
||||
|
||||
return new PrecisionTimeSpan(precisionTimeSpan._totalns / (Decimal)scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(PrecisionTimeSpan left, PrecisionTimeSpan 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._totalns == right._totalns;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
return (left._totalns < right._totalns);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
return (left._totalns > right._totalns);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
return (left._totalns <= right._totalns);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(PrecisionTimeSpan left, PrecisionTimeSpan right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null PrecisionTimeSpan");
|
||||
|
||||
return (left._totalns >= right._totalns);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
// no operators right now
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static PrecisionTimeSpan Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out PrecisionTimeSpan value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = PrecisionTimeSpan.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
367
Source/TSRealLib/Common/Raytheon.Common/Units/Pressure.cs
Normal file
367
Source/TSRealLib/Common/Raytheon.Common/Units/Pressure.cs
Normal file
@@ -0,0 +1,367 @@
|
||||
//############################################################################//
|
||||
// RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
|
||||
// PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
|
||||
// AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
|
||||
// UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
|
||||
// RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
|
||||
// CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
|
||||
// OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
|
||||
// COMPANY.
|
||||
//
|
||||
// THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. GOVERNMENT
|
||||
//
|
||||
// UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
|
||||
//
|
||||
// WARNING: THIS DOCUMENT CONTAINS TECHNICAL DATA AND / OR TECHNOLOGY WHOSE
|
||||
// EXPORT OR DISCLOSURE TO NON-U.S. PERSONS, WHEREVER LOCATED, IS RESTRICTED
|
||||
// BY THE INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) (22 C.F.R. SECTION
|
||||
// 120-130) OR THE EXPORT ADMINISTRATION REGULATIONS (EAR) (15 C.F.R. SECTION
|
||||
// 730-774). THIS DOCUMENT CANNOT BE EXPORTED (E.G., PROVIDED TO A SUPPLIER
|
||||
// OUTSIDE OF THE UNITED STATES) OR DISCLOSED TO A NON-U.S. PERSON, WHEREVER
|
||||
// LOCATED, UNTIL A FINAL JURISDICTION AND CLASSIFICATION DETERMINATION HAS
|
||||
// BEEN COMPLETED AND APPROVED BY RAYTHEON, AND ANY REQUIRED U.S. GOVERNMENT
|
||||
// APPROVALS HAVE BEEN OBTAINED. VIOLATIONS ARE SUBJECT TO SEVERE CRIMINAL
|
||||
// PENALTIES.
|
||||
//
|
||||
// DOD 5220.22-M, INDUSTRIAL SECURITY MANUAL, CHAPTER 5, SECTION 1 THROUGH 9 :
|
||||
// FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN OR DOD 5200.1-R,
|
||||
// INFORMATION SECURITY PROGRAM, CHAPTER 6. FOR UNCLASSIFIED, LIMITED DOCUMENTS
|
||||
// DESTROY BY ANY METHOD THAT WILL PREVENT DISCLOSURE OF CONTENTS OR
|
||||
// RECONSTRUCTION OF THE DOCUMENT.
|
||||
//############################################################################//
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a Pressure
|
||||
///
|
||||
/// 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. Pascal).
|
||||
///
|
||||
/// Properties (e.g. Pascal) are provided to convert the value to other units and return
|
||||
/// as type double.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Pressure
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Bar,
|
||||
Millibar,
|
||||
Pascal,
|
||||
PSI
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Bar, new List<string>(){ "bar" } },
|
||||
{Unit.Millibar, new List<string>(){ "mbar" } },
|
||||
{Unit.Pascal, new List<string>(){ "Pa" } },
|
||||
{Unit.PSI, new List<string>(){ "PSI" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.PSI;
|
||||
private const int PascalPerBar = 100000;
|
||||
private const double PascalPerPSI = 6894.75729;
|
||||
|
||||
[DataMember(Name = "Pascal", IsRequired = true)]
|
||||
private double _pressure; // Pascal
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Pressure>> _unitInfo = new Dictionary<Unit, UnitInfo<Pressure>>()
|
||||
{
|
||||
{ Unit.Bar, new UnitInfo<Pressure>(UnitAbbreviations[Unit.Bar], FromBar, typeof(Pressure).GetProperty("Bar").GetGetMethod()) },
|
||||
{ Unit.Millibar, new UnitInfo<Pressure>(UnitAbbreviations[Unit.Millibar], FromMillibar, typeof(Pressure).GetProperty("Millibar").GetGetMethod()) },
|
||||
{ Unit.Pascal, new UnitInfo<Pressure>(UnitAbbreviations[Unit.Pascal], FromPascal, typeof(Pressure).GetProperty("Pascal").GetGetMethod()) },
|
||||
{ Unit.PSI, new UnitInfo<Pressure>(UnitAbbreviations[Unit.PSI], FromPSI, typeof(Pressure).GetProperty("PSI").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Pressure"/> class from being created
|
||||
/// Use FromXXX methods to create objects of this type.
|
||||
/// </summary>
|
||||
private Pressure()
|
||||
{
|
||||
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="pressure">The pressure - pascal</param>
|
||||
private Pressure(double pressure)
|
||||
{
|
||||
this._pressure = pressure;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
public double Bar { get { return _pressure / PascalPerBar; } }
|
||||
|
||||
public double Millibar { get { return _pressure / PascalPerBar * 1000; } }
|
||||
|
||||
public double Pascal { get { return _pressure; } }
|
||||
|
||||
public double PSI { get { return _pressure / PascalPerPSI; } }
|
||||
#endregion
|
||||
|
||||
#region Creation Methods
|
||||
public static Pressure FromBar(double bars)
|
||||
{
|
||||
return new Pressure(bars * PascalPerBar);
|
||||
}
|
||||
|
||||
public static Pressure FromMillibar(double millibars)
|
||||
{
|
||||
return new Pressure(millibars * PascalPerBar / 1000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Pressure object interpreting passed value as Pascal
|
||||
/// </summary>
|
||||
/// <param name="Pressure">Pascal</param>
|
||||
/// <returns></returns>
|
||||
public static Pressure FromPascal(double pascal)
|
||||
{
|
||||
return new Pressure(pascal);
|
||||
}
|
||||
|
||||
public static Pressure FromPSI(double psi)
|
||||
{
|
||||
return new Pressure(psi * PascalPerPSI);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Pressure pressure = obj as Pressure;
|
||||
|
||||
return (pressure == null) ? false : (this._pressure == pressure._pressure);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _pressure.GetHashCode();
|
||||
}
|
||||
|
||||
#region Binary Operators
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Pressure operator +(Pressure left, Pressure right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot add a null Pressure");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot add null Pressure");
|
||||
}
|
||||
|
||||
return new Pressure(left._pressure + right._pressure);
|
||||
}
|
||||
|
||||
|
||||
public static Pressure operator -(Pressure left, Pressure right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot add a null Pressure");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot add null Pressure");
|
||||
}
|
||||
|
||||
return new Pressure(left._pressure - right._pressure);
|
||||
}
|
||||
|
||||
|
||||
public static Pressure operator *(Pressure pressure, double scalar)
|
||||
{
|
||||
if (null == pressure)
|
||||
{
|
||||
throw new ArgumentNullException("pressure", "Cannot multiply a null Pressure");
|
||||
}
|
||||
|
||||
return new Pressure(pressure._pressure * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Pressure operator *(double scalar, Pressure pressure)
|
||||
{
|
||||
if (null == pressure)
|
||||
{
|
||||
throw new ArgumentNullException("pressure", "Cannot multiply a null Pressure");
|
||||
}
|
||||
|
||||
return new Pressure(scalar * pressure._pressure);
|
||||
}
|
||||
|
||||
|
||||
public static Pressure operator /(Pressure pressure, double scalar)
|
||||
{
|
||||
if (null == pressure)
|
||||
{
|
||||
throw new ArgumentNullException("pressure", "Cannot divide a null Pressure");
|
||||
}
|
||||
|
||||
if (0.0 == scalar)
|
||||
{
|
||||
throw new DivideByZeroException("Cannot divide Pressure by 0");
|
||||
}
|
||||
|
||||
return new Pressure(pressure._pressure / scalar);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
public static bool operator ==(Pressure left, Pressure 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._pressure == right._pressure;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Pressure left, Pressure right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Pressure left, Pressure right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
return (left._pressure < right._pressure);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Pressure left, Pressure right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
return (left._pressure > right._pressure);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Pressure left, Pressure right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
return (left._pressure <= right._pressure);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Pressure left, Pressure right)
|
||||
{
|
||||
if (null == left)
|
||||
{
|
||||
throw new ArgumentNullException("left", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
if (null == right)
|
||||
{
|
||||
throw new ArgumentNullException("right", "Cannot compare null Pressure");
|
||||
}
|
||||
|
||||
return (left._pressure >= right._pressure);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Pressure Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Pressure value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Pressure.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
369
Source/TSRealLib/Common/Raytheon.Common/Units/Resistance.cs
Normal file
369
Source/TSRealLib/Common/Raytheon.Common/Units/Resistance.cs
Normal file
@@ -0,0 +1,369 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents electrical resistance.
|
||||
///
|
||||
/// 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. FromOhms).
|
||||
///
|
||||
/// Properties (e.g. KiloOhms) 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.
|
||||
/// Current, Voltage).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Resistance
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Ohms,
|
||||
KiloOhms,
|
||||
MegaOhms
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Ohms, new List<string>(){ "Ohm" } },
|
||||
{Unit.KiloOhms, new List<string>(){ "kOhm" } },
|
||||
{Unit.MegaOhms, new List<string>(){ "MOhm" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Ohms;
|
||||
private const int KiloOhmsPerOhm = 1000;
|
||||
private const int MegaOhmsPerOhm = 1000000;
|
||||
|
||||
[DataMember(Name = "Resistance", IsRequired = true)]
|
||||
private double _resistance; // ohms
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Resistance>> _unitInfo = new Dictionary<Unit, UnitInfo<Resistance>>()
|
||||
{
|
||||
{ Unit.Ohms, new UnitInfo<Resistance>(UnitAbbreviations[Unit.Ohms], FromOhms, typeof(Resistance).GetProperty("Ohms").GetGetMethod()) },
|
||||
{ Unit.KiloOhms, new UnitInfo<Resistance>(UnitAbbreviations[Unit.KiloOhms], FromKiloOhms, typeof(Resistance).GetProperty("KiloOhms").GetGetMethod()) },
|
||||
{ Unit.MegaOhms, new UnitInfo<Resistance>(UnitAbbreviations[Unit.MegaOhms], FromMegaOhms, typeof(Resistance).GetProperty("MegaOhms").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Resistance"/> 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 Resistance()
|
||||
{
|
||||
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="resistance">The resistance - ohms</param>
|
||||
private Resistance(double resistance)
|
||||
{
|
||||
_resistance = resistance;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns ohms as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// ohms
|
||||
/// </value>
|
||||
public double Ohms
|
||||
{
|
||||
get { return _resistance; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns kiloohms as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// kiloohms
|
||||
/// </value>
|
||||
public double KiloOhms
|
||||
{
|
||||
get { return _resistance / KiloOhmsPerOhm; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns megaohms as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// megaohms
|
||||
/// </value>
|
||||
public double MegaOhms
|
||||
{
|
||||
get { return _resistance / MegaOhmsPerOhm; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Resistance object interpreting passed value as kiloohms
|
||||
/// </summary>
|
||||
/// <param name="kiloohms">kiloohms</param>
|
||||
/// <returns></returns>
|
||||
public static Resistance FromKiloOhms(double kiloOhms)
|
||||
{
|
||||
return new Resistance(kiloOhms * KiloOhmsPerOhm);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Resistance object interpreting passed value as megaohms
|
||||
/// </summary>
|
||||
/// <param name="megaohms">megaohms</param>
|
||||
/// <returns></returns>
|
||||
public static Resistance FromMegaOhms(double megaOhms)
|
||||
{
|
||||
return new Resistance(megaOhms * MegaOhmsPerOhm);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Resistance object interpreting passed value as ohms
|
||||
/// </summary>
|
||||
/// <param name="ohms">ohms</param>
|
||||
/// <returns></returns>
|
||||
public static Resistance FromOhms(double ohms)
|
||||
{
|
||||
return new Resistance(ohms);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Resistance r = obj as Resistance;
|
||||
|
||||
return (r == null) ? false : (this._resistance == r._resistance);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _resistance.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Resistance operator +(Resistance left, Resistance right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Resistance");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Resistance");
|
||||
|
||||
return new Resistance(left._resistance + right._resistance);
|
||||
}
|
||||
|
||||
|
||||
public static Resistance operator -(Resistance left, Resistance right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Resistance");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Resistance");
|
||||
|
||||
return new Resistance(left._resistance - right._resistance);
|
||||
}
|
||||
|
||||
|
||||
public static Resistance operator *(Resistance resistance, double scalar)
|
||||
{
|
||||
if (null == resistance)
|
||||
throw new ArgumentNullException("resistance", "Cannot multiply a null Resistance");
|
||||
|
||||
return new Resistance(resistance._resistance * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Resistance operator *(double scalar, Resistance resistance)
|
||||
{
|
||||
if (null == resistance)
|
||||
throw new ArgumentNullException("resistance", "Cannot multiply a null Resistance");
|
||||
|
||||
return new Resistance(scalar * resistance._resistance);
|
||||
}
|
||||
|
||||
|
||||
public static Resistance operator /(Resistance resistance, double scalar)
|
||||
{
|
||||
if (null == resistance)
|
||||
throw new ArgumentNullException("resistance", "Cannot divide a null Resistance");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Resistance by 0");
|
||||
|
||||
return new Resistance(resistance._resistance / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Resistance left, Resistance 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._resistance == right._resistance;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Resistance left, Resistance right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Resistance left, Resistance right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Resistance");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Resistance");
|
||||
|
||||
return (left._resistance < right._resistance);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Resistance left, Resistance right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Resistance");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Resistance");
|
||||
|
||||
return (left._resistance > right._resistance);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Resistance left, Resistance right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Resistance");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Resistance");
|
||||
|
||||
return (left._resistance <= right._resistance);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Resistance left, Resistance right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Resistance");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Resistance");
|
||||
|
||||
return (left._resistance >= right._resistance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with non-resistance classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Voltage = Resistance / Current
|
||||
/// </summary>
|
||||
/// <param name="resistance">The resistance.</param>
|
||||
/// <param name="current">The current.</param>
|
||||
/// <returns>
|
||||
/// Voltage
|
||||
/// </returns>
|
||||
|
||||
public static Voltage operator *(Resistance resistance, Current current)
|
||||
{
|
||||
if (null == resistance)
|
||||
throw new ArgumentNullException("resistance", "Cannot divide a null Resistance");
|
||||
|
||||
if (null == current)
|
||||
throw new ArgumentNullException("current", "Cannot divide by a null Current");
|
||||
|
||||
if (0.0 == current.Amps)
|
||||
throw new DivideByZeroException("Cannot divide Resistance by 0 Current");
|
||||
|
||||
return Voltage.FromVolts(resistance.Ohms * current.Amps);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Resistance Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Resistance value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Resistance.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
325
Source/TSRealLib/Common/Raytheon.Common/Units/Temperature.cs
Normal file
325
Source/TSRealLib/Common/Raytheon.Common/Units/Temperature.cs
Normal file
@@ -0,0 +1,325 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Temperature
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
DegreesC,
|
||||
DegreesF,
|
||||
DegreesK
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.DegreesC, new List<string>(){ "C" } },
|
||||
{Unit.DegreesF, new List<string>(){ "F" } },
|
||||
{Unit.DegreesK, new List<string>(){ "K" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.DegreesC;
|
||||
private const double CdegreesPerFdegree = 5.0 / 9.0;
|
||||
private const double CtoF_Offset = 32.0;
|
||||
private const double KtoC_Offset = 273.15;
|
||||
|
||||
[DataMember(Name = "Temperature", IsRequired = true)]
|
||||
private double _temperature; // degrees C
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Temperature>> _unitInfo = new Dictionary<Unit, UnitInfo<Temperature>>()
|
||||
{
|
||||
{ Unit.DegreesC, new UnitInfo<Temperature>(UnitAbbreviations[Unit.DegreesC], FromDegreesC, typeof(Temperature).GetProperty("DegreesC").GetGetMethod()) },
|
||||
{ Unit.DegreesF, new UnitInfo<Temperature>(UnitAbbreviations[Unit.DegreesF], FromDegreesF, typeof(Temperature).GetProperty("DegreesF").GetGetMethod()) },
|
||||
{ Unit.DegreesK, new UnitInfo<Temperature>(UnitAbbreviations[Unit.DegreesK], FromDegreesK, typeof(Temperature).GetProperty("DegreesK").GetGetMethod()) }
|
||||
};
|
||||
|
||||
private Temperature()
|
||||
{
|
||||
throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object");
|
||||
}
|
||||
|
||||
private Temperature(double temperature)
|
||||
{
|
||||
_temperature = temperature;
|
||||
}
|
||||
|
||||
#region output properties
|
||||
|
||||
public double DegreesC
|
||||
{
|
||||
get
|
||||
{
|
||||
return _temperature;
|
||||
}
|
||||
}
|
||||
|
||||
public double DegreesF
|
||||
{
|
||||
get
|
||||
{
|
||||
return (_temperature / CdegreesPerFdegree) + CtoF_Offset;
|
||||
}
|
||||
}
|
||||
|
||||
public double DegreesK
|
||||
{
|
||||
get
|
||||
{
|
||||
return DegreesC + KtoC_Offset;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
public static Temperature FromDegreesC(double temperature)
|
||||
{
|
||||
return new Temperature(temperature);
|
||||
}
|
||||
|
||||
public static Temperature FromDegreesF(double temperature)
|
||||
{
|
||||
return new Temperature((temperature - CtoF_Offset) * CdegreesPerFdegree);
|
||||
}
|
||||
|
||||
public static Temperature FromDegreesK(double temperature)
|
||||
{
|
||||
return FromDegreesC(temperature - KtoC_Offset);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region add delta methods
|
||||
|
||||
public void Add(TemperatureDelta delta)
|
||||
{
|
||||
if (delta == null)
|
||||
{
|
||||
throw new ArgumentNullException("delta");
|
||||
}
|
||||
|
||||
_temperature += delta.DegreesC;
|
||||
}
|
||||
|
||||
public void AddDegreesC(double delta)
|
||||
{
|
||||
_temperature += TemperatureDelta.FromDegreesC(delta).DegreesC;
|
||||
}
|
||||
|
||||
public void AddDegreesF(double delta)
|
||||
{
|
||||
_temperature += TemperatureDelta.FromDegreesF(delta).DegreesC;
|
||||
}
|
||||
|
||||
public void AddDegreesK(double delta)
|
||||
{
|
||||
_temperature += TemperatureDelta.FromDegreesK(delta).DegreesC;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region binary operators
|
||||
|
||||
|
||||
public static Temperature operator +(Temperature left, TemperatureDelta right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null TemperatureDelta");
|
||||
|
||||
return new Temperature(left.DegreesC + right.DegreesC);
|
||||
}
|
||||
|
||||
|
||||
public static Temperature operator +(TemperatureDelta left, Temperature right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null TemperatureDelta");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Temperature");
|
||||
|
||||
return new Temperature(left.DegreesC + right.DegreesC);
|
||||
}
|
||||
|
||||
|
||||
public static TemperatureDelta operator -(Temperature left, Temperature right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Temperature");
|
||||
|
||||
return TemperatureDelta.FromDegreesC(left.DegreesC - right.DegreesC);
|
||||
}
|
||||
|
||||
|
||||
public static Temperature operator -(Temperature left, TemperatureDelta right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null TemperatureDelta");
|
||||
|
||||
return Temperature.FromDegreesC(left.DegreesC - right.DegreesC);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Temperature left, Temperature 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._temperature == right._temperature;
|
||||
}
|
||||
}
|
||||
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Temperature left, Temperature right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Temperature left, Temperature right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Temperature");
|
||||
|
||||
return (left._temperature < right._temperature);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Temperature left, Temperature right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Temperature");
|
||||
|
||||
return (left._temperature > right._temperature);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Temperature left, Temperature right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Temperature");
|
||||
|
||||
return (left._temperature <= right._temperature);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Temperature left, Temperature right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Temperature");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Temperature");
|
||||
|
||||
return (left._temperature >= right._temperature);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Temperature Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Temperature value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Temperature.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Object overrides
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Temperature t = obj as Temperature;
|
||||
|
||||
return (t == null) ? false : (this._temperature == t._temperature);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _temperature.GetHashCode();
|
||||
}
|
||||
|
||||
/// <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]}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class TemperatureDelta
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
DegreesC,
|
||||
DegreesF,
|
||||
DegreesK
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.DegreesC, new List<string>(){ "C" } },
|
||||
{Unit.DegreesF, new List<string>(){ "F" } },
|
||||
{Unit.DegreesK, new List<string>(){ "K" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.DegreesC;
|
||||
private const double CdegreesPerFdegree = 5.0 / 9.0;
|
||||
|
||||
[DataMember(Name = "Delta", IsRequired = true)]
|
||||
private double _delta; // degrees C or K
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<TemperatureDelta>> _unitInfo = new Dictionary<Unit, UnitInfo<TemperatureDelta>>()
|
||||
{
|
||||
{ Unit.DegreesC, new UnitInfo<TemperatureDelta>(UnitAbbreviations[Unit.DegreesC], FromDegreesC, typeof(TemperatureDelta).GetProperty("DegreesC").GetGetMethod()) },
|
||||
{ Unit.DegreesF, new UnitInfo<TemperatureDelta>(UnitAbbreviations[Unit.DegreesF], FromDegreesF, typeof(TemperatureDelta).GetProperty("DegreesF").GetGetMethod()) },
|
||||
{ Unit.DegreesK, new UnitInfo<TemperatureDelta>(UnitAbbreviations[Unit.DegreesK], FromDegreesK, typeof(TemperatureDelta).GetProperty("DegreesK").GetGetMethod()) }
|
||||
};
|
||||
|
||||
private TemperatureDelta()
|
||||
{
|
||||
throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object");
|
||||
}
|
||||
|
||||
private TemperatureDelta(double delta)
|
||||
{
|
||||
_delta = delta;
|
||||
}
|
||||
|
||||
#region output properties
|
||||
|
||||
public double DegreesC
|
||||
{
|
||||
get
|
||||
{
|
||||
return _delta;
|
||||
}
|
||||
}
|
||||
|
||||
public double DegreesF
|
||||
{
|
||||
get
|
||||
{
|
||||
return _delta / CdegreesPerFdegree;
|
||||
}
|
||||
}
|
||||
|
||||
public double DegreesK
|
||||
{
|
||||
get
|
||||
{
|
||||
return _delta;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
public static TemperatureDelta FromDegreesC(double temperature)
|
||||
{
|
||||
return new TemperatureDelta(temperature);
|
||||
}
|
||||
|
||||
public static TemperatureDelta FromDegreesF(double temperature)
|
||||
{
|
||||
return new TemperatureDelta(temperature * CdegreesPerFdegree);
|
||||
}
|
||||
|
||||
public static TemperatureDelta FromDegreesK(double temperature)
|
||||
{
|
||||
return new TemperatureDelta(temperature);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// binary operators are in Temperature class
|
||||
|
||||
#region comparison operators
|
||||
|
||||
|
||||
public static bool operator <(TemperatureDelta left, TemperatureDelta right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null TemperatureDelta");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null TemperatureDelta");
|
||||
|
||||
return (left._delta < right._delta);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(TemperatureDelta left, TemperatureDelta right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null TemperatureDelta");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null TemperatureDelta");
|
||||
|
||||
return (left._delta > right._delta);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(TemperatureDelta left, TemperatureDelta right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null TemperatureDelta");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null TemperatureDelta");
|
||||
|
||||
return (left._delta <= right._delta);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(TemperatureDelta left, TemperatureDelta right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null TemperatureDelta");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null TemperatureDelta");
|
||||
|
||||
return (left._delta >= right._delta);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static TemperatureDelta Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out TemperatureDelta value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = TemperatureDelta.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Object overrides
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
TemperatureDelta d = obj as TemperatureDelta;
|
||||
|
||||
return (d == null) ? false : (this._delta == d._delta);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _delta.GetHashCode();
|
||||
}
|
||||
|
||||
/// <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]}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
409
Source/TSRealLib/Common/Raytheon.Common/Units/Velocity.cs
Normal file
409
Source/TSRealLib/Common/Raytheon.Common/Units/Velocity.cs
Normal file
@@ -0,0 +1,409 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a velocity.
|
||||
///
|
||||
/// 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. FromMetersPerSec).
|
||||
///
|
||||
/// Properties (e.g. FeetPerSec) 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.
|
||||
/// Acceleration, Length).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Velocity
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
FeetPerSecond,
|
||||
MetersPerSecond,
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.FeetPerSecond, new List<string>(){ "ft/sec" } },
|
||||
{Unit.MetersPerSecond, new List<string>(){ "m/sec" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.MetersPerSecond;
|
||||
private const double FeetPerMeter = 3.280839895013123;
|
||||
|
||||
[DataMember(Name = "Velocity", IsRequired = true)]
|
||||
private double _velocity; // meters/second
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Velocity>> _unitInfo = new Dictionary<Unit, UnitInfo<Velocity>>()
|
||||
{
|
||||
{ Unit.FeetPerSecond, new UnitInfo<Velocity>(UnitAbbreviations[Unit.FeetPerSecond], FromFeetPerSec, typeof(Velocity).GetProperty("FeetPerSec").GetGetMethod()) },
|
||||
{ Unit.MetersPerSecond, new UnitInfo<Velocity>(UnitAbbreviations[Unit.MetersPerSecond], FromMetersPerSec, typeof(Velocity).GetProperty("MetersPerSec").GetGetMethod()) },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Velocity"/> 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 Velocity()
|
||||
{
|
||||
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="velocity">The velocity - m/sec.</param>
|
||||
private Velocity(double velocity)
|
||||
{
|
||||
_velocity = velocity;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
/// <summary>
|
||||
/// Returns feet/sec as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// feet/sec
|
||||
/// </value>
|
||||
public double FeetPerSec
|
||||
{
|
||||
get { return _velocity * FeetPerMeter; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns meters/sec as type double
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// meters/sec
|
||||
/// </value>
|
||||
public double MetersPerSec
|
||||
{
|
||||
get { return _velocity; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Velocity object interpreting passed value as ft/sec
|
||||
/// </summary>
|
||||
/// <param name="feetPerSec">ft/sec</param>
|
||||
/// <returns></returns>
|
||||
public static Velocity FromFeetPerSec(double feetPerSec)
|
||||
{
|
||||
return new Velocity(feetPerSec / FeetPerMeter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Velocity object interpreting passed value as meters/sec
|
||||
/// </summary>
|
||||
/// <param name="metersPerSec">meters/sec</param>
|
||||
/// <returns></returns>
|
||||
public static Velocity FromMetersPerSec(double metersPerSec)
|
||||
{
|
||||
return new Velocity(metersPerSec);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Velocity v = obj as Velocity;
|
||||
|
||||
return (v == null) ? false : (this._velocity == v._velocity);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _velocity.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Velocity operator +(Velocity left, Velocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Velocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Velocity");
|
||||
|
||||
return new Velocity(left._velocity + right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static Velocity operator -(Velocity left, Velocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Velocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Velocity");
|
||||
|
||||
return new Velocity(left._velocity - right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static Velocity operator *(Velocity velocity, double scalar)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply a null Velocity");
|
||||
|
||||
return new Velocity(velocity._velocity * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Velocity operator *(double scalar, Velocity velocity)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply a null Velocity");
|
||||
|
||||
return new Velocity(scalar * velocity._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static Velocity operator /(Velocity velocity, double scalar)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide a null Velocity");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Velocity by 0");
|
||||
|
||||
return new Velocity(velocity._velocity / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Velocity left, Velocity 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._velocity == right._velocity;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Velocity left, Velocity right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Velocity left, Velocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Velocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Velocity");
|
||||
|
||||
return (left._velocity < right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Velocity left, Velocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Velocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Velocity");
|
||||
|
||||
return (left._velocity > right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Velocity left, Velocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Velocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Velocity");
|
||||
|
||||
return (left._velocity <= right._velocity);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Velocity left, Velocity right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Velocity");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Velocity");
|
||||
|
||||
return (left._velocity >= right._velocity);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with other classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Length = Velocity * PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Length
|
||||
/// </returns>
|
||||
|
||||
public static Length operator *(Velocity velocity, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot multiply a null Velocity");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot multiply a null PrecisionTimeSpan");
|
||||
|
||||
return Length.FromMeters(velocity.MetersPerSec * time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Length = PrecisionTimeSpan * Velocity
|
||||
/// </summary>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <returns>
|
||||
/// Length
|
||||
/// </returns>
|
||||
|
||||
public static Length operator *(PrecisionTimeSpan time, Velocity velocity)
|
||||
{
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot divide a null PrecisionTimeSpan");
|
||||
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide by a null Velocity");
|
||||
|
||||
if (0.0 == velocity.MetersPerSec)
|
||||
throw new DivideByZeroException("Cannot divide Time Span by 0 Velocity");
|
||||
|
||||
return Length.FromMeters(time.Seconds * velocity.MetersPerSec);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Acceleration = Velocity / PrecisionTimeSpan
|
||||
/// </summary>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <param name="time">The time.</param>
|
||||
/// <returns>
|
||||
/// Acceleration
|
||||
/// </returns>
|
||||
|
||||
public static Acceleration operator /(Velocity velocity, PrecisionTimeSpan time)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide a null Velocity");
|
||||
|
||||
if (null == time)
|
||||
throw new ArgumentNullException("time", "Cannot divide by a null PrecisionTimeSpan");
|
||||
|
||||
if (0.0 == time.Milliseconds)
|
||||
throw new DivideByZeroException("Cannot divide Velocity by 0 Time Span");
|
||||
|
||||
return Acceleration.FromMetersPerSecSqrd(velocity.MetersPerSec / time.Seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for PrecisionTimeSpan = Velocity / Acceleration
|
||||
/// </summary>
|
||||
/// <param name="velocity">The velocity.</param>
|
||||
/// <param name="time">The acceleration.</param>
|
||||
/// <returns>
|
||||
/// PrecisionTimeSpan
|
||||
/// </returns>
|
||||
|
||||
public static PrecisionTimeSpan operator /(Velocity velocity, Acceleration acceleration)
|
||||
{
|
||||
if (null == velocity)
|
||||
throw new ArgumentNullException("velocity", "Cannot divide a null Velocity");
|
||||
|
||||
if (null == acceleration)
|
||||
throw new ArgumentNullException("acceleration", "Cannot divide by a null Acceleration");
|
||||
|
||||
if (0.0 == acceleration.MetersPerSecSqrd)
|
||||
throw new DivideByZeroException("Cannot divide Velocity by 0 Acceleration");
|
||||
|
||||
return PrecisionTimeSpan.FromSeconds(velocity.MetersPerSec / acceleration.MetersPerSecSqrd);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Velocity Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Velocity value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Velocity.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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
356
Source/TSRealLib/Common/Raytheon.Common/Units/Voltage.cs
Normal file
356
Source/TSRealLib/Common/Raytheon.Common/Units/Voltage.cs
Normal file
@@ -0,0 +1,356 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Raytheon.Units
|
||||
{
|
||||
/// <summary>
|
||||
/// This class represents a voltage.
|
||||
///
|
||||
/// 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. FromVolts).
|
||||
///
|
||||
/// Properties (e.g. Volts) 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.
|
||||
/// Current, Resistance).
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[DataContract]
|
||||
public class Voltage
|
||||
{
|
||||
public enum Unit
|
||||
{
|
||||
Volts,
|
||||
Millivolts
|
||||
}
|
||||
|
||||
public static IDictionary<Unit, List<string>> UnitAbbreviations = new Dictionary<Unit, List<string>>()
|
||||
{
|
||||
{Unit.Volts, new List<string>(){ "V" } },
|
||||
{Unit.Millivolts, new List<string>(){ "mV" } },
|
||||
};
|
||||
|
||||
private const Unit DefaultUnits = Unit.Volts;
|
||||
|
||||
[DataMember(Name = "Voltage", IsRequired = true)]
|
||||
private double _voltage; // volts
|
||||
|
||||
// map: unit => abbreviation, conversion method and property
|
||||
private static IDictionary<Unit, UnitInfo<Voltage>> _unitInfo = new Dictionary<Unit, UnitInfo<Voltage>>()
|
||||
{
|
||||
{ Unit.Volts, new UnitInfo<Voltage>(UnitAbbreviations[Unit.Volts], FromVolts, typeof(Voltage).GetProperty("Volts").GetGetMethod()) },
|
||||
{ Unit.Millivolts, new UnitInfo<Voltage>(UnitAbbreviations[Unit.Millivolts], FromMillivolts, typeof(Voltage).GetProperty("Millivolts").GetGetMethod()) }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Prevents a default instance of the <see cref="Voltage"/> 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 Voltage()
|
||||
{
|
||||
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="voltage">The voltage - volts.</param>
|
||||
private Voltage(double voltage)
|
||||
{
|
||||
_voltage = voltage;
|
||||
}
|
||||
|
||||
#region conversion properties
|
||||
|
||||
public double Volts
|
||||
{
|
||||
get { return _voltage; }
|
||||
}
|
||||
|
||||
|
||||
public double Millivolts
|
||||
{
|
||||
get { return _voltage * 1000; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region creation methods
|
||||
|
||||
/// <summary>
|
||||
/// Returns Voltage object interpreting passed value as volts
|
||||
/// </summary>
|
||||
/// <param name="volts">volts</param>
|
||||
/// <returns></returns>
|
||||
public static Voltage FromVolts(double volts)
|
||||
{
|
||||
return new Voltage(volts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns Voltage object interpreting passed value as millivolts
|
||||
/// </summary>
|
||||
/// <param name="millivolts">millivolts</param>
|
||||
/// <returns></returns>
|
||||
|
||||
|
||||
public static Voltage FromMillivolts(double millivolts)
|
||||
{
|
||||
return new Voltage(millivolts / 1000);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
Voltage v = obj as Voltage;
|
||||
|
||||
return (v == null) ? false : (this._voltage == v._voltage);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _voltage.GetHashCode();
|
||||
}
|
||||
|
||||
#region binary operators
|
||||
|
||||
// not implementing %, &, |, ^, <<, >>
|
||||
|
||||
|
||||
public static Voltage operator +(Voltage left, Voltage right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot add a null Voltage");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot add null Voltage");
|
||||
|
||||
return new Voltage(left._voltage + right._voltage);
|
||||
}
|
||||
|
||||
|
||||
public static Voltage operator -(Voltage left, Voltage right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot subtract a null Voltage");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot subtract a null Voltage");
|
||||
|
||||
return new Voltage(left._voltage - right._voltage);
|
||||
}
|
||||
|
||||
|
||||
public static Voltage operator *(Voltage voltage, double scalar)
|
||||
{
|
||||
if (null == voltage)
|
||||
throw new ArgumentNullException("voltage", "Cannot multiply a null Voltage");
|
||||
|
||||
return new Voltage(voltage._voltage * scalar);
|
||||
}
|
||||
|
||||
|
||||
public static Voltage operator *(double scalar, Voltage voltage)
|
||||
{
|
||||
if (null == voltage)
|
||||
throw new ArgumentNullException("voltage", "Cannot multiply a null Voltage");
|
||||
|
||||
return new Voltage(scalar * voltage._voltage);
|
||||
}
|
||||
|
||||
|
||||
public static Voltage operator /(Voltage voltage, double scalar)
|
||||
{
|
||||
if (null == voltage)
|
||||
throw new ArgumentNullException("voltage", "Cannot divide a null Voltage");
|
||||
|
||||
if (0.0 == scalar)
|
||||
throw new DivideByZeroException("Cannot divide Voltage by 0");
|
||||
|
||||
return new Voltage(voltage._voltage / scalar);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region comparison operators
|
||||
|
||||
public static bool operator ==(Voltage left, Voltage 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._voltage == right._voltage;
|
||||
}
|
||||
}
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
public static bool operator !=(Voltage left, Voltage right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <(Voltage left, Voltage right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Voltage");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Voltage");
|
||||
|
||||
return (left._voltage < right._voltage);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >(Voltage left, Voltage right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Voltage");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Voltage");
|
||||
|
||||
return (left._voltage > right._voltage);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator <=(Voltage left, Voltage right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Voltage");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Voltage");
|
||||
|
||||
return (left._voltage <= right._voltage);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator >=(Voltage left, Voltage right)
|
||||
{
|
||||
if (null == left)
|
||||
throw new ArgumentNullException("left", "Cannot compare null Voltage");
|
||||
|
||||
if (null == right)
|
||||
throw new ArgumentNullException("right", "Cannot compare null Voltage");
|
||||
|
||||
return (left._voltage >= right._voltage);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operators with non-Voltage classes
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Current = Voltage / Resistance
|
||||
/// </summary>
|
||||
/// <param name="voltage">The voltage.</param>
|
||||
/// <param name="time">The resistance.</param>
|
||||
/// <returns>
|
||||
/// Current
|
||||
/// </returns>
|
||||
|
||||
public static Current operator /(Voltage voltage, Resistance resistance)
|
||||
{
|
||||
if (null == voltage)
|
||||
throw new ArgumentNullException("voltage", "Cannot divide a null Voltage");
|
||||
|
||||
if (null == resistance)
|
||||
throw new ArgumentNullException("resistance", "Cannot divide by a null Resistance");
|
||||
|
||||
if (0.0 == resistance.Ohms)
|
||||
throw new DivideByZeroException("Cannot divide Voltage by 0 Resistance");
|
||||
|
||||
return Current.FromAmps(voltage.Volts / resistance.Ohms);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator * for Resistance = Voltage / Current
|
||||
/// </summary>
|
||||
/// <param name="voltage">The voltage.</param>
|
||||
/// <param name="time">The current.</param>
|
||||
/// <returns>
|
||||
/// Resistance
|
||||
/// </returns>
|
||||
|
||||
public static Resistance operator /(Voltage voltage, Current current)
|
||||
{
|
||||
if (null == voltage)
|
||||
throw new ArgumentNullException("voltage", "Cannot divide a null Voltage");
|
||||
|
||||
if (null == current)
|
||||
throw new ArgumentNullException("current", "Cannot divide by a null Current");
|
||||
|
||||
if (0.0 == current.Amps)
|
||||
throw new DivideByZeroException("Cannot divide Voltage by 0 Current");
|
||||
|
||||
return Resistance.FromOhms(voltage.Volts / current.Amps);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region parsing
|
||||
|
||||
public static Voltage Parse(string s)
|
||||
{
|
||||
return Common.Parse(s, _unitInfo);
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out Voltage value)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = Voltage.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