mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8177552: Compact Number Formatting support
Reviewed-by: naoto, rriggs
This commit is contained in:
parent
f3646ad79b
commit
4a28e27fd2
28 changed files with 5157 additions and 196 deletions
|
@ -43,6 +43,7 @@ package sun.util.locale.provider;
|
|||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.text.MessageFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
|
@ -88,6 +89,7 @@ public class LocaleResources {
|
|||
private static final String ZONE_IDS_CACHEKEY = "ZID";
|
||||
private static final String CALENDAR_NAMES = "CALN.";
|
||||
private static final String NUMBER_PATTERNS_CACHEKEY = "NP";
|
||||
private static final String COMPACT_NUMBER_PATTERNS_CACHEKEY = "CNP";
|
||||
private static final String DATE_TIME_PATTERN = "DTP.";
|
||||
|
||||
// TimeZoneNamesBundle exemplar city prefix
|
||||
|
@ -478,6 +480,32 @@ public class LocaleResources {
|
|||
return numberPatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the FormatData resource bundle of this LocaleResources.
|
||||
* The FormatData should be used only for accessing extra
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -40,12 +40,14 @@
|
|||
|
||||
package sun.util.locale.provider;
|
||||
|
||||
import java.text.CompactNumberFormat;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.spi.NumberFormatProvider;
|
||||
import java.util.Currency;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -225,6 +227,49 @@ public class NumberFormatProviderImpl extends NumberFormatProvider implements Av
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
CompactNumberFormat format = new CompactNumberFormat(numberPatterns[0],
|
||||
symbols, cnPatterns);
|
||||
return format;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAvailableLanguageTags() {
|
||||
return langtags;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue