ICU-13524 Reformatting ICU4J number files with 4 spaces.

X-SVN-Rev: 40750
This commit is contained in:
Shane Carr 2017-12-23 01:20:31 +00:00
parent 56574d1042
commit fcc198868e
3 changed files with 110 additions and 46 deletions

View File

@ -78,10 +78,15 @@ public class LongNameHandler implements MicroPropsGenerator {
// NOTE: outArray MUST have at least ARRAY_LENGTH entries. No bounds checking is performed.
private static void getMeasureData(ULocale locale, MeasureUnit unit, UnitWidth width, String[] outArray) {
private static void getMeasureData(
ULocale locale,
MeasureUnit unit,
UnitWidth width,
String[] outArray) {
PluralTableSink sink = new PluralTableSink(outArray);
ICUResourceBundle resource;
resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, locale);
resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME,
locale);
StringBuilder key = new StringBuilder();
key.append("units");
if (width == UnitWidth.NARROW) {
@ -112,7 +117,8 @@ public class LongNameHandler implements MicroPropsGenerator {
// Example pattern from data: "{0} {1}"
// Example output after find-and-replace: "{0} US dollars"
simpleFormat = simpleFormat.replace("{1}", longName);
// String compiled = SimpleFormatterImpl.compileToStringMinMaxArguments(simpleFormat, sb, 1, 1);
// String compiled = SimpleFormatterImpl.compileToStringMinMaxArguments(simpleFormat, sb, 1,
// 1);
// SimpleModifier mod = new SimpleModifier(compiled, Field.CURRENCY, false);
outArray[index] = simpleFormat;
}
@ -120,7 +126,8 @@ public class LongNameHandler implements MicroPropsGenerator {
private static String getPerUnitFormat(ULocale locale, UnitWidth width) {
ICUResourceBundle resource;
resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, locale);
resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME,
locale);
StringBuilder key = new StringBuilder();
key.append("units");
if (width == UnitWidth.NARROW) {
@ -132,7 +139,8 @@ public class LongNameHandler implements MicroPropsGenerator {
try {
return resource.getStringWithFallback(key.toString());
} catch (MissingResourceException e) {
throw new IllegalArgumentException("Could not find x-per-y format for " + locale + ", width " + width);
throw new IllegalArgumentException(
"Could not find x-per-y format for " + locale + ", width " + width);
}
}
@ -144,14 +152,19 @@ public class LongNameHandler implements MicroPropsGenerator {
private final PluralRules rules;
private final MicroPropsGenerator parent;
private LongNameHandler(Map<StandardPlural, SimpleModifier> modifiers, PluralRules rules,
private LongNameHandler(
Map<StandardPlural, SimpleModifier> modifiers,
PluralRules rules,
MicroPropsGenerator parent) {
this.modifiers = modifiers;
this.rules = rules;
this.parent = parent;
}
public static LongNameHandler forCurrencyLongNames(ULocale locale, Currency currency, PluralRules rules,
public static LongNameHandler forCurrencyLongNames(
ULocale locale,
Currency currency,
PluralRules rules,
MicroPropsGenerator parent) {
String[] simpleFormats = new String[ARRAY_LENGTH];
getCurrencyLongNameData(locale, currency, simpleFormats);
@ -162,8 +175,13 @@ public class LongNameHandler implements MicroPropsGenerator {
return new LongNameHandler(modifiers, rules, parent);
}
public static LongNameHandler forMeasureUnit(ULocale locale, MeasureUnit unit, MeasureUnit perUnit, UnitWidth width,
PluralRules rules, MicroPropsGenerator parent) {
public static LongNameHandler forMeasureUnit(
ULocale locale,
MeasureUnit unit,
MeasureUnit perUnit,
UnitWidth width,
PluralRules rules,
MicroPropsGenerator parent) {
if (perUnit != null) {
// Compound unit: first try to simplify (e.g., meters per second is its own unit).
MeasureUnit simplified = MeasureUnit.resolveUnitPerUnit(unit, perUnit);
@ -185,8 +203,13 @@ public class LongNameHandler implements MicroPropsGenerator {
return new LongNameHandler(modifiers, rules, parent);
}
private static LongNameHandler forCompoundUnit(ULocale locale, MeasureUnit unit, MeasureUnit perUnit,
UnitWidth width, PluralRules rules, MicroPropsGenerator parent) {
private static LongNameHandler forCompoundUnit(
ULocale locale,
MeasureUnit unit,
MeasureUnit perUnit,
UnitWidth width,
PluralRules rules,
MicroPropsGenerator parent) {
String[] primaryData = new String[ARRAY_LENGTH];
getMeasureData(locale, unit, width, primaryData);
String[] secondaryData = new String[ARRAY_LENGTH];
@ -199,10 +222,13 @@ public class LongNameHandler implements MicroPropsGenerator {
// rawPerUnitFormat is something like "{0}/{1}"; we need to substitute in the secondary unit.
// TODO: Lots of thrashing. Improve?
StringBuilder sb = new StringBuilder();
String compiled = SimpleFormatterImpl.compileToStringMinMaxArguments(rawPerUnitFormat, sb, 2, 2);
String compiled = SimpleFormatterImpl
.compileToStringMinMaxArguments(rawPerUnitFormat, sb, 2, 2);
String secondaryFormat = getWithPlural(secondaryData, StandardPlural.ONE);
String secondaryCompiled = SimpleFormatterImpl.compileToStringMinMaxArguments(secondaryFormat, sb, 1, 1);
String secondaryString = SimpleFormatterImpl.getTextWithNoArguments(secondaryCompiled).trim();
String secondaryCompiled = SimpleFormatterImpl
.compileToStringMinMaxArguments(secondaryFormat, sb, 1, 1);
String secondaryString = SimpleFormatterImpl.getTextWithNoArguments(secondaryCompiled)
.trim();
perUnitFormat = SimpleFormatterImpl.formatCompiledPattern(compiled, "{0}", secondaryString);
}
// TODO: What field to use for units?
@ -212,7 +238,9 @@ public class LongNameHandler implements MicroPropsGenerator {
return new LongNameHandler(modifiers, rules, parent);
}
private static void simpleFormatsToModifiers(String[] simpleFormats, NumberFormat.Field field,
private static void simpleFormatsToModifiers(
String[] simpleFormats,
NumberFormat.Field field,
Map<StandardPlural, SimpleModifier> output) {
StringBuilder sb = new StringBuilder();
for (StandardPlural plural : StandardPlural.VALUES) {
@ -222,14 +250,18 @@ public class LongNameHandler implements MicroPropsGenerator {
}
}
private static void multiSimpleFormatsToModifiers(String[] leadFormats, String trailFormat,
NumberFormat.Field field, Map<StandardPlural, SimpleModifier> output) {
private static void multiSimpleFormatsToModifiers(
String[] leadFormats,
String trailFormat,
NumberFormat.Field field,
Map<StandardPlural, SimpleModifier> output) {
StringBuilder sb = new StringBuilder();
String trailCompiled = SimpleFormatterImpl.compileToStringMinMaxArguments(trailFormat, sb, 1, 1);
for (StandardPlural plural : StandardPlural.VALUES) {
String leadFormat = getWithPlural(leadFormats, plural);
String compoundFormat = SimpleFormatterImpl.formatCompiledPattern(trailCompiled, leadFormat);
String compoundCompiled = SimpleFormatterImpl.compileToStringMinMaxArguments(compoundFormat, sb, 1, 1);
String compoundCompiled = SimpleFormatterImpl
.compileToStringMinMaxArguments(compoundFormat, sb, 1, 1);
output.put(plural, new SimpleModifier(compoundCompiled, field, false));
}
}

View File

@ -13,8 +13,8 @@ import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.text.NumberFormat.Field;
/**
* A StringBuilder optimized for number formatting. It implements the following key features beyond a normal JDK
* StringBuilder:
* A StringBuilder optimized for number formatting. It implements the following key features beyond a
* normal JDK StringBuilder:
*
* <ol>
* <li>Efficient prepend as well as append.
@ -156,8 +156,8 @@ public class NumberStringBuilder implements CharSequence {
}
/**
* Inserts the specified CharSequence at the specified index in the string, reading from the CharSequence from start
* (inclusive) to end (exclusive).
* Inserts the specified CharSequence at the specified index in the string, reading from the
* CharSequence from start (inclusive) to end (exclusive).
*
* @return The number of chars added, which is the length of CharSequence.
*/
@ -172,8 +172,8 @@ public class NumberStringBuilder implements CharSequence {
}
/**
* Appends the chars in the specified char array to the end of the string, and associates them with the fields in
* the specified field array, which must have the same length as chars.
* Appends the chars in the specified char array to the end of the string, and associates them with
* the fields in the specified field array, which must have the same length as chars.
*
* @return The number of chars added, which is the length of the char array.
*/
@ -182,8 +182,8 @@ public class NumberStringBuilder implements CharSequence {
}
/**
* Inserts the chars in the specified char array at the specified index in the string, and associates them with the
* fields in the specified field array, which must have the same length as chars.
* Inserts the chars in the specified char array at the specified index in the string, and associates
* them with the fields in the specified field array, which must have the same length as chars.
*
* @return The number of chars added, which is the length of the char array.
*/
@ -257,7 +257,8 @@ public class NumberStringBuilder implements CharSequence {
}
private int prepareForInsertHelper(int index, int count) {
// Java note: Keeping this code out of prepareForInsert() increases the speed of append operations.
// Java note: Keeping this code out of prepareForInsert() increases the speed of append
// operations.
int oldCapacity = getCapacity();
int oldZero = zero;
char[] oldChars = chars;
@ -272,9 +273,17 @@ public class NumberStringBuilder implements CharSequence {
// First copy the prefix and then the suffix, leaving room for the new chars that the
// caller wants to insert.
System.arraycopy(oldChars, oldZero, newChars, newZero, index);
System.arraycopy(oldChars, oldZero + index, newChars, newZero + index + count, length - index);
System.arraycopy(oldChars,
oldZero + index,
newChars,
newZero + index + count,
length - index);
System.arraycopy(oldFields, oldZero, newFields, newZero, index);
System.arraycopy(oldFields, oldZero + index, newFields, newZero + index + count, length - index);
System.arraycopy(oldFields,
oldZero + index,
newFields,
newZero + index + count,
length - index);
chars = newChars;
fields = newFields;
@ -286,9 +295,17 @@ public class NumberStringBuilder implements CharSequence {
// First copy the entire string to the location of the prefix, and then move the suffix
// to make room for the new chars that the caller wants to insert.
System.arraycopy(oldChars, oldZero, oldChars, newZero, length);
System.arraycopy(oldChars, newZero + index, oldChars, newZero + index + count, length - index);
System.arraycopy(oldChars,
newZero + index,
oldChars,
newZero + index + count,
length - index);
System.arraycopy(oldFields, oldZero, oldFields, newZero, length);
System.arraycopy(oldFields, newZero + index, oldFields, newZero + index + count, length - index);
System.arraycopy(oldFields,
newZero + index,
oldFields,
newZero + index + count,
length - index);
zero = newZero;
length += count;
@ -342,8 +359,8 @@ public class NumberStringBuilder implements CharSequence {
* Returns a string that includes field information, for debugging purposes.
*
* <p>
* For example, if the string is "-12.345", the debug string will be something like "&lt;NumberStringBuilder
* [-123.45] [-iii.ff]&gt;"
* For example, if the string is "-12.345", the debug string will be something like
* "&lt;NumberStringBuilder [-123.45] [-iii.ff]&gt;"
*
* @return A string for debugging purposes.
*/
@ -374,7 +391,8 @@ public class NumberStringBuilder implements CharSequence {
}
/**
* @return Whether the contents and field values of this string builder are equal to the given chars and fields.
* @return Whether the contents and field values of this string builder are equal to the given chars
* and fields.
* @see #toCharArray
* @see #toFieldArray
*/
@ -455,7 +473,8 @@ public class NumberStringBuilder implements CharSequence {
Field _field = (i < zero + length) ? fields[i] : null;
if (seenStart && field != _field) {
// Special case: GROUPING_SEPARATOR counts as an INTEGER.
if (field == NumberFormat.Field.INTEGER && _field == NumberFormat.Field.GROUPING_SEPARATOR) {
if (field == NumberFormat.Field.INTEGER
&& _field == NumberFormat.Field.GROUPING_SEPARATOR) {
continue;
}
fp.setEndIndex(i - zero + offset);
@ -482,9 +501,13 @@ public class NumberStringBuilder implements CharSequence {
int currentStart = -1;
for (int i = 0; i < length; i++) {
Field field = fields[i + zero];
if (current == NumberFormat.Field.INTEGER && field == NumberFormat.Field.GROUPING_SEPARATOR) {
if (current == NumberFormat.Field.INTEGER
&& field == NumberFormat.Field.GROUPING_SEPARATOR) {
// Special case: GROUPING_SEPARATOR counts as an INTEGER.
as.addAttribute(NumberFormat.Field.GROUPING_SEPARATOR, NumberFormat.Field.GROUPING_SEPARATOR, i, i + 1);
as.addAttribute(NumberFormat.Field.GROUPING_SEPARATOR,
NumberFormat.Field.GROUPING_SEPARATOR,
i,
i + 1);
} else if (current != field) {
if (current != null) {
as.addAttribute(current, current, currentStart, i);

View File

@ -6,8 +6,8 @@ import com.ibm.icu.impl.SimpleFormatterImpl;
import com.ibm.icu.text.NumberFormat.Field;
/**
* The second primary implementation of {@link Modifier}, this one consuming a {@link com.ibm.icu.text.SimpleFormatter}
* pattern.
* The second primary implementation of {@link Modifier}, this one consuming a
* {@link com.ibm.icu.text.SimpleFormatter} pattern.
*/
public class SimpleModifier implements Modifier {
private final String compiledPattern;
@ -59,7 +59,8 @@ public class SimpleModifier implements Modifier {
count += Character.codePointCount(compiledPattern, 2, 2 + prefixLength);
}
if (suffixLength > 0) {
count += Character.codePointCount(compiledPattern, 1 + suffixOffset, 1 + suffixOffset + suffixLength);
count += Character
.codePointCount(compiledPattern, 1 + suffixOffset, 1 + suffixOffset + suffixLength);
}
return count;
}
@ -71,12 +72,13 @@ public class SimpleModifier implements Modifier {
/**
* TODO: This belongs in SimpleFormatterImpl. The only reason I haven't moved it there yet is because
* DoubleSidedStringBuilder is an internal class and SimpleFormatterImpl feels like it should not depend on it.
* DoubleSidedStringBuilder is an internal class and SimpleFormatterImpl feels like it should not
* depend on it.
*
* <p>
* Formats a value that is already stored inside the StringBuilder <code>result</code> between the indices
* <code>startIndex</code> and <code>endIndex</code> by inserting characters before the start index and after the
* end index.
* Formats a value that is already stored inside the StringBuilder <code>result</code> between the
* indices <code>startIndex</code> and <code>endIndex</code> by inserting characters before the start
* index and after the end index.
*
* <p>
* This is well-defined only for patterns with exactly one argument.
@ -89,12 +91,19 @@ public class SimpleModifier implements Modifier {
* The right index of the value within the string builder.
* @return The number of characters (UTF-16 code points) that were added to the StringBuilder.
*/
public int formatAsPrefixSuffix(NumberStringBuilder result, int startIndex, int endIndex, Field field) {
public int formatAsPrefixSuffix(
NumberStringBuilder result,
int startIndex,
int endIndex,
Field field) {
if (prefixLength > 0) {
result.insert(startIndex, compiledPattern, 2, 2 + prefixLength, field);
}
if (suffixLength > 0) {
result.insert(endIndex + prefixLength, compiledPattern, 1 + suffixOffset, 1 + suffixOffset + suffixLength,
result.insert(endIndex + prefixLength,
compiledPattern,
1 + suffixOffset,
1 + suffixOffset + suffixLength,
field);
}
return prefixLength + suffixLength;