Beispiel #1
0
 /// <summary>
 /// Actively initializes the information for all currencies.
 /// </summary>
 /// <remarks>Use this method if you plan to use a lot of currencies in your program.
 /// <para>When most of currencies are expected to be used, it is recommeneded to initialize the information for all of them,
 /// saving time each time the first instance is accessed.</para></remarks>
 public static void InitializeAllCurrencies()
 {
     CurrencyIsoCode[] isoCodes = Enumeration.GetValues <CurrencyIsoCode>();
     using (var initializer = CurrencyInfo.CreateInitializer())
     {
         for (int i = 0; i < isoCodes.Length; i++)
         {
             CurrencyIsoCode isoCode = isoCodes[i];
             var             copy    = initializer;
             CurrencyCache.GetOrAdd(isoCode, () => init(isoCode, copy.Get));
         }
     }
 }
Beispiel #2
0
        public static bool TryGet(CurrencyIsoCode isoCode, out Currency currency)
        {
            bool tryGet = false;

            currency = null;

            if (Enumeration.CheckDefined(isoCode))
            {
                tryGet   = true;
                currency = CurrencyCache.GetOrAdd(isoCode, () => init(isoCode, _provider.Get));
            }
            RaiseIfObsolete(currency);
            return(tryGet);
        }
Beispiel #3
0
 public static IEnumerable <Currency> FindAll()
 {
     CurrencyIsoCode[] isoCodes = Enumeration.GetValues <CurrencyIsoCode>();
     using (var initializer = CurrencyInfo.CreateInitializer())
     {
         for (int i = 0; i < isoCodes.Length; i++)
         {
             CurrencyIsoCode isoCode  = isoCodes[i];
             var             copy     = initializer;
             Currency        currency = CurrencyCache.GetOrAdd(isoCode, () => init(isoCode, copy.Get));
             RaiseIfObsolete(isoCode);
             yield return(currency);
         }
     }
 }
Beispiel #4
0
        public static Currency Get(string threeLetterIsoCode)
        {
            Currency currency = CurrencyCache.GetOrAdd(threeLetterIsoCode, () =>
            {
                var isoCode = Code.ParseArgument(threeLetterIsoCode, nameof(threeLetterIsoCode));
                var built   = init(isoCode, _provider.Get);
                if (built == null)
                {
                    throw new MisconfiguredCurrencyException(isoCode);
                }
                return(built);
            });

            RaiseIfObsolete(currency);
            return(currency);
        }
Beispiel #5
0
        public static bool TryGet(string threeLetterIsoSymbol, out Currency currency)
        {
            bool tryGet = false;

            currency = null;
            CurrencyIsoCode?isoCode;

            if (threeLetterIsoSymbol != null && Enumeration.TryParse(threeLetterIsoSymbol.ToUpperInvariant(), out isoCode))
            {
                tryGet   = true;
                currency = CurrencyCache.GetOrAdd(isoCode.GetValueOrDefault(), () =>
                                                  init(isoCode.GetValueOrDefault(), _provider.Get));
            }
            RaiseIfObsolete(currency);
            return(tryGet);
        }
Beispiel #6
0
        public static Currency Get(CurrencyIsoCode isoCode)
        {
            Enumeration.AssertDefined(isoCode);

            Currency currency = CurrencyCache.GetOrAdd(isoCode, () =>
            {
                var built = init(isoCode, _provider.Get);
                if (built == null)
                {
                    throw new MisconfiguredCurrencyException(isoCode);
                }
                return(built);
            });

            RaiseIfObsolete(isoCode);
            return(currency);
        }
Beispiel #7
0
        /// <summary>
        /// Initialized static shortcuts and caches
        /// </summary>
        static Currency()
        {
            _provider = CurrencyInfo.CreateProvider();

            using (var initializer = CurrencyInfo.CreateInitializer())
            {
                Aud = init(CurrencyIsoCode.AUD, initializer.Get);
                CurrencyCache.Add(Aud);

                Cad = init(CurrencyIsoCode.CAD, initializer.Get);
                CurrencyCache.Add(Cad);

                Chf = init(CurrencyIsoCode.CHF, initializer.Get);
                CurrencyCache.Add(Chf);

                Cny = init(CurrencyIsoCode.CNY, initializer.Get);
                CurrencyCache.Add(Cny);

                Dkk = init(CurrencyIsoCode.DKK, initializer.Get);
                CurrencyCache.Add(Dkk);

                Eur = init(CurrencyIsoCode.EUR, initializer.Get);
                CurrencyCache.Add(Eur);

                Gbp = init(CurrencyIsoCode.GBP, initializer.Get);
                CurrencyCache.Add(Gbp);

                Hkd = init(CurrencyIsoCode.HKD, initializer.Get);
                CurrencyCache.Add(Hkd);

                Huf = init(CurrencyIsoCode.HUF, initializer.Get);
                CurrencyCache.Add(Huf);

                Inr = init(CurrencyIsoCode.INR, initializer.Get);
                CurrencyCache.Add(Inr);

                Jpy = init(CurrencyIsoCode.JPY, initializer.Get);
                CurrencyCache.Add(Jpy);

                Mxn = init(CurrencyIsoCode.MXN, initializer.Get);
                CurrencyCache.Add(Mxn);

                Myr = init(CurrencyIsoCode.MYR, initializer.Get);
                CurrencyCache.Add(Myr);

                Nok = init(CurrencyIsoCode.NOK, initializer.Get);
                CurrencyCache.Add(Nok);

                Nzd = init(CurrencyIsoCode.NZD, initializer.Get);
                CurrencyCache.Add(Nzd);

                Rub = init(CurrencyIsoCode.RUB, initializer.Get);
                CurrencyCache.Add(Rub);

                Sek = init(CurrencyIsoCode.SEK, initializer.Get);
                CurrencyCache.Add(Sek);

                Sgd = init(CurrencyIsoCode.SGD, initializer.Get);
                CurrencyCache.Add(Sgd);

                Thb = init(CurrencyIsoCode.THB, initializer.Get);
                CurrencyCache.Add(Thb);

                Usd = init(CurrencyIsoCode.USD, initializer.Get);
                CurrencyCache.Add(Usd);

                Zar = init(CurrencyIsoCode.ZAR, initializer.Get);
                CurrencyCache.Add(Zar);

                Xxx = init(CurrencyIsoCode.XXX, initializer.Get);
                CurrencyCache.Add(Xxx);

                Xts = init(CurrencyIsoCode.XTS, initializer.Get);
                CurrencyCache.Add(Xts);
            }

            Euro   = Eur;
            Dollar = Usd;
            Pound  = Gbp;
            None   = Xxx;
            Test   = Xts;
        }