Skip to content

Commit

Permalink
#21 Fixed currency handler could try to enumerate duplicated currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
JPVenson committed Apr 29, 2022
1 parent 34a3b2a commit 58a7806
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions Morestachio/Formatter/Predefined/Accounting/CurrencyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading;
using Morestachio.Formatter.Framework.Attributes;

namespace Morestachio.Formatter.Predefined.Accounting;
Expand All @@ -14,32 +15,42 @@ public class CurrencyHandler
{
static CurrencyHandler()
{
DefaultHandler = new CurrencyHandler();
DefaultHandler.Currencies = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.GroupBy(e => e.LCID)
.Select(f =>
{
//this has to be tried because in some systems we can get invalid regions
try
{
return new RegionInfo(f.Key);
}
catch (Exception e)
IDictionary<string, Currency> GetSystemCurrencies()
{
return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.GroupBy(e => e.LCID)
.Select(f =>
{
return null;
}
})
.Where(e => e != null)
.GroupBy(e => e.CurrencyEnglishName)
.Select(f => f.First()).ToDictionary(e => e.ISOCurrencySymbol, e => new Currency(e.CurrencySymbol, e.ISOCurrencySymbol));
//this has to be tried because in some systems we can get invalid regions
try
{
return new RegionInfo(f.Key);
}
catch (Exception e)
{
return null;
}
})
.Where(e => e != null)
.GroupBy(e => e.ISOCurrencySymbol)
.Select(f => f.First())
.ToDictionary(e => e.ISOCurrencySymbol, e => new Currency(e.CurrencySymbol, e.ISOCurrencySymbol));
}

DefaultHandler = new CurrencyHandler(GetSystemCurrencies);
}

/// <summary>
///
/// </summary>
public CurrencyHandler()
public CurrencyHandler()
: this(() => new Dictionary<string, Currency>())
{
}

private CurrencyHandler(Func<IDictionary<string, Currency>> currencyFactory)
{
Currencies = new Dictionary<string, Currency>();
_currencies = new Lazy<IDictionary<string, Currency>>(currencyFactory);
ConversionFactors = new HashSet<CurrencyConversion>();
}

Expand All @@ -49,11 +60,16 @@ public CurrencyHandler()
[Description("The Default handler that contains all system wide known currencies")]
public static CurrencyHandler DefaultHandler { get; }

private Lazy<IDictionary<string, Currency>> _currencies;

/// <summary>
/// The list of all known currencies
/// </summary>
[Description("The list of all known currencies")]
public IDictionary<string, Currency> Currencies { get; private set; }
public IDictionary<string, Currency> Currencies
{
get { return _currencies.Value; }
}

/// <summary>
/// A list of known conversions for Currencies
Expand All @@ -67,9 +83,9 @@ public CurrencyHandler()
/// <returns></returns>
public CurrencyHandler Clone()
{
return new CurrencyHandler()
var currencies = new Dictionary<string, Currency>(Currencies);
return new CurrencyHandler(() => currencies)
{
Currencies = new Dictionary<string, Currency>(Currencies),
ConversionFactors = new HashSet<CurrencyConversion>(ConversionFactors)
};
}
Expand Down

0 comments on commit 58a7806

Please sign in to comment.