Beispiel #1
0
        public Enum Parse([NotNull] string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvider formatProvider = null)
        {
            if (unitAbbreviation == null)
            {
                throw new ArgumentNullException(nameof(unitAbbreviation));
            }
            unitAbbreviation = unitAbbreviation.Trim();

            if (!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
            {
                throw new UnitNotFoundException($"No abbreviations defined for unit type [{unitType}] for culture [{formatProvider}].");
            }

            var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation, ignoreCase: true);

            // Narrow the search if too many hits, for example Megabar "Mbar" and Millibar "mbar" need to be distinguished
            if (unitIntValues.Count > 1)
            {
                unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation, ignoreCase: false);
            }

            switch (unitIntValues.Count)
            {
            case 1:
                return((Enum)Enum.ToObject(unitType, unitIntValues[0]));

            case 0:
                throw new UnitNotFoundException($"Unit not found with abbreviation [{unitAbbreviation}] for unit type [{unitType}].");

            default:
                string unitsCsv = string.Join(", ", unitIntValues.Select(x => Enum.GetName(unitType, x)).ToArray());
                throw new AmbiguousUnitParseException(
                          $"Cannot parse \"{unitAbbreviation}\" since it could be either of these: {unitsCsv}");
            }
        }
Beispiel #2
0
        object Parse([NotNull] string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvider formatProvider = null)
        {
            if (unitAbbreviation == null)
            {
                throw new ArgumentNullException(nameof(unitAbbreviation));
            }
            unitAbbreviation = unitAbbreviation.Trim();

            if (!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
            {
                throw new UnitNotFoundException($"No abbreviations defined for unit type [{unitType}] for culture [{formatProvider}].");
            }

            var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation);

            switch (unitIntValues.Count)
            {
            case 1:
                return(unitIntValues[0]);

            case 0:
                throw new UnitNotFoundException($"Unit not found with abbreviation [{unitAbbreviation}] for unit type [{unitType}].");

            default:
                string unitsCsv = string.Join(", ", unitIntValues.Select(x => Enum.GetName(unitType, x)).ToArray());
                throw new AmbiguousUnitParseException(
                          $"Cannot parse \"{unitAbbreviation}\" since it could be either of these: {unitsCsv}");
            }
        }