
net.yapbam.currency.CountryCurrencyMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yapbam-commons Show documentation
Show all versions of yapbam-commons Show documentation
Commons Yapbam classes used by desktop and Android versions.
package net.yapbam.currency;
import java.util.Collections;
import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/** A map between countries and currencies.
*
This map is based on java internal data.
*
This means that its content is related to the java version used, not to an "always uptodate" Internet source.
* @author Jean-Marc Astesana (License GPL)
*/
public class CountryCurrencyMap {
public static final CountryCurrencyMap INSTANCE = new CountryCurrencyMap();
/* The following information is related to an online source of data.
* After thinking about it, it seems a better solution to use internal java data.
* The pro are: The data is guaranteed to be available (it is part of the JVM).
* The cons are; The data is not necessary upto date.
private static final int CURRENCY_COL_INDEX = 14;
private static final int COUNTRY_COL_INDEX = 2;
private static final String TABLE_URL = "https://raw.github.com/datasets/country-codes/master/data/country-codes.csv";
*/
private Map countryToCurrency;
private Map> currencyToCountries;
private CountryCurrencyMap() {
this.countryToCurrency = new HashMap();
this.currencyToCountries = new HashMap>();
String[] isoCountries = Locale.getISOCountries();
for (String isoCountry : isoCountries) {
Currency currency = Currency.getInstance(new Locale("", isoCountry));
if (currency==null) {
this.countryToCurrency.put(isoCountry, null);
} else {
String code = currency.getCurrencyCode();
this.countryToCurrency.put(isoCountry, code);
Set countries = currencyToCountries.get(code);
if (countries==null) {
countries = new TreeSet();
currencyToCountries.put(code, countries);
}
countries.add(isoCountry);
}
}
}
/** Gets the currency used in a country.
* @param country The ISO 3166 code of a country (example: FR).
* @return The ISO 4217 code of the currency, or null if the country is unknown or has no currency.
*/
public String getCurrency(String country) {
return this.countryToCurrency.get(country);
}
/** Gets the countries that use a currency.
* @param currencyCode The ISO 4217 code of the currency (example: EUR).
* @return An unmodifiable set of ISO 3166 country codes, or null if the currency is unknown.
*/
public Set getCountries(String currencyCode) {
Set set = this.currencyToCountries.get(currencyCode);
return set==null ? set : Collections.unmodifiableSet(set);
}
/** Gets the known countries.
* @return An unmodifiable set of ISO 3166 country codes.
*/
public Set getCountries() {
return Collections.unmodifiableSet(this.countryToCurrency.keySet());
}
/** Gets the known currencies.
* @return An unmodifiable set of ISO 4217 currency codes.
*/
public Set getCurrencies() {
return Collections.unmodifiableSet(this.currencyToCountries.keySet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy