Java Code Examples for java.text.NumberFormat#Style

The following examples show how to use java.text.NumberFormat#Style . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocaleResources.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the compact number format patterns.
 * @param formatStyle the style for formatting a number
 * @return an array of compact number patterns
 */
@SuppressWarnings("unchecked")
public String[] getCNPatterns(NumberFormat.Style formatStyle) {

    Objects.requireNonNull(formatStyle);
    String[] compactNumberPatterns = null;
    removeEmptyReferences();
    String width = (formatStyle == NumberFormat.Style.LONG) ? "long" : "short";
    String cacheKey = width + "." + COMPACT_NUMBER_PATTERNS_CACHEKEY;
    ResourceReference data = cache.get(cacheKey);
    if (data == null || ((compactNumberPatterns
            = (String[]) data.get()) == null)) {
        ResourceBundle resource = localeData.getNumberFormatData(locale);
        compactNumberPatterns = (String[]) resource
                .getObject(width + ".CompactNumberPatterns");
        cache.put(cacheKey, new ResourceReference(cacheKey,
                (Object) compactNumberPatterns, referenceQueue));
    }
    return compactNumberPatterns;
}
 
Example 2
Source File: NumberFormatProviderImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new {@code NumberFormat} instance which formats
 * a number in its compact form for the specified
 * {@code locale} and {@code formatStyle}.
 *
 * @param locale the desired locale
 * @param formatStyle the style for formatting a number
 * @throws NullPointerException if {@code locale} or {@code formatStyle}
 *     is {@code null}
 * @throws IllegalArgumentException if {@code locale} isn't
 *     one of the locales returned from
 *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 *     getAvailableLocales()}.
 * @return a compact number formatter
 *
 * @see java.text.NumberFormat#getCompactNumberInstance(Locale,
 *                      NumberFormat.Style)
 * @since 12
 */
@Override
public NumberFormat getCompactNumberInstance(Locale locale,
        NumberFormat.Style formatStyle) {

    Objects.requireNonNull(locale);
    Objects.requireNonNull(formatStyle);

    // Check for region override
    Locale override = locale.getUnicodeLocaleType("nu") == null
            ? CalendarDataUtility.findRegionOverride(locale)
            : locale;

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    LocaleResources resource = adapter.getLocaleResources(override);

    String[] numberPatterns = resource.getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override);
    String[] cnPatterns = resource.getCNPatterns(formatStyle);

    // plural rules
    String pluralRules = rulesMap.getOrDefault(override.toString(),
            rulesMap.getOrDefault(override.getLanguage(), ""));

    CompactNumberFormat format = new CompactNumberFormat(numberPatterns[0],
            symbols, cnPatterns, pluralRules);
    return format;
}
 
Example 3
Source File: SPILocaleProviderAdapter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public NumberFormat getCompactNumberInstance(Locale locale,
                        NumberFormat.Style style) {
    locale = CalendarDataUtility.findRegionOverride(locale);
    NumberFormatProvider nfp = getImpl(locale);
    return nfp.getCompactNumberInstance(locale, style);
}
 
Example 4
Source File: NumberFormatProvider.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a new {@code NumberFormat} instance which formats
 * a number in its compact form for the specified
 * {@code locale} and {@code formatStyle}.
 *
 * @implSpec The default implementation of this method throws
 * {@link java.lang.UnsupportedOperationException
 * UnsupportedOperationException}. Overriding the implementation
 * of this method returns the compact number formatter instance
 * of the given {@code locale} with specified {@code formatStyle}.
 *
 * @param locale the desired locale
 * @param formatStyle the style for formatting a number
 * @throws NullPointerException if {@code locale} or {@code formatStyle}
 *     is {@code null}
 * @throws IllegalArgumentException if {@code locale} is not
 *     one of the locales returned from
 *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 *     getAvailableLocales()}.
 * @throws UnsupportedOperationException if the implementation does not
 *      support this method
 * @return a compact number formatter
 *
 * @see java.text.NumberFormat#getCompactNumberInstance(Locale,
 *                      NumberFormat.Style)
 * @since 12
 */
public NumberFormat getCompactNumberInstance(Locale locale,
        NumberFormat.Style formatStyle) {
    throw new UnsupportedOperationException(
            "The " + this.getClass().getName() + " should override this"
            + " method to return compact number format instance of "
            + locale + " locale and " + formatStyle + " style.");
}