Search in sources :

Example 1 with CurrencyFilter

use of android.icu.text.CurrencyMetaInfo.CurrencyFilter 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 CurrencyFilter

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

the class Currency method getAvailableCurrencyCodes.

/**
 * Returns an array of Strings which contain the currency
 * identifiers that are valid for the given locale on the
 * given date.  If there are no such identifiers, returns null.
 * Returned identifiers are in preference order.
 * @param loc the locale for which to retrieve currency codes.
 * @param d the date for which to retrieve currency codes for the given locale.
 * @return The array of ISO currency codes.
 */
public static String[] getAvailableCurrencyCodes(ULocale loc, Date d) {
    String region = ULocale.getRegionForSupplementalData(loc, false);
    CurrencyFilter filter = CurrencyFilter.onDate(d).withRegion(region);
    List<String> list = getTenderCurrencies(filter);
    // the test assumed it did.  Kept the behavior and amended the spec.
    if (list.isEmpty()) {
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter)

Example 3 with CurrencyFilter

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

the class Currency method getKeywordValuesForLocale.

// end registry stuff
/**
 * Given a key and a locale, returns an array of values for the key for which data
 * exists.  If commonlyUsed is true, these are the values that typically are used
 * with this locale, otherwise these are all values for which data exists.
 * This is a common service API.
 * <p>
 * The only supported key is "currency", other values return an empty array.
 * <p>
 * Currency information is based on the region of the locale.  If the locale does not
 * indicate a region, {@link ULocale#addLikelySubtags(ULocale)} is used to infer a region,
 * except for the 'und' locale.
 * <p>
 * If commonlyUsed is true, only the currencies known to be in use as of the current date
 * are returned.  When there are more than one, these are returned in preference order
 * (typically, this occurs when a country is transitioning to a new currency, and the
 * newer currency is preferred), see
 * <a href="http://unicode.org/reports/tr35/#Supplemental_Currency_Data">Unicode TR#35 Sec. C1</a>.
 * If commonlyUsed is false, all currencies ever used in any locale are returned, in no
 * particular order.
 *
 * @param key           key whose values to look up.  the only recognized key is "currency"
 * @param locale        the locale
 * @param commonlyUsed  if true, return only values that are currently used in the locale.
 *                      Otherwise returns all values.
 * @return an array of values for the given key and the locale.  If there is no data, the
 *   array will be empty.
 */
public static final String[] getKeywordValuesForLocale(String key, ULocale locale, boolean commonlyUsed) {
    // The only keyword we recognize is 'currency'
    if (!"currency".equals(key)) {
        return EMPTY_STRING_ARRAY;
    }
    if (!commonlyUsed) {
        // Behavior change from 4.3.3, no longer sort the currencies
        return getAllTenderCurrencies().toArray(new String[0]);
    }
    // which we don't want.
    if (UND.equals(locale)) {
        return EMPTY_STRING_ARRAY;
    }
    String prefRegion = ULocale.getRegionForSupplementalData(locale, true);
    CurrencyFilter filter = CurrencyFilter.now().withRegion(prefRegion);
    // currencies are in region's preferred order when we're filtering on region, which
    // matches our spec
    List<String> result = getTenderCurrencies(filter);
    // No fallback anymore (change from 4.3.3)
    if (result.size() == 0) {
        return EMPTY_STRING_ARRAY;
    }
    return result.toArray(new String[result.size()]);
}
Also used : CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter)

Example 4 with CurrencyFilter

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

the class Currency method getAllTenderCurrencies.

/*
     * Returns an unmodifiable String list including all known tender currency codes.
     */
private static synchronized List<String> getAllTenderCurrencies() {
    List<String> all = (ALL_TENDER_CODES == null) ? null : ALL_TENDER_CODES.get();
    if (all == null) {
        // Filter out non-tender currencies which have "from" date set to 9999-12-31
        // CurrencyFilter has "to" value set to 9998-12-31 in order to exclude them
        // CurrencyFilter filter = CurrencyFilter.onDateRange(null, new Date(253373299200000L));
        CurrencyFilter filter = CurrencyFilter.all();
        all = Collections.unmodifiableList(getTenderCurrencies(filter));
        ALL_TENDER_CODES = new SoftReference<List<String>>(all);
    }
    return all;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter)

Example 5 with CurrencyFilter

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

the class CurrencyTest method TestCurrMetaInfoBaseClass.

/*
     *
     * Test methods of base class CurrencyMetaInfo. ICU4J only creates subclasses,
     * never an instance of the base class.
     */
@Test
public void TestCurrMetaInfoBaseClass() {
    CurrencyFilter usFilter = CurrencyFilter.onRegion("US");
    assertEquals("Empty list expected", 0, tcurrMetaInfo.currencyInfo(usFilter).size());
    assertEquals("Empty list expected", 0, tcurrMetaInfo.currencies(usFilter).size());
    assertEquals("Empty list expected", 0, tcurrMetaInfo.regions(usFilter).size());
    assertEquals("Iso format for digits expected", "CurrencyDigits(fractionDigits='2',roundingIncrement='0')", tcurrMetaInfo.currencyDigits("isoCode").toString());
}
Also used : CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter) Test(org.junit.Test)

Aggregations

CurrencyFilter (android.icu.text.CurrencyMetaInfo.CurrencyFilter)7 Test (org.junit.Test)4 CurrencyMetaInfo (android.icu.text.CurrencyMetaInfo)3 CurrencyInfo (android.icu.text.CurrencyMetaInfo.CurrencyInfo)3 DateFormat (android.icu.text.DateFormat)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1