using System; using System.Collections.Generic; using System.Runtime.Serialization; namespace Raytheon.Units { /// /// 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. /// [Serializable] [DataContract] public class Frequency { public enum Unit { Hertz, KiloHertz, MegaHertz, GigaHertz } public static IDictionary> UnitAbbreviations = new Dictionary>() { {Unit.Hertz, new List(){ "Hz" } }, {Unit.KiloHertz, new List(){ "kHz" } }, {Unit.MegaHertz, new List(){ "MHz" } }, {Unit.GigaHertz, new List(){ "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> _unitInfo = new Dictionary>() { { Unit.Hertz, new UnitInfo(UnitAbbreviations[Unit.Hertz], FromHertz, typeof(Frequency).GetProperty("Hertz").GetGetMethod()) }, { Unit.KiloHertz, new UnitInfo(UnitAbbreviations[Unit.KiloHertz], FromKiloHertz, typeof(Frequency).GetProperty("KiloHertz").GetGetMethod()) }, { Unit.MegaHertz, new UnitInfo(UnitAbbreviations[Unit.MegaHertz], FromMegaHertz, typeof(Frequency).GetProperty("MegaHertz").GetGetMethod()) }, { Unit.GigaHertz, new UnitInfo(UnitAbbreviations[Unit.GigaHertz], FromGigaHertz, typeof(Frequency).GetProperty("GigaHertz").GetGetMethod()) } }; /// /// Prevents a default instance of the class from being created. /// Use FromXXX methods to create objects of this type. /// /// No one should be calling this, it is only here to prevent users from creating default object private Frequency() { throw new InvalidOperationException("No one should be calling this, it is only here to prevent users from creating default object"); } /// /// ctor used by this class to create objects in FromXXX methods /// /// The frequency - Hertz. 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 /// /// Returns Frequency object interpreting passed value as Hertz /// /// Hertz /// public static Frequency FromGigaHertz(double frequency) { return new Frequency(frequency * Constants.GIGA); } /// /// Returns Frequency object interpreting passed value as Hertz /// /// Hertz /// public static Frequency FromHertz(double frequency) { return new Frequency(frequency); } /// /// Returns Frequency object interpreting passed value as Hertz /// /// Hertz /// public static Frequency FromKiloHertz(double frequency) { return new Frequency(frequency * Constants.KILO); } /// /// Returns Frequency object interpreting passed value as Hertz /// /// Hertz /// 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 /// /// Returns a string appended with default units (e.g. "1.23 m/sec^2"). /// /// /// A string that represents this instance /// public override string ToString() { return ToString(DefaultUnits); } /// /// overload that returns a string appended with specified units (e.g. "1.23 ft/sec^2"). /// /// /// public string ToString(Unit units) { double value = (double)_unitInfo[units].PropertyGetter.Invoke(this, null); return $"{value} {_unitInfo[units].Abbreviation[0]}"; } } }