Files
GenericTeProgramLibrary/Source/TSRealLib/Common/Raytheon.Common/Units/Energy.cs
2025-03-13 12:04:22 -07:00

323 lines
9.7 KiB
C#

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]}";
}
}
}