Search in sources :

Example 1 with CurrencyMetaInfo

use of android.icu.text.CurrencyMetaInfo in project j2objc by google.

the class CurrencyTest method testCurrencyMetaInfoRangesWithLongs.

@Test
public void testCurrencyMetaInfoRangesWithLongs() {
    CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
    assertNotNull("have metainfo", metainfo);
    // must be capitalized
    CurrencyFilter filter = CurrencyFilter.onRegion("DE");
    List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
    // must be capitalized
    CurrencyFilter filter_br = CurrencyFilter.onRegion("BR");
    List<CurrencyInfo> currenciesInBrazil = metainfo.currencyInfo(filter_br);
    logln("currencies Germany: " + currenciesInGermany.size());
    logln("currencies Brazil: " + currenciesInBrazil.size());
    long demFirstDate = Long.MIN_VALUE;
    long demLastDate = Long.MAX_VALUE;
    long eurFirstDate = Long.MIN_VALUE;
    CurrencyInfo demInfo = null;
    for (CurrencyInfo info : currenciesInGermany) {
        logln(info.toString());
        if (info.code.equals("DEM")) {
            demInfo = info;
            demFirstDate = info.from;
            demLastDate = info.to;
        } else if (info.code.equals("EUR")) {
            eurFirstDate = info.from;
        }
    }
    // the Euro and Deutschmark overlapped for several years
    assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
    // demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
    long demLastDatePlus1ms = demLastDate + 1;
    assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
    // both available for start of euro
    assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
    // but not one millisecond before the start of the first day
    long eurFirstDateMinus1ms = eurFirstDate - 1;
    assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
    // Deutschmark available from first millisecond on
    assertEquals("Millisecond of DEM Big Bang", 1, metainfo.currencyInfo(CurrencyFilter.onDate(demFirstDate).withRegion("DE")).size());
    assertEquals("From Deutschmark to Euro", 2, metainfo.currencyInfo(CurrencyFilter.onDateRange(demFirstDate, eurFirstDate).withRegion("DE")).size());
    assertEquals("all Tender for Brazil", 7, metainfo.currencyInfo(CurrencyFilter.onTender().withRegion("BR")).size());
    assertTrue("No legal tender", demInfo.isTender());
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo) CurrencyInfo(android.icu.text.CurrencyMetaInfo.CurrencyInfo) CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter) Test(org.junit.Test)

Example 2 with CurrencyMetaInfo

use of android.icu.text.CurrencyMetaInfo in project j2objc by google.

the class CurrencyTest method TestWithTender.

@Test
public void TestWithTender() {
    CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
    if (metainfo == null) {
        errln("Unable to get CurrencyMetaInfo instance.");
        return;
    }
    CurrencyMetaInfo.CurrencyFilter filter = CurrencyMetaInfo.CurrencyFilter.onRegion("CH");
    List<String> currencies = metainfo.currencies(filter);
    assertTrue("More than one currency for switzerland", currencies.size() > 1);
    assertEquals("With tender", Arrays.asList(new String[] { "CHF", "CHE", "CHW" }), metainfo.currencies(filter.withTender()));
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo) CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter) Test(org.junit.Test)

Example 3 with CurrencyMetaInfo

use of android.icu.text.CurrencyMetaInfo in project j2objc by google.

the class Currency method isAvailable.

/**
 * Queries if the given ISO 4217 3-letter code is available on the specified date range.
 * <p>
 * Note: For checking availability of a currency on a specific date, specify the date on both <code>from</code> and
 * <code>to</code>. When both <code>from</code> and <code>to</code> are null, this method checks if the specified
 * currency is available all time.
 *
 * @param code
 *            The ISO 4217 3-letter code.
 * @param from
 *            The lower bound of the date range, inclusive. When <code>from</code> is null, check the availability
 *            of the currency any date before <code>to</code>
 * @param to
 *            The upper bound of the date range, inclusive. When <code>to</code> is null, check the availability of
 *            the currency any date after <code>from</code>
 * @return true if the given ISO 4217 3-letter code is supported on the specified date range.
 * @throws IllegalArgumentException when <code>to</code> is before <code>from</code>.
 */
public static boolean isAvailable(String code, Date from, Date to) {
    if (!isAlpha3Code(code)) {
        return false;
    }
    if (from != null && to != null && from.after(to)) {
        throw new IllegalArgumentException("To is before from");
    }
    code = code.toUpperCase(Locale.ENGLISH);
    boolean isKnown = getAllCurrenciesAsSet().contains(code);
    if (isKnown == false) {
        return false;
    } else if (from == null && to == null) {
        return true;
    }
    // If caller passed a date range, we cannot rely solely on the cache
    CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
    List<String> allActive = info.currencies(CurrencyFilter.onDateRange(from, to).withCurrency(code));
    return allActive.contains(code);
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo)

Example 4 with CurrencyMetaInfo

use of android.icu.text.CurrencyMetaInfo in project j2objc by google.

the class Currency method getAllCurrenciesAsSet.

private static synchronized Set<String> getAllCurrenciesAsSet() {
    Set<String> all = (ALL_CODES_AS_SET == null) ? null : ALL_CODES_AS_SET.get();
    if (all == null) {
        CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
        all = Collections.unmodifiableSet(new HashSet<String>(info.currencies(CurrencyFilter.all())));
        ALL_CODES_AS_SET = new SoftReference<Set<String>>(all);
    }
    return all;
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo) HashSet(java.util.HashSet) Set(java.util.Set) HashSet(java.util.HashSet)

Example 5 with CurrencyMetaInfo

use of android.icu.text.CurrencyMetaInfo in project j2objc by google.

the class Currency method getDefaultFractionDigits.

/**
 * Returns the number of the number of fraction digits that should
 * be displayed for this currency with Usage.
 * @param Usage the usage of currency(Standard or Cash)
 * @return a non-negative number of fraction digits to be
 * displayed
 */
public int getDefaultFractionDigits(CurrencyUsage Usage) {
    CurrencyMetaInfo info = CurrencyMetaInfo.getInstance();
    CurrencyDigits digits = info.currencyDigits(subType, Usage);
    return digits.fractionDigits;
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo) CurrencyDigits(android.icu.text.CurrencyMetaInfo.CurrencyDigits)

Aggregations

CurrencyMetaInfo (android.icu.text.CurrencyMetaInfo)10 CurrencyFilter (android.icu.text.CurrencyMetaInfo.CurrencyFilter)4 Test (org.junit.Test)4 CurrencyInfo (android.icu.text.CurrencyMetaInfo.CurrencyInfo)3 CurrencyDigits (android.icu.text.CurrencyMetaInfo.CurrencyDigits)2 HashSet (java.util.HashSet)2 DateFormat (android.icu.text.DateFormat)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 Date (java.util.Date)1 Set (java.util.Set)1