Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(182)

Unified Diff: icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java

Issue 335150043: Refreshing Number Parsing: ICU4J Base URL: svn+icussh://source.icu-project.org/repos/icu/trunk/
Patch Set: Replying to Andy feedback round one. See commit message. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java
===================================================================
--- icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java (revision 40724)
+++ icu4j/main/classes/core/src/com/ibm/icu/number/NumberFormatterImpl.java (working copy)
@@ -25,12 +25,12 @@
import com.ibm.icu.util.MeasureUnit;
/**
- * This is the "brain" of the number formatting pipeline. It ties all the pieces together, taking in a MacroProps and a
- * DecimalQuantity and outputting a properly formatted number string.
+ * This is the "brain" of the number formatting pipeline. It ties all the pieces together, taking in a
+ * MacroProps and a DecimalQuantity and outputting a properly formatted number string.
*
* <p>
- * This class, as well as NumberPropertyMapper, could go into the impl package, but they depend on too many
- * package-private members of the public APIs.
+ * This class, as well as NumberPropertyMapper, could go into the impl package, but they depend on too
+ * many package-private members of the public APIs.
*/
class NumberFormatterImpl {
@@ -40,8 +40,13 @@
return new NumberFormatterImpl(microPropsGenerator);
}
- /** Builds and evaluates an "unsafe" MicroPropsGenerator, which is cheaper but can be used only once. */
- public static MicroProps applyStatic(MacroProps macros, DecimalQuantity inValue, NumberStringBuilder outString) {
+ /**
+ * Builds and evaluates an "unsafe" MicroPropsGenerator, which is cheaper but can be used only once.
+ */
+ public static MicroProps applyStatic(
+ MacroProps macros,
+ DecimalQuantity inValue,
+ NumberStringBuilder outString) {
MicroPropsGenerator microPropsGenerator = macrosToMicroGenerator(macros, false);
MicroProps micros = microPropsGenerator.processQuantity(inValue);
microsToString(micros, inValue, outString);
@@ -84,17 +89,17 @@
}
/**
- * Synthesizes the MacroProps into a MicroPropsGenerator. All information, including the locale, is encoded into the
- * MicroPropsGenerator, except for the quantity itself, which is left abstract and must be provided to the returned
- * MicroPropsGenerator instance.
+ * Synthesizes the MacroProps into a MicroPropsGenerator. All information, including the locale, is
+ * encoded into the MicroPropsGenerator, except for the quantity itself, which is left abstract and
+ * must be provided to the returned MicroPropsGenerator instance.
*
* @see MicroPropsGenerator
* @param macros
* The {@link MacroProps} to consume. This method does not mutate the MacroProps instance.
* @param safe
- * If true, the returned MicroPropsGenerator will be thread-safe. If false, the returned value will
- * <em>not</em> be thread-safe, intended for a single "one-shot" use only. Building the thread-safe
- * object is more expensive.
+ * If true, the returned MicroPropsGenerator will be thread-safe. If false, the returned
+ * value will <em>not</em> be thread-safe, intended for a single "one-shot" use only.
+ * Building the thread-safe object is more expensive.
*/
private static MicroPropsGenerator macrosToMicroGenerator(MacroProps macros, boolean safe) {
MicroProps micros = new MicroProps(safe);
@@ -109,7 +114,8 @@
boolean isPercent = isNoUnit && unitIsPercent(macros.unit);
boolean isPermille = isNoUnit && unitIsPermille(macros.unit);
boolean isCldrUnit = !isCurrency && !isNoUnit;
- boolean isAccounting = macros.sign == SignDisplay.ACCOUNTING || macros.sign == SignDisplay.ACCOUNTING_ALWAYS;
+ boolean isAccounting = macros.sign == SignDisplay.ACCOUNTING
+ || macros.sign == SignDisplay.ACCOUNTING_ALWAYS;
Currency currency = isCurrency ? (Currency) macros.unit : DEFAULT_CURRENCY;
UnitWidth unitWidth = UnitWidth.SHORT;
if (macros.unitWidth != null) {
@@ -134,13 +140,15 @@
} else if (!isCurrency || unitWidth == UnitWidth.FULL_NAME) {
patternStyle = NumberFormat.NUMBERSTYLE;
} else if (isAccounting) {
- // NOTE: Although ACCOUNTING and ACCOUNTING_ALWAYS are only supported in currencies right now,
+ // NOTE: Although ACCOUNTING and ACCOUNTING_ALWAYS are only supported in currencies right
+ // now,
// the API contract allows us to add support to other units in the future.
patternStyle = NumberFormat.ACCOUNTINGCURRENCYSTYLE;
} else {
patternStyle = NumberFormat.CURRENCYSTYLE;
}
- String pattern = NumberFormat.getPatternForStyleAndNumberingSystem(macros.loc, nsName, patternStyle);
+ String pattern = NumberFormat
+ .getPatternForStyleAndNumberingSystem(macros.loc, nsName, patternStyle);
ParsedPatternInfo patternInfo = PatternStringParser.parseToPatternInfo(pattern);
/////////////////////////////////////////////////////////////////////////////////////
@@ -247,7 +255,8 @@
// Lazily create PluralRules
rules = PluralRules.forLocale(macros.loc);
}
- chain = LongNameHandler.forMeasureUnit(macros.loc, macros.unit, unitWidth, rules, chain);
+ chain = LongNameHandler
+ .forMeasureUnit(macros.loc, macros.unit, macros.perUnit, unitWidth, rules, chain);
} else if (isCurrency && unitWidth == UnitWidth.FULL_NAME) {
if (rules == null) {
// Lazily create PluralRules
@@ -267,11 +276,15 @@
// Lazily create PluralRules
rules = PluralRules.forLocale(macros.loc);
}
- CompactType compactType = (macros.unit instanceof Currency && macros.unitWidth != UnitWidth.FULL_NAME)
- ? CompactType.CURRENCY
- : CompactType.DECIMAL;
- chain = ((CompactNotation) macros.notation).withLocaleData(macros.loc, nsName, compactType, rules,
- safe ? patternMod : null, chain);
+ CompactType compactType = (macros.unit instanceof Currency
+ && macros.unitWidth != UnitWidth.FULL_NAME) ? CompactType.CURRENCY
+ : CompactType.DECIMAL;
+ chain = ((CompactNotation) macros.notation).withLocaleData(macros.loc,
+ nsName,
+ compactType,
+ rules,
+ safe ? patternMod : null,
+ chain);
}
return chain;
@@ -289,7 +302,10 @@
* @param string
* The output string. Will be mutated.
*/
- private static void microsToString(MicroProps micros, DecimalQuantity quantity, NumberStringBuilder string) {
+ private static void microsToString(
+ MicroProps micros,
+ DecimalQuantity quantity,
+ NumberStringBuilder string) {
micros.rounding.apply(quantity);
if (micros.integerWidth.maxInt == -1) {
quantity.setIntegerLength(micros.integerWidth.minInt, Integer.MAX_VALUE);
@@ -309,7 +325,10 @@
}
}
- private static int writeNumber(MicroProps micros, DecimalQuantity quantity, NumberStringBuilder string) {
+ private static int writeNumber(
+ MicroProps micros,
+ DecimalQuantity quantity,
+ NumberStringBuilder string) {
int length = 0;
if (quantity.isInfinite()) {
length += string.insert(length, micros.symbols.getInfinity(), NumberFormat.Field.INTEGER);
@@ -322,9 +341,12 @@
length += writeIntegerDigits(micros, quantity, string);
// Add the decimal point
- if (quantity.getLowerDisplayMagnitude() < 0 || micros.decimal == DecimalSeparatorDisplay.ALWAYS) {
- length += string.insert(length, micros.useCurrency ? micros.symbols.getMonetaryDecimalSeparatorString()
- : micros.symbols.getDecimalSeparatorString(), NumberFormat.Field.DECIMAL_SEPARATOR);
+ if (quantity.getLowerDisplayMagnitude() < 0
+ || micros.decimal == DecimalSeparatorDisplay.ALWAYS) {
+ length += string.insert(length,
+ micros.useCurrency ? micros.symbols.getMonetaryDecimalSeparatorString()
+ : micros.symbols.getDecimalSeparatorString(),
+ NumberFormat.Field.DECIMAL_SEPARATOR);
}
// Add the fraction digits
@@ -334,23 +356,30 @@
return length;
}
- private static int writeIntegerDigits(MicroProps micros, DecimalQuantity quantity, NumberStringBuilder string) {
+ private static int writeIntegerDigits(
+ MicroProps micros,
+ DecimalQuantity quantity,
+ NumberStringBuilder string) {
int length = 0;
int integerCount = quantity.getUpperDisplayMagnitude() + 1;
for (int i = 0; i < integerCount; i++) {
// Add grouping separator
if (micros.grouping.groupAtPosition(i, quantity)) {
- length += string.insert(0, micros.useCurrency ? micros.symbols.getMonetaryGroupingSeparatorString()
- : micros.symbols.getGroupingSeparatorString(), NumberFormat.Field.GROUPING_SEPARATOR);
+ length += string.insert(0,
+ micros.useCurrency ? micros.symbols.getMonetaryGroupingSeparatorString()
+ : micros.symbols.getGroupingSeparatorString(),
+ NumberFormat.Field.GROUPING_SEPARATOR);
}
// Get and append the next digit value
byte nextDigit = quantity.getDigit(i);
if (micros.symbols.getCodePointZero() != -1) {
- length += string.insertCodePoint(0, micros.symbols.getCodePointZero() + nextDigit,
+ length += string.insertCodePoint(0,
+ micros.symbols.getCodePointZero() + nextDigit,
NumberFormat.Field.INTEGER);
} else {
- length += string.insert(0, micros.symbols.getDigitStringsLocal()[nextDigit],
+ length += string.insert(0,
+ micros.symbols.getDigitStringsLocal()[nextDigit],
NumberFormat.Field.INTEGER);
}
}
@@ -357,7 +386,10 @@
return length;
}
- private static int writeFractionDigits(MicroProps micros, DecimalQuantity quantity, NumberStringBuilder string) {
+ private static int writeFractionDigits(
+ MicroProps micros,
+ DecimalQuantity quantity,
+ NumberStringBuilder string) {
int length = 0;
int fractionCount = -quantity.getLowerDisplayMagnitude();
for (int i = 0; i < fractionCount; i++) {
@@ -367,7 +399,8 @@
length += string.appendCodePoint(micros.symbols.getCodePointZero() + nextDigit,
NumberFormat.Field.FRACTION);
} else {
- length += string.append(micros.symbols.getDigitStringsLocal()[nextDigit], NumberFormat.Field.FRACTION);
+ length += string.append(micros.symbols.getDigitStringsLocal()[nextDigit],
+ NumberFormat.Field.FRACTION);
}
}
return length;

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b