Search in sources :

Example 16 with Currency

use of java.util.Currency in project hibernate-orm by hibernate.

the class MonetaryAmountUserType method nullSafeGet.

public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
    BigDecimal amt = StandardBasicTypes.BIG_DECIMAL.nullSafeGet(rs, names[0], session);
    Currency cur = StandardBasicTypes.CURRENCY.nullSafeGet(rs, names[1], session);
    if (amt == null)
        return null;
    return new MonetaryAmount(amt, cur);
}
Also used : Currency(java.util.Currency) BigDecimal(java.math.BigDecimal)

Example 17 with Currency

use of java.util.Currency in project jdk8u_jdk by JetBrains.

the class CurrencyTest method testLocaleMapping.

static void testLocaleMapping() {
    // very basic test: most countries have their own currency, and then
    // their currency code is an extension of their country code.
    Locale[] locales = Locale.getAvailableLocales();
    int goodCountries = 0;
    int ownCurrencies = 0;
    for (int i = 0; i < locales.length; i++) {
        Locale locale = locales[i];
        if (locale.getCountry().length() == 0) {
            boolean gotException = false;
            try {
                Currency.getInstance(locale);
            } catch (IllegalArgumentException e) {
                gotException = true;
            }
            if (!gotException) {
                throw new RuntimeException("didn't get specified exception");
            }
        } else {
            goodCountries++;
            Currency currency = Currency.getInstance(locale);
            if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
                ownCurrencies++;
            }
        }
    }
    System.out.println("Countries tested: " + goodCountries + ", own currencies: " + ownCurrencies);
    if (ownCurrencies < (goodCountries / 2 + 1)) {
        throw new RuntimeException("suspicious: not enough countries have their own currency.");
    }
    // check a few countries that don't change their currencies too often
    String[] country1 = { "US", "CA", "JP", "CN", "SG", "CH" };
    String[] currency1 = { "USD", "CAD", "JPY", "CNY", "SGD", "CHF" };
    for (int i = 0; i < country1.length; i++) {
        checkCountryCurrency(country1[i], currency1[i]);
    }
    /*
        * check currency changes
        * In current implementation, there is no data of old currency and transition date at jdk/src/share/classes/java/util/CurrencyData.properties.
        * So, all the switch data arrays are empty. In the future, if data of old currency and transition date are necessary for any country, the
        * arrays here can be updated so that the program can check the currency switch.
        */
    String[] switchOverCtry = {};
    String[] switchOverOld = {};
    String[] switchOverNew = {};
    String[] switchOverTZ = {};
    int[] switchOverYear = {};
    int[] switchOverMonth = {};
    int[] switchOverDay = {};
    for (int i = 0; i < switchOverCtry.length; i++) {
        TimeZone.setDefault(TimeZone.getTimeZone(switchOverTZ[i]));
        Calendar date = new GregorianCalendar(switchOverYear[i], switchOverMonth[i], switchOverDay[i]);
        long switchOver = date.getTime().getTime();
        boolean switchedOver = System.currentTimeMillis() >= switchOver;
        checkCountryCurrency(switchOverCtry[i], switchedOver ? switchOverNew[i] : switchOverOld[i]);
    }
    // check a country code which doesn't have a currency
    checkCountryCurrency("AQ", null);
    // check an invalid country code
    boolean gotException = false;
    try {
        Currency.getInstance(new Locale("", "EU"));
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        throw new RuntimeException("didn't get specified exception.");
    }
}
Also used : Locale(java.util.Locale) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Currency(java.util.Currency)

Example 18 with Currency

use of java.util.Currency in project jdk8u_jdk by JetBrains.

the class CurrencyTest method testSerialization.

static void testSerialization() throws Exception {
    Currency currency1 = Currency.getInstance("DEM");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oStream = new ObjectOutputStream(baos);
    oStream.writeObject(currency1);
    oStream.flush();
    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream iStream = new ObjectInputStream(bais);
    Currency currency2 = (Currency) iStream.readObject();
    if (currency1 != currency2) {
        throw new RuntimeException("serialization breaks class invariant");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Currency(java.util.Currency) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 19 with Currency

use of java.util.Currency in project jdk8u_jdk by JetBrains.

the class Bug4512215 method testCountryCurrency.

private static void testCountryCurrency(String country, String currencyCode, int digits) {
    testCurrencyDefined(currencyCode, digits);
    Currency currency = Currency.getInstance(new Locale("", country));
    if (!currency.getCurrencyCode().equals(currencyCode)) {
        throw new RuntimeException("[" + country + "] expected: " + currencyCode + "; got: " + currency.getCurrencyCode());
    }
}
Also used : Locale(java.util.Locale) Currency(java.util.Currency)

Example 20 with Currency

use of java.util.Currency in project jdk8u_jdk by JetBrains.

the class CurrencyTest method checkCountryCurrency.

static void checkCountryCurrency(String countryCode, String expected) {
    Locale locale = new Locale("", countryCode);
    Currency currency = Currency.getInstance(locale);
    String code = (currency != null) ? currency.getCurrencyCode() : null;
    if (!(expected == null ? code == null : expected.equals(code))) {
        throw new RuntimeException("Wrong currency for " + locale.getDisplayCountry() + ": expected " + expected + ", got " + code);
    }
}
Also used : Locale(java.util.Locale) Currency(java.util.Currency)

Aggregations

Currency (java.util.Currency)58 Locale (java.util.Locale)21 BigDecimal (java.math.BigDecimal)8 DecimalFormat (java.text.DecimalFormat)7 DecimalFormatSymbols (java.text.DecimalFormatSymbols)7 NumberFormat (java.text.NumberFormat)6 ChoiceFormat (java.text.ChoiceFormat)3 HashSet (java.util.HashSet)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 ArrayList (java.util.ArrayList)2 Exchange (org.apache.camel.Exchange)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1