internal void PerformAbbreviationMapping(Enum unitValue, IFormatProvider formatProvider, bool setAsDefault, bool allowAbbreviationLookup, [NotNull] params string[] abbreviations)
        {
            if (abbreviations == null)
            {
                throw new ArgumentNullException(nameof(abbreviations));
            }

            formatProvider = formatProvider ?? GlobalConfiguration.DefaultCulture;

            if (!_lookupsForCulture.TryGetValue(formatProvider, out var quantitiesForProvider))
            {
                quantitiesForProvider = _lookupsForCulture[formatProvider] = new UnitTypeToLookup();
            }

            var unitType = unitValue.GetType();

            if (!quantitiesForProvider.TryGetValue(unitType, out var unitToAbbreviations))
            {
                unitToAbbreviations = quantitiesForProvider[unitType] = new UnitValueAbbreviationLookup();
            }

            var unitValueAsInt = Convert.ToInt32(unitValue);

            foreach (var abbr in abbreviations)
            {
                unitToAbbreviations.Add(unitValueAsInt, abbr, setAsDefault, allowAbbreviationLookup);
            }
        }
Example #2
0
        private void PerformAbbreviationMapping(Type unitType, int unitValue, IFormatProvider formatProvider, bool setAsDefault, [NotNull] params string[] abbreviations)
        {
            if (!unitType.Wrap().IsEnum)
            {
                throw new ArgumentException("Must be an enum type.", nameof(unitType));
            }

            if (abbreviations == null)
            {
                throw new ArgumentNullException(nameof(abbreviations));
            }

            formatProvider = formatProvider ?? CultureInfo.CurrentUICulture;

            if (!_lookupsForCulture.TryGetValue(formatProvider, out var quantitiesForProvider))
            {
                quantitiesForProvider = _lookupsForCulture[formatProvider] = new UnitTypeToLookup();
            }

            if (!quantitiesForProvider.TryGetValue(unitType, out var unitToAbbreviations))
            {
                unitToAbbreviations = quantitiesForProvider[unitType] = new UnitValueAbbreviationLookup();
            }

            foreach (var abbr in abbreviations)
            {
                unitToAbbreviations.Add(unitValue, abbr, setAsDefault);
            }
        }
        internal bool TryGetUnitValueAbbreviationLookup(Type unitType, IFormatProvider formatProvider, out UnitValueAbbreviationLookup unitToAbbreviations)
        {
            unitToAbbreviations = null;

            formatProvider = formatProvider ?? GlobalConfiguration.DefaultCulture;

            if (!_lookupsForCulture.TryGetValue(formatProvider, out var quantitiesForProvider))
            {
                return(formatProvider != FallbackCulture?TryGetUnitValueAbbreviationLookup(unitType, FallbackCulture, out unitToAbbreviations) : false);
            }

            if (!quantitiesForProvider.TryGetValue(unitType, out unitToAbbreviations))
            {
                return(formatProvider != FallbackCulture?TryGetUnitValueAbbreviationLookup(unitType, FallbackCulture, out unitToAbbreviations) : false);
            }

            return(true);
        }