public override bool Equals(object obj) { QuantityDimension QD = obj as QuantityDimension; if (QD != null) { if (!this.ElectricCurrent.Equals(QD.ElectricCurrent)) { return(false); } if (!this.Length.Equals(QD.Length)) { return(false); } if (!this.LuminousIntensity.Equals(QD.LuminousIntensity)) { return(false); } if (!this.Mass.Equals(QD.Mass)) { return(false); } if (!this.AmountOfSubstance.Equals(QD.AmountOfSubstance)) { return(false); } if (!this.Temperature.Equals(QD.Temperature)) { return(false); } if (!this.Time.Equals(QD.Time)) { return(false); } if (!this.Currency.Equals(QD.Currency)) { return(false); } if (!this.Digital.Equals(QD.Digital)) { return(false); } return(true); } else { return(false); } }
public QuantityDimension(QuantityDimension dimension) { Mass = dimension.Mass; Length = dimension.Length; Time = dimension.Time; ElectricCurrent = dimension.ElectricCurrent; Temperature = dimension.Temperature; AmountOfSubstance = dimension.AmountOfSubstance; LuminousIntensity = dimension.LuminousIntensity; Currency = dimension.Currency; Digital = dimension.Digital; }
/// <summary> /// Returns the quantity type or typeof(DerivedQuantity<>) without throwing exception /// </summary> /// <param name="dimension"></param> /// <returns></returns> public static Type GetQuantityTypeFrom(QuantityDimension dimension) { Type qType; if (CurrentQuantitiesDictionary.TryGetValue(dimension, out qType)) { return(qType); } else { return(typeof(DerivedQuantity <>)); } }
public QuantityDimension Invert() { QuantityDimension qd = new QuantityDimension(); qd.Mass = Mass.Invert(); qd.Length = Length.Invert(); qd.Time = Time.Invert(); qd.ElectricCurrent = ElectricCurrent.Invert(); qd.Temperature = Temperature.Invert(); qd.AmountOfSubstance = AmountOfSubstance.Invert(); qd.LuminousIntensity = LuminousIntensity.Invert(); qd.Currency = Currency.Invert(); qd.Digital = Digital.Invert(); return(qd); }
/// <summary> /// Get the corresponding typed quantity in the framework of this dimension /// Throws QuantityNotFounException when there is corresponding one. /// </summary> /// <param name="dimension"></param> /// <returns></returns> public static Type QuantityTypeFrom(QuantityDimension dimension) { try { Type QuantityType = CurrentQuantitiesDictionary[dimension]; return(QuantityType); } catch (KeyNotFoundException ex) { QuantityNotFoundException qnfe = new QuantityNotFoundException("Couldn't Find the quantity dimension in the dimensions Hash Key", ex); throw qnfe; } }
public static QuantityDimension Multiply(QuantityDimension dimension, float exponent) { QuantityDimension QD = new QuantityDimension(); QD.Mass = dimension.Mass.Multiply(exponent); QD.Length = dimension.Length.Multiply(exponent); QD.Time = dimension.Time.Multiply(exponent); QD.Temperature = dimension.Temperature.Multiply(exponent); QD.ElectricCurrent = dimension.ElectricCurrent.Multiply(exponent); QD.AmountOfSubstance = dimension.AmountOfSubstance.Multiply(exponent); QD.LuminousIntensity = dimension.LuminousIntensity.Multiply(exponent); QD.Currency = dimension.Currency.Multiply(exponent); QD.Digital = dimension.Digital.Multiply(exponent); return(QD); }
public static QuantityDimension Subtract(QuantityDimension firstDimension, QuantityDimension secondDimension) { QuantityDimension QD = new QuantityDimension(); QD.Mass = firstDimension.Mass.Subtract(secondDimension.Mass); QD.Length = firstDimension.Length.Subtract(secondDimension.Length); QD.Time = firstDimension.Time.Subtract(secondDimension.Time); QD.Temperature = firstDimension.Temperature.Subtract(secondDimension.Temperature); QD.ElectricCurrent = firstDimension.ElectricCurrent.Subtract(secondDimension.ElectricCurrent); QD.AmountOfSubstance = firstDimension.AmountOfSubstance.Subtract(secondDimension.AmountOfSubstance); QD.LuminousIntensity = firstDimension.LuminousIntensity.Subtract(secondDimension.LuminousIntensity); QD.Currency = firstDimension.Currency.Subtract(secondDimension.Currency); QD.Digital = firstDimension.Digital.Subtract(secondDimension.Digital); return(QD); }
/// <summary> /// Equality here based on first level of exponent validation. /// Which means length explicitly is compared to the total dimension value not on radius and normal length values. /// </summary> /// <param name="dimension"></param> /// <returns></returns> public bool IsEqual(QuantityDimension dimension) { if (this.ElectricCurrent.Exponent != dimension.ElectricCurrent.Exponent) { return(false); } if (this.Length.Exponent != dimension.Length.Exponent) { return(false); } if (this.LuminousIntensity.Exponent != dimension.LuminousIntensity.Exponent) { return(false); } if (this.Mass.Exponent != dimension.Mass.Exponent) { return(false); } if (this.AmountOfSubstance.Exponent != dimension.AmountOfSubstance.Exponent) { return(false); } if (this.Temperature.Exponent != dimension.Temperature.Exponent) { return(false); } if (this.Time.Exponent != dimension.Time.Exponent) { return(false); } if (this.Currency.Exponent != dimension.Currency.Exponent) { return(false); } return(true); }
/// <summary> /// Returns Strongly typed Any Quantity From the dimension based on the discovered quantities discovered when /// framework initiated. /// Throws <see cref="QuantityNotFoundException"/> when quantity is not found. /// </summary> /// <typeparam name="T">The value container of the Quantity</typeparam> /// <param name="dimension"></param> /// <returns></returns> public static AnyQuantity <T> QuantityFrom <T>(QuantityDimension dimension) { lock (QuantitiesCached) { Type QuantityType = QuantityTypeFrom(dimension); //the quantity type now is without container type we should generate it Type QuantityWithContainerType = QuantityType.MakeGenericType(typeof(T)); object j; if (QuantitiesCached.TryGetValue(QuantityWithContainerType, out j)) { return((AnyQuantity <T>)((AnyQuantity <T>)j).Clone()); } else { j = Activator.CreateInstance(QuantityWithContainerType); QuantitiesCached.Add(QuantityWithContainerType, j); return((AnyQuantity <T>)((AnyQuantity <T>)j).Clone()); } } }
public static QuantityDimension Parse(string dimension) { dimension = dimension.Trim(); // M L T I O N J C List <float> exps = new List <float>(); if (char.IsNumber(dimension[0])) { // parsing started with numbers which will be most probably // on the format of numbers with spaces string[] dims = dimension.Split(' '); foreach (var dim in dims) { var dimtrimmed = dim.Trim(); if (!string.IsNullOrEmpty(dimtrimmed)) { exps.Add(float.Parse(dimtrimmed)); } } float M = exps.Count > 0 ? exps[0] : 0; float L = exps.Count > 1 ? exps[1] : 0; float T = exps.Count > 2 ? exps[2] : 0; float I = exps.Count > 3 ? exps[3] : 0; float O = exps.Count > 4 ? exps[4] : 0; float N = exps.Count > 5 ? exps[5] : 0; float J = exps.Count > 6 ? exps[6] : 0; float C = exps.Count > 7 ? exps[7] : 0; float D = exps.Count > 8 ? exps[8] : 0; var qd = new QuantityDimension(M, L, T, I, O, N, J); qd.Currency = new CurrencyDescriptor(C); qd.Digital = new DigitalDescriptor(D); return(qd); } else { // the format is based on letter and number var dumber = dimension.ToUpperInvariant(); var mts = Regex.Matches(dumber, @"(([MmLlTtIiOoNnJjCcDd])(\-*[0-9]+))+?"); Dictionary <char, float> edps = new Dictionary <char, float>(); foreach (Match m in mts) { edps.Add(m.Groups[2].Value[0], float.Parse(m.Groups[3].Value)); } if (!edps.ContainsKey('M')) { edps['M'] = 0; } if (!edps.ContainsKey('L')) { edps['L'] = 0; } if (!edps.ContainsKey('T')) { edps['T'] = 0; } if (!edps.ContainsKey('I')) { edps['I'] = 0; } if (!edps.ContainsKey('O')) { edps['O'] = 0; } if (!edps.ContainsKey('N')) { edps['N'] = 0; } if (!edps.ContainsKey('J')) { edps['J'] = 0; } if (!edps.ContainsKey('C')) { edps['C'] = 0; } if (!edps.ContainsKey('D')) { edps['D'] = 0; } var qd = new QuantityDimension(edps['M'] , edps['L'], edps['T'], edps['I'], edps['O'] , edps['N'], edps['J']); qd.Currency = new CurrencyDescriptor(edps['C']); qd.Digital = new DigitalDescriptor(edps['D']); return(qd); } }