Пример #1
0
        public bool TryBuild(out Area result, AreaUnit?defaultUnit = null)
        {
            number?  squareMetres = _squareMetres;
            number?  value        = _value;
            AreaUnit?unit         = _unit ?? defaultUnit;

            if (squareMetres.HasValue)
            {
                result = new Area(squareMetres.Value);
                if (unit.HasValue)
                {
                    result = result.Convert(unit.Value);
                }
                return(true);
            }

            if (value.HasValue && unit.HasValue)
            {
                result = new Area(value.Value, unit.Value);
                return(true);
            }

            result = default(Area);
            return(false);
        }
Пример #2
0
        /// <summary>
        ///     Creates the quantity with the given numeric value and unit.
        /// </summary>
        /// <param name="numericValue">The numeric value  to contruct this quantity with.</param>
        /// <param name="unit">The unit representation to contruct this quantity with.</param>
        /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
        /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception>
        private Area(double numericValue, AreaUnit unit)
        {
            if (unit == AreaUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = unit;
        }
Пример #3
0
        public Area(decimal value, AreaUnit unit)
        {
            if (unit == AreaUnit.Undefined)
            {
                throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit));
            }

            _value = value;
            _unit  = unit;
        }
Пример #4
0
        /// <summary>
        /// Creates an instance of the quantity with the given numeric value in units compatible with the given <see cref="UnitSystem"/>.
        /// </summary>
        /// <param name="numericValue">The numeric value  to contruct this quantity with.</param>
        /// <param name="unitSystem">The unit system to create the quantity with.</param>
        /// <exception cref="ArgumentNullException">The given <see cref="UnitSystem"/> is null.</exception>
        /// <exception cref="InvalidOperationException">No unit was found for the given <see cref="UnitSystem"/>.</exception>
        /// <exception cref="InvalidOperationException">More than one unit was found for the given <see cref="UnitSystem"/>.</exception>
        public Area(double numericValue, UnitSystem unitSystem)
        {
            if (unitSystem == null)
            {
                throw new ArgumentNullException(nameof(unitSystem));
            }

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = Info.GetUnitInfoFor(unitSystem.BaseUnits).Value;
        }
Пример #5
0
        private Area(number squareMetres, number?value, AreaUnit?unit, bool validate = true)
        {
            if (validate)
            {
                Assert.IsInRange(squareMetres, MinSquareMetres, MaxSquareMetres, nameof(value));
            }

            SquareMetres = squareMetres;
            _value       = value;
            _unit        = unit;
        }
Пример #6
0
        /// <summary>
        /// Creates an instance of the quantity with the given numeric value in units compatible with the given <see cref="UnitSystem"/>.
        /// If multiple compatible units were found, the first match is used.
        /// </summary>
        /// <param name="numericValue">The numeric value  to contruct this quantity with.</param>
        /// <param name="unitSystem">The unit system to create the quantity with.</param>
        /// <exception cref="ArgumentNullException">The given <see cref="UnitSystem"/> is null.</exception>
        /// <exception cref="ArgumentException">No unit was found for the given <see cref="UnitSystem"/>.</exception>
        public Area(double numericValue, UnitSystem unitSystem)
        {
            if (unitSystem == null)
            {
                throw new ArgumentNullException(nameof(unitSystem));
            }

            var unitInfos     = Info.GetUnitInfosFor(unitSystem.BaseUnits);
            var firstUnitInfo = unitInfos.FirstOrDefault();

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = firstUnitInfo?.Value ?? throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem));
        }
Пример #7
0
        public Area(decimal value, UnitSystem unitSystem)
        {
            if (unitSystem is null)
            {
                throw new ArgumentNullException(nameof(unitSystem));
            }

            var unitInfos     = Info.GetUnitInfosFor(unitSystem.BaseUnits);
            var firstUnitInfo = unitInfos.FirstOrDefault();

            _value = value;
            _unit  = firstUnitInfo?.Value ?? throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem));
        }
Пример #8
0
        public Area(double value, UnitSystem unitSystem)
        {
            if (unitSystem is null)
            {
                throw new ArgumentNullException(nameof(unitSystem));
            }

            _value = Guard.EnsureValidNumber(value, nameof(value));

            var defaultUnitInfo = unitSystem.GetDefaultUnitInfo(QuantityType) as UnitInfo <AreaUnit>;

            _unit = defaultUnitInfo?.Value ?? throw new ArgumentException("No default unit was defined for the given UnitSystem.", nameof(unitSystem));
        }
Пример #9
0
        /// <summary>
        /// Creates an instance of the quantity with the given numeric value in units compatible with the given <see cref="UnitSystem"/>.
        /// If multiple compatible units were found, the first match is used.
        /// </summary>
        /// <param name="numericValue">The numeric value  to contruct this quantity with.</param>
        /// <param name="unitSystem">The unit system to create the quantity with.</param>
        /// <exception cref="ArgumentNullException">The given <see cref="UnitSystem"/> is null.</exception>
        /// <exception cref="ArgumentException">No unit was found for the given <see cref="UnitSystem"/>.</exception>
        public Area(double numericValue, UnitSystem unitSystem)
        {
            if (unitSystem == null)
            {
                throw new ArgumentNullException(nameof(unitSystem));
            }

            var unitInfos     = Info.GetUnitInfosFor(unitSystem.BaseUnits);
            var firstUnitInfo = unitInfos.FirstOrDefault(u => u.Value.Equals(BaseUnit));

            // for custom units, sometimes we don't find the base unit, this grabs the first off the list.
            if (Equals(firstUnitInfo, null))
            {
                firstUnitInfo = unitInfos.FirstOrDefault();
            }

            _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue));
            _unit  = firstUnitInfo?.Value ?? throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem));
        }
Пример #10
0
 public Area(double squaremeters)
 {
     _value = Convert.ToDouble(squaremeters);
     _unit  = BaseUnit;
 }
Пример #11
0
 Area(double numericValue, AreaUnit unit)
 {
     _value = numericValue;
     _unit  = unit;
 }
Пример #12
0
 /// <summary>
 ///     Creates the quantity with a value of 0 in the base unit SquareMeter.
 /// </summary>
 /// <remarks>
 ///     Windows Runtime Component requires a default constructor.
 /// </remarks>
 public Area()
 {
     _value = 0;
     _unit  = BaseUnit;
 }
Пример #13
0
 public void SetUnit(AreaUnit unit)
 {
     _unit = unit;
 }
Пример #14
0
 public AreaBuilder(Area area)
 {
     _squareMetres = area.SquareMetres;
     _value        = area.Value;
     _unit         = area.Unit;
 }
Пример #15
0
        private void Initialize()
        {
            this.initialized = true;
            BindingStringFormat.TryGet(this.binding, out this.bindingQuantityFormat);
            if (!string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat))
            {
                if (this.ValueFormat != null)
                {
                    this.errorText.AppendLine($"ValueFormat cannot be set when Binding.StringFormat is a unit format.");
                }

                if (this.StringFormat != null)
                {
                    this.errorText.AppendLine($"ValueFormat cannot be set when Binding.StringFormat is a unit format.");
                }
            }

            if (this.quantityFormat != null)
            {
                if (this.ValueFormat != null)
                {
                    this.errorText.AppendLine($"Both ValueFormat and StringFormat cannot be set.");
                }
            }
            else
            {
                if (this.unit != null && this.SymbolFormat != null)
                {
                    this.quantityFormat = FormatCache <AreaUnit> .GetOrCreate(this.ValueFormat, this.unit.Value, this.SymbolFormat.Value);
                }
            }

            if (this.unit == null)
            {
                var hasFmtUnit     = !string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat);
                var hasBindingUnit = !string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat);
                if (!hasFmtUnit && !hasBindingUnit)
                {
                    this.errorText.AppendLine($"Unit cannot be null.");
                    this.errorText.AppendLine($"Must be specified Explicitly or in StringFormat or in Binding.StringFormat.");
                }
                else if (hasFmtUnit && !hasBindingUnit)
                {
                    this.unit = this.quantityFormat.Unit;
                }
                else if (!hasFmtUnit && hasBindingUnit)
                {
                    this.unit = this.bindingQuantityFormat.Unit;
                }
                else
                {
                    if (this.quantityFormat.Unit != this.bindingQuantityFormat.Unit)
                    {
                        this.errorText.AppendLine($"Ambiguous units StringFormat: {this.quantityFormat.CompositeFormat} Binding.StringFormat: {this.bindingQuantityFormat.CompositeFormat}");
                    }

                    this.unit = this.quantityFormat.Unit;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat) &&
                    this.unit != this.quantityFormat.Unit)
                {
                    this.errorText.AppendLine($"Unit is set to '{this.unit}' but StringFormat is '{this.StringFormat.Replace("{0:", string.Empty).Replace("}", string.Empty)}'");
                }

                var hasBindingUnit = !string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat);
            }

            if (this.UnitInput == Wpf.UnitInput.SymbolRequired)
            {
                if (string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat))
                {
                    if (string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat))
                    {
                        if (this.unit == null)
                        {
                            this.errorText.AppendLine("UnitInput == SymbolRequired but no unit format is specified");
                        }
                        else if (this.SymbolFormat != null)
                        {
                            this.quantityFormat = FormatCache <AreaUnit> .GetOrCreate(this.ValueFormat, this.unit.Value, this.SymbolFormat.Value);

                            if (this.UnitInput == null)
                            {
                                this.UnitInput = Wpf.UnitInput.SymbolRequired;
                            }
                            else if (this.UnitInput == Wpf.UnitInput.ScalarOnly)
                            {
                                this.errorText.AppendLine("Cannot have ScalarOnly and SymbolFormat specified");
                            }
                        }
                        else
                        {
                            this.quantityFormat = FormatCache <AreaUnit> .GetOrCreate(this.ValueFormat, this.unit.Value);

                            if (this.UnitInput == null)
                            {
                                this.UnitInput = Wpf.UnitInput.ScalarOnly;
                            }
                            else if (this.UnitInput == Wpf.UnitInput.ScalarOnly)
                            {
                                this.errorText.AppendLine("Cannot have ScalarOnly and SymbolFormat specified");
                            }
                        }
                    }
                    else
                    {
                        this.quantityFormat = this.bindingQuantityFormat;
                    }
                }
            }

            if (this.UnitInput == null)
            {
                if (!string.IsNullOrEmpty(this.quantityFormat?.SymbolFormat) ||
                    !string.IsNullOrEmpty(this.bindingQuantityFormat?.SymbolFormat))
                {
                    this.UnitInput = Wpf.UnitInput.SymbolRequired;
                }
            }
        }