Exemplo n.º 1
0
        /// <summary>
        ///     Constructs an instance.
        /// </summary>
        /// <param name="quantityType">The quantity enum value.</param>
        /// <param name="unitInfos">The information about the units for this quantity.</param>
        /// <param name="baseUnit">The base unit enum value.</param>
        /// <param name="zero">The zero quantity.</param>
        /// <param name="baseDimensions">The base dimensions of the quantity.</param>
        /// <exception cref="ArgumentException">Quantity type can not be undefined.</exception>
        /// <exception cref="ArgumentNullException">If units -or- baseUnit -or- zero -or- baseDimensions is null.</exception>
        public QuantityInfo(QuantityType quantityType, [NotNull] UnitInfo[] unitInfos, [NotNull] Enum baseUnit, [NotNull] IQuantity zero, [NotNull] BaseDimensions baseDimensions)
        {
            if (quantityType == QuantityType.Undefined)
            {
                throw new ArgumentException("Quantity type can not be undefined.", nameof(quantityType));
            }
            if (baseUnit == null)
            {
                throw new ArgumentNullException(nameof(baseUnit));
            }

            BaseDimensions = baseDimensions ?? throw new ArgumentNullException(nameof(baseDimensions));
            Zero           = zero ?? throw new ArgumentNullException(nameof(zero));


            Name           = quantityType.ToString();
            QuantityType   = quantityType;
            UnitType       = UnitEnumTypes.First(t => t.Name == $"{quantityType}Unit");
            UnitInfos      = unitInfos ?? throw new ArgumentNullException(nameof(unitInfos));
            BaseUnitInfo   = UnitInfos.First(unitInfo => unitInfo.Value.Equals(baseUnit));
            Zero           = zero ?? throw new ArgumentNullException(nameof(zero));
            ValueType      = zero.GetType();
            BaseDimensions = baseDimensions ?? throw new ArgumentNullException(nameof(baseDimensions));

            // Obsolete members
#pragma warning disable 618
            UnitNames = UnitInfos.Select(unitInfo => unitInfo.Name).ToArray();
            Units     = UnitInfos.Select(unitInfo => unitInfo.Value).ToArray();
            BaseUnit  = BaseUnitInfo.Value;
#pragma warning restore 618
        }
Exemplo n.º 2
0
        public QuantityInfo(QuantityType quantityType, [NotNull] Enum[] units, [NotNull] Enum baseUnit, [NotNull] IQuantity zero, [NotNull] BaseDimensions baseDimensions)
        {
            if (quantityType == QuantityType.Undefined)
            {
                throw new ArgumentException("Quantity type can not be undefined.", nameof(quantityType));
            }
            if (units == null)
            {
                throw new ArgumentNullException(nameof(units));
            }
            if (baseUnit == null)
            {
                throw new ArgumentNullException(nameof(baseUnit));
            }
            if (zero == null)
            {
                throw new ArgumentNullException(nameof(zero));
            }
            if (baseDimensions == null)
            {
                throw new ArgumentNullException(nameof(baseDimensions));
            }

            Name           = quantityType.ToString();
            QuantityType   = quantityType;
            UnitType       = UnitEnumTypes.First(t => t.Name == $"{quantityType}Unit");
            UnitInfos      = units.Select(unit => new UnitInfo(unit)).ToArray();
            UnitNames      = UnitInfos.Select(unitInfo => unitInfo.Name).ToArray();
            Units          = units;
            BaseUnitInfo   = new UnitInfo(baseUnit);
            BaseUnit       = BaseUnitInfo.Value;
            Zero           = zero;
            ValueType      = zero.GetType();
            BaseDimensions = baseDimensions;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Parse elements that are inline in the read loop
 /// </summary>
 public void ParseElementsInline(XmlReader s, IQuantity retVal, DatatypeR2FormatterParseResult result)
 {
     if (s.LocalName == "expression") // Format using ED
     {
         var hostResult = this.Host.Parse(s, typeof(ED));
         result.Code = hostResult.Code;
         result.AddResultDetail(hostResult.Details);
         retVal.Expression = hostResult.Structure as ED;
     }
     else if (s.LocalName == "originalText") // display name
     {
         var hostResult = this.Host.Parse(s, typeof(ED));
         result.Code = hostResult.Code;
         result.AddResultDetail(hostResult.Details);
         retVal.OriginalText = hostResult.Structure as ED;
     }
     else if (s.LocalName == "uncertainty") // Translation
     {
         var hostResult = Host.Parse(s, retVal.GetType());
         result.Code = hostResult.Code;
         result.AddResultDetail(hostResult.Details);
         retVal.Uncertainty = hostResult.Structure as IQuantity;
     }
     else if (s.LocalName == "uncertainRange") // Uncertainty range
     {
         var hostResult = Host.Parse(s, typeof(IVL <IQuantity>));
         result.Code = hostResult.Code;
         result.AddResultDetail(hostResult.Details);
         retVal.UncertainRange = hostResult.Structure as IVL <IQuantity>;
     }
     else if (s.NodeType == System.Xml.XmlNodeType.Element)
     {
         result.AddResultDetail(new NotImplementedElementResultDetail(ResultDetailType.Warning, s.LocalName, s.NamespaceURI, s.ToString(), null));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Constructs an instance.
        /// </summary>
        /// <param name="name">Name of the quantity.</param>
        /// <param name="unitInfos">The information about the units for this quantity.</param>
        /// <param name="baseUnit">The base unit enum value.</param>
        /// <param name="zero">The zero quantity.</param>
        /// <param name="baseDimensions">The base dimensions of the quantity.</param>
        /// <param name="quantityType">The the quantity type. Defaults to Undefined.</param>
        /// <exception cref="ArgumentException">Quantity type can not be undefined.</exception>
        /// <exception cref="ArgumentNullException">If units -or- baseUnit -or- zero -or- baseDimensions is null.</exception>
        public QuantityInfo([NotNull] string name, [NotNull] UnitInfo[] unitInfos, [NotNull] Enum baseUnit, [NotNull] IQuantity zero, [NotNull] BaseDimensions baseDimensions,
                            QuantityType quantityType = QuantityType.Undefined)
        {
            if (baseUnit == null)
            {
                throw new ArgumentNullException(nameof(baseUnit));
            }

            BaseDimensions = baseDimensions ?? throw new ArgumentNullException(nameof(baseDimensions));
            Zero           = zero ?? throw new ArgumentNullException(nameof(zero));

            Name           = name;
            UnitType       = UnitEnumTypes.First(t => t.Name == $"{name}Unit");
            UnitInfos      = unitInfos ?? throw new ArgumentNullException(nameof(unitInfos));
            BaseUnitInfo   = UnitInfos.First(unitInfo => unitInfo.Value.Equals(baseUnit));
            Zero           = zero ?? throw new ArgumentNullException(nameof(zero));
            ValueType      = zero.GetType();
            BaseDimensions = baseDimensions ?? throw new ArgumentNullException(nameof(baseDimensions));

            // Obsolete members
            UnitNames    = UnitInfos.Select(unitInfo => unitInfo.Name).ToArray();
            Units        = UnitInfos.Select(unitInfo => unitInfo.Value).ToArray();
            BaseUnit     = BaseUnitInfo.Value;
            QuantityType = quantityType;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Registers the specified unit.
        /// </summary>
        /// <param name="unit">
        /// The unit.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        public void RegisterUnit(IQuantity unit, string name)
        {
            var type = unit.GetType();

            if (!this.units.ContainsKey(type))
            {
                this.units.Add(type, new Dictionary <string, IQuantity>());
            }

            if (this.units[type].ContainsKey(name))
            {
                throw new InvalidOperationException(string.Format("{0} is already added to {1}", name, type));
            }

            this.units[type].Add(name, unit);
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Constructs an instance.
        /// </summary>
        /// <param name="name">Name of the quantity.</param>
        /// <param name="unitType">The unit enum type, such as <see cref="LengthUnit" />.</param>
        /// <param name="unitInfos">The information about the units for this quantity.</param>
        /// <param name="baseUnit">The base unit enum value.</param>
        /// <param name="zero">The zero quantity.</param>
        /// <param name="baseDimensions">The base dimensions of the quantity.</param>
        /// <param name="quantityType">The the quantity type. Defaults to Undefined.</param>
        /// <exception cref="ArgumentException">Quantity type can not be undefined.</exception>
        /// <exception cref="ArgumentNullException">If units -or- baseUnit -or- zero -or- baseDimensions is null.</exception>
        public QuantityInfo(string name, Type unitType, UnitInfo[] unitInfos, Enum baseUnit, IQuantity zero, Dimensions baseDimensions,
                            QuantityType quantityType = QuantityType.Undefined)
        {
            if (baseUnit == null)
            {
                throw new ArgumentNullException(nameof(baseUnit));
            }

            BaseDimensions = baseDimensions ?? throw new ArgumentNullException(nameof(baseDimensions));
            Zero           = zero ?? throw new ArgumentNullException(nameof(zero));

            Name           = name ?? throw new ArgumentNullException(nameof(name));
            UnitType       = unitType ?? throw new ArgumentNullException(nameof(unitType));
            UnitInfos      = unitInfos ?? throw new ArgumentNullException(nameof(unitInfos));
            BaseUnitInfo   = UnitInfos.First(unitInfo => unitInfo.Value.Equals(baseUnit));
            Zero           = zero ?? throw new ArgumentNullException(nameof(zero));
            ValueType      = zero.GetType();
            BaseDimensions = baseDimensions ?? throw new ArgumentNullException(nameof(baseDimensions));

            // Obsolete members
            QuantityType = quantityType;
        }
Exemplo n.º 7
0
 public static GBP From(IQuantity <decimal> q)
 {
     if (q.Unit.Family != GBP.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"GBP\"", q.GetType().Name));
     }
     return(new GBP((GBP.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 8
0
        public static Fahrenheit From(IQuantity <double> q)
        {
            Scale <double> scale = Catalog.Scale(Fahrenheit.Family, q.Unit);

            if (scale == null)
            {
                throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Fahrenheit\".", q.GetType().Name));
            }
            return(Fahrenheit.From(scale.Create(q.Value)));
        }
Exemplo n.º 9
0
 public static Cycles From(IQuantity <double> q)
 {
     if (q.Unit.Family != Cycles.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Cycles\"", q.GetType().Name));
     }
     return(new Cycles((Cycles.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 10
0
 public static NewtonMeter From(IQuantity <double> q)
 {
     if (q.Unit.Family != NewtonMeter.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"NewtonMeter\"", q.GetType().Name));
     }
     return(new NewtonMeter((NewtonMeter.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 11
0
 public static Meter_Sec From(IQuantity <double> q)
 {
     if (q.Unit.Family != Meter_Sec.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Meter_Sec\"", q.GetType().Name));
     }
     return(new Meter_Sec((Meter_Sec.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 12
0
 public static Joule_Kelvin From(IQuantity <double> q)
 {
     if (q.Unit.Family != Joule_Kelvin.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Joule_Kelvin\"", q.GetType().Name));
     }
     return(new Joule_Kelvin((Joule_Kelvin.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 13
0
 public static AtmTechnical From(IQuantity <double> q)
 {
     if (q.Unit.Family != AtmTechnical.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"AtmTechnical\"", q.GetType().Name));
     }
     return(new AtmTechnical((AtmTechnical.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 14
0
 public static Kilogram From(IQuantity <double> q)
 {
     if (q.Unit.Family != Kilogram.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Kilogram\"", q.GetType().Name));
     }
     return(new Kilogram((Kilogram.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 15
0
 public static DegFahrenheit From(IQuantity <double> q)
 {
     if (q.Unit.Family != DegFahrenheit.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"DegFahrenheit\"", q.GetType().Name));
     }
     return(new DegFahrenheit((DegFahrenheit.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 16
0
 public static PoundForce From(IQuantity <double> q)
 {
     if (q.Unit.Family != PoundForce.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"PoundForce\"", q.GetType().Name));
     }
     return(new PoundForce((PoundForce.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 17
0
 public static Steradian From(IQuantity <double> q)
 {
     if (q.Unit.Family != Steradian.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Steradian\"", q.GetType().Name));
     }
     return(new Steradian((Steradian.Factor / q.Unit.Factor) * q.Value));
 }
Exemplo n.º 18
0
 public static Farad From(IQuantity <double> q)
 {
     if (q.Unit.Family != Farad.Family)
     {
         throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" to \"Farad\"", q.GetType().Name));
     }
     return(new Farad((Farad.Factor / q.Unit.Factor) * q.Value));
 }