Big changes
This commit is contained in:
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]}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user