using System; using System.Text; using System.Runtime.Serialization; using System.Diagnostics.CodeAnalysis; using System.Collections.Generic; namespace Raytheon.Units { /// /// 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). /// [Serializable] [DataContract] public class Current { public enum Unit { Amps, Milliamps } public static IDictionary> UnitAbbreviations = new Dictionary>() { {Unit.Amps, new List(){ "A" } }, {Unit.Milliamps, new List(){ "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> _unitInfo = new Dictionary>() { { Unit.Amps, new UnitInfo(UnitAbbreviations[Unit.Amps], FromAmps, typeof(Current).GetProperty("Amps").GetGetMethod()) }, { Unit.Milliamps, new UnitInfo(UnitAbbreviations[Unit.Milliamps], FromMilliamps, typeof(Current).GetProperty("Milliamps").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 Current() { 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 current - amps. private Current(double current) { _current = current; } #region conversion properties /// /// Returns amps as type double /// /// /// amps /// public double Amps { get { return _current; } } /// /// Returns milliamps as type double /// /// /// milliamps /// public double Milliamps { get { return _current * MilliampsPerAmp; } } #endregion #region creation methods /// /// Returns Current object interpreting passed value as amps /// /// amps /// public static Current FromAmps(double amps) { return new Current(amps); } /// /// Returns Current object interpreting passed value as milliamps /// /// milliamps /// 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 /// /// Implements the operator * for Voltage = Current * Resistance /// /// The current. /// The resistance. /// /// Voltage /// 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 /// /// 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]}"; } } }