下面列出了java.text.NumberFormat#Style ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
@Override
public NumberFormat getCompactNumberInstance(Locale locale,
NumberFormat.Style style) {
locale = CalendarDataUtility.findRegionOverride(locale);
NumberFormatProvider nfp = getImpl(locale);
return nfp.getCompactNumberInstance(locale, style);
}
/**
* 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.");
}