Java Currency Code Get countryCodesForCurrency(String c)

Here you can find the source of countryCodesForCurrency(String c)

Description

country Codes For Currency

License

Open Source License

Declaration

private static List<String> countryCodesForCurrency(String c) 

Method Source Code

//package com.java2s;
/**//  www . ja  v a 2  s  . co  m
 * Copyright (C) 2015 https://github.com/ymanv
 *
 * This software is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Currency;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;

import java.util.Set;

public class Main {
    private static List<String> countryCodesForCurrency(String c) {
        Set<String> codes = new HashSet<>();

        if (isValidCode(c)) {
            for (Locale l : Locale.getAvailableLocales()) {
                try {
                    Currency currentCurrency = Currency.getInstance(l);

                    if (currentCurrency.getCurrencyCode().equals(c)) {
                        codes.add(l.getCountry());
                    }
                } catch (IllegalArgumentException e) {
                }
            }
        }

        return new ArrayList<>(codes);
    }

    public static boolean isValidCode(String cur) {
        try {
            Currency.getInstance(cur);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }
}

Related

  1. currencyFromCode(String currency)
  2. getAvaiableCurrencySymbols()
  3. getCurrency(final String code)
  4. getCurrencyDisplay(Object amount)