/// <summary> /// Searches in the collection for a unit that is a unit for the same physical dimension as /// the <paramref name="unit"/> is for. /// </summary> /// <param name="collection">The collection to search.</param> /// <param name="unit">The unit to search for.</param> /// <returns>The index of the first occurrance in the collection. If no item in the collection matches the criteria, this method returns -1.</returns> public static int LinearSearch(this IEnumerable <UnitComposite> collection, UnitBase unit) { int index = 0; foreach (UnitComposite c in collection) { if (unit.Quantity.Equals(c.Quantity)) { return(index); } index++; } return(-1); }
/// <summary> /// Adds a new divided by unit. /// </summary> /// <param name="unit"></param> /// <param name="power"></param> public void DividedBy(UnitBase unit, int power) { if (unit == null) { return; } if (power == 0) { // power of 0 equals to 1, so no unit. return; } AddInternal(new DividedByUnit(unit, power)); }
/// <summary> /// Does a unit conversion on this object instance. /// </summary> /// <param name="newUnit"></param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown, if <param name="newUnit"> is null.</param></exception> protected virtual void ConvertToInternal(UnitBase newUnit) { if (newUnit == null) { throw new ArgumentNullException("newUnit"); } if (newUnit == this.UnitInternal) { // nothing to do return; } var oldUnit = UnitInternal; this.UnitInternal = newUnit; this.Value = value * GetConversionFactor(oldUnit, newUnit); }
/// <summary> /// Does a unit conversion on this object instance. /// </summary> /// <param name="newUnit"></param> /// <returns></returns> /// <exception cref="ArgumentNullException">Thrown, if <param name="newUnit"> is null.</param></exception> protected override void ConvertToInternal(UnitBase newUnit) { TemperatureUnit oldUnit = (TemperatureUnit)this.Unit; Temperature t = new Temperature(this.Value, oldUnit); // divert over Celsius, because it is the base unit. // Fahrenheit -> Celsius double valueC = t.Value - oldUnit.Offset; // apply the offset first valueC = valueC * GetConversionFactor(oldUnit, Celsius); // then the factor Temperature tC = new Temperature(valueC, Celsius); // new temperature in celsius. // celsius -> Fahrenheit double valueNew = tC.Value * GetConversionFactor(Celsius, newUnit); // first the factor valueNew = valueNew + newUnit.Offset; // then the offset. this.Value = valueNew; this.UnitInternal = newUnit; }
/// <summary> /// Base constructor for all physical quantities. /// </summary> /// <param name="value">The numerical value of the physical quantity in the given unit.</param> /// <param name="unit">The unit of the quantity.</param> protected PhysicalQuantityBase(double value, UnitBase unit) { this.UnitInternal = unit; this.value = value; }
/// <summary> /// Decorates a unit with a positive power. /// </summary> /// <param name="unit">The unit that is multiplied with.</param> /// <param name="power">The power of that unit that.</param> public MultipliedByUnit(UnitBase unit, int power) : base(unit, Math.Abs(power)) { }
/// <summary> /// Constructor for base units. /// </summary> /// <param name="multiple">The multiple that shall be applied to the base unit</param> /// <param name="baseUnit">The base unit.</param> protected CoherentUnitBase(Multiple multiple, UnitBase baseUnit) : base(multiple, baseUnit) { }
/// <summary> /// Constructor for both coherent and base units /// </summary> /// <param name="baseUnit">The base unit.</param> protected CoherentUnitBase(UnitBase baseUnit) : this(new UnitCompositeCollection() { new MultipliedByUnit(baseUnit, 1) }) { }