Exemplo n.º 1
0
        /// <summary>
        /// Gets the <see cref="UnitInfo"/> whose <see cref="BaseUnits"/> is a subset of <paramref name="baseUnits"/>.
        /// </summary>
        /// <example>Length.Info.GetUnitInfoFor(unitSystemWithFootAsLengthUnit) returns <see cref="UnitInfo" /> for <see cref="LengthUnit.Foot" />.</example>
        /// <param name="baseUnits">The <see cref="BaseUnits"/> to check against.</param>
        /// <returns>The <see cref="UnitInfo"/> that has <see cref="BaseUnits"/> that is a subset of <paramref name="baseUnits"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="baseUnits"/> is null.</exception>
        /// <exception cref="InvalidOperationException">No unit was found that is a subset of <paramref name="baseUnits"/>.</exception>
        /// <exception cref="InvalidOperationException">More than one unit was found that is a subset of <paramref name="baseUnits"/>.</exception>
        public UnitInfo GetUnitInfoFor(BaseUnits baseUnits)
        {
            if (baseUnits is null)
            {
                throw new ArgumentNullException(nameof(baseUnits));
            }

            var matchingUnitInfos = GetUnitInfosFor(baseUnits)
                                    .Take(2)
                                    .ToArray();

            var firstUnitInfo = matchingUnitInfos.FirstOrDefault();

            if (firstUnitInfo == null)
            {
                throw new InvalidOperationException($"No unit was found that is a subset of {nameof(baseUnits)}");
            }

            if (matchingUnitInfos.Length > 1)
            {
                throw new InvalidOperationException($"More than one unit was found that is a subset of {nameof(baseUnits)}");
            }

            return(firstUnitInfo);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates an instance of the UnitInfo class.
 /// </summary>
 /// <param name="value">The enum value for this class, for example <see cref="LengthUnit.Meter"/>.</param>
 /// <param name="pluralName">The plural name of the unit, such as "Centimeters".</param>
 /// <param name="baseUnits">The <see cref="BaseUnits"/> for this unit.</param>
 public UnitInfo([NotNull] Enum value, [NotNull] string pluralName, [NotNull] BaseUnits baseUnits)
 {
     Value      = value ?? throw new ArgumentNullException(nameof(value));
     Name       = value.ToString();
     PluralName = pluralName;
     BaseUnits  = baseUnits ?? throw new ArgumentNullException(nameof(baseUnits));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets an <see cref="IEnumerable{T}"/> of <see cref="UnitInfo"/> that have <see cref="BaseUnits"/> that is a subset of <paramref name="baseUnits"/>.
        /// </summary>
        /// <param name="baseUnits">The <see cref="BaseUnits"/> to check against.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="UnitInfo"/> that have <see cref="BaseUnits"/> that is a subset of <paramref name="baseUnits"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="baseUnits"/> is null.</exception>
        public IEnumerable <UnitInfo> GetUnitInfosFor(BaseUnits baseUnits)
        {
            if (baseUnits is null)
            {
                throw new ArgumentNullException(nameof(baseUnits));
            }

            return(UnitInfos.Where((unitInfo) => unitInfo.BaseUnits.IsSubsetOf(baseUnits)));
        }
Exemplo n.º 4
0
        public bool Equals(UnitSystem other)
        {
            if (other is null)
            {
                return(false);
            }

            return(BaseUnits.Equals(other.BaseUnits));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates an instance of a unit system with the specified base units.
        /// </summary>
        /// <param name="baseUnits">The base units for the unit system.</param>
        public UnitSystem(BaseUnits baseUnits)
        {
            if (baseUnits is null)
            {
                throw new ArgumentNullException(nameof(baseUnits));
            }
            if (!baseUnits.IsFullyDefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }

            BaseUnits = baseUnits;
        }
Exemplo n.º 6
0
        public bool Equals(BaseUnits other)
        {
            if (other is null)
            {
                return(false);
            }

            return(Length == other.Length &&
                   Mass == other.Mass &&
                   Time == other.Time &&
                   Current == other.Current &&
                   Temperature == other.Temperature &&
                   Amount == other.Amount &&
                   LuminousIntensity == other.LuminousIntensity);
        }
Exemplo n.º 7
0
        public UnitSystem(BaseUnits baseUnits)
        {
            if (baseUnits is null)
            {
                throw new ArgumentNullException(nameof(baseUnits));
            }
            if (!baseUnits.IsFullyDefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }

            // default implementation (does not fix the prefixed entities matching issue)
            _systemUnits = new Lazy <UnitSystemInfo?[]>(() => Quantity.Infos.Select(i =>
            {
                var commonUnits = i.GetUnitInfosFor(baseUnits).ToArray();
                var defaultInfo = commonUnits.FirstOrDefault(); // respecting the original behavior
                return(defaultInfo == null ? null : new UnitSystemInfo(defaultInfo, commonUnits));
            }).ToArray());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates an instance of a unit system with the specified base units.
        /// </summary>
        /// <param name="baseUnits">The base units for the unit system.</param>
        public UnitSystem(BaseUnits baseUnits)
        {
            if (baseUnits is null)
            {
                throw new ArgumentNullException(nameof(baseUnits));
            }

            if (baseUnits.Length == LengthUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }
            if (baseUnits.Mass == MassUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }
            if (baseUnits.Time == DurationUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }
            if (baseUnits.Current == ElectricCurrentUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }
            if (baseUnits.Temperature == TemperatureUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }
            if (baseUnits.Amount == AmountOfSubstanceUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }
            if (baseUnits.LuminousIntensity == LuminousIntensityUnit.Undefined)
            {
                throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
            }

            BaseUnits = baseUnits;
        }
Exemplo n.º 9
0
 /// <inheritdoc />
 public bool Equals(UnitSystem?other)
 {
     return(other is not null && BaseUnits.Equals(other.BaseUnits));
 }