From: Travis Keep Date: Thu, 30 Jan 2014 07:07:11 +0000 (+0000) Subject: ICU-10640 Fix broken build. Fix MeasureFormat.format() to give correct begin and... X-Git-Tag: milestone-59-0-1~2200 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b5a7244a2985df4bc920d8ebd3985996afd58cc5;p=icu ICU-10640 Fix broken build. Fix MeasureFormat.format() to give correct begin and end index in FieldPosition even if the StringBuffer was initially non-empty. X-SVN-Rev: 35035 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java index fbe80aaa18c..7ce3daec843 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyFormat.java @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (c) 2004-2013, International Business Machines +* Copyright (c) 2004-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -86,18 +86,9 @@ class CurrencyFormat extends MeasureFormat { * @provisional */ @Override - public String formatMeasures(Measure... measures) { - return mf.formatMeasures(measures); - } - - /** - * @draft ICU 53 - * @provisional - */ - @Override - public T formatMeasures( - T appendable, FieldPosition fieldPosition, Measure... measures) { - return mf.formatMeasures(appendable, fieldPosition, measures); + public StringBuilder formatMeasures( + StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) { + return mf.formatMeasures(appendTo, fieldPosition, measures); } /** @@ -109,15 +100,6 @@ class CurrencyFormat extends MeasureFormat { return mf.getWidth(); } - /** - * @draft ICU 53 - * @provisional - */ - @Override - public ULocale getLocale() { - return mf.getLocale(); - } - /** * @draft ICU 53 * @provisional diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java b/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java index fc1bf3eda9d..ef9679aee54 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/ListFormatter.java @@ -272,20 +272,20 @@ final public class ListFormatter { throw new IllegalArgumentException("Need {0} and {1} only in pattern " + pattern); } if (recordOffset || offsetRecorded()) { - SimplePatternFormatter.Formatted formatted = pattern.formatValues(new Object[]{current, next}); - int oneOffset = formatted.getOffset(1); - int zeroOffset = formatted.getOffset(0); - if (zeroOffset == -1 || oneOffset == -1) { - throw new IllegalArgumentException("{0} or {1} missing from pattern " + pattern); + int[] offsets = new int[2]; + current = pattern.format( + new StringBuilder(), offsets, current, next.toString()).toString(); + if (offsets[0] == -1 || offsets[1] == -1) { + throw new IllegalArgumentException( + "{0} or {1} missing from pattern " + pattern); } if (recordOffset) { - offset = oneOffset; + offset = offsets[1]; } else { - offset += zeroOffset; + offset += offsets[0]; } - current = formatted.toString(); } else { - current = pattern.format(current, next); + current = pattern.format(current, next.toString()); } return this; } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java index 5e2550e6674..1bfe8bf84fd 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java @@ -281,6 +281,9 @@ public class MeasureFormat extends UFormat { */ @Override public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { + int prevLength = toAppendTo.length(); + FieldPosition fpos = + new FieldPosition(pos.getFieldAttribute(), pos.getField()); if (obj instanceof Collection) { Collection coll = (Collection) obj; Measure[] measures = new Measure[coll.size()]; @@ -291,14 +294,19 @@ public class MeasureFormat extends UFormat { } measures[idx++] = (Measure) o; } - return toAppendTo.append(formatMeasures(new StringBuilder(), pos, measures)); + toAppendTo.append(formatMeasures(new StringBuilder(), fpos, measures)); } else if (obj instanceof Measure[]) { - return toAppendTo.append(formatMeasures(new StringBuilder(), pos, (Measure[]) obj)); + toAppendTo.append(formatMeasures(new StringBuilder(), fpos, (Measure[]) obj)); } else if (obj instanceof Measure){ - return toAppendTo.append(formatMeasure((Measure) obj, new StringBuilder(), pos)); + toAppendTo.append(formatMeasure((Measure) obj, new StringBuilder(), fpos)); } else { throw new IllegalArgumentException(obj.toString()); } + if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) { + pos.setBeginIndex(fpos.getBeginIndex() + prevLength); + pos.setEndIndex(fpos.getEndIndex() + prevLength); + } + return toAppendTo; } /** @@ -326,14 +334,15 @@ public class MeasureFormat extends UFormat { * @draft ICU 53 * @provisional */ - public String formatMeasures(Measure... measures) { - StringBuilder result = this.formatMeasures( - new StringBuilder(), DontCareFieldPosition.INSTANCE, measures); - return result.toString(); + public final String formatMeasures(Measure... measures) { + return formatMeasures( + new StringBuilder(), + DontCareFieldPosition.INSTANCE, + measures).toString(); } /** - * Formats a sequence of measures and adds to appendable. + * Formats a sequence of measures. * * If the fieldPosition argument identifies a NumberFormat field, * then its indices are set to the beginning and end of the first such field @@ -369,7 +378,7 @@ public class MeasureFormat extends UFormat { ListFormatter listFormatter = ListFormatter.getInstance( getLocale(), formatWidth.getListFormatterStyle()); if (fieldPosition != DontCareFieldPosition.INSTANCE) { - return appendTo.append(formatMeasuresSlowTrack(listFormatter, fieldPosition, measures)); + return formatMeasuresSlowTrack(listFormatter, appendTo, fieldPosition, measures); } // Fast track: No field position. String[] results = new String[measures.length]; @@ -426,7 +435,7 @@ public class MeasureFormat extends UFormat { * @draft ICU 53 * @provisional */ - public ULocale getLocale() { + public final ULocale getLocale() { return getLocale(ULocale.VALID_LOCALE); } @@ -618,14 +627,13 @@ public class MeasureFormat extends UFormat { Map styleToCountToFormat = unitToStyleToCountToFormat.get(unit); QuantityFormatter countToFormat = styleToCountToFormat.get(formatWidth); SimplePatternFormatter formatter = countToFormat.getByVariant(keyword); - SimplePatternFormatter.Formatted result = formatter.formatValues(new Object[] {formattedNumber}); - appendTo.append(result.toString()); - int offset = result.getOffset(0); - if (offset != -1) { // there is a number (may not happen with, say, Arabic dual) + int[] offsets = new int[1]; + formatter.format(appendTo, offsets, formattedNumber.toString()); + if (offsets[0] != -1) { // there is a number (may not happen with, say, Arabic dual) // Fix field position if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) { - fieldPosition.setBeginIndex(fpos.getBeginIndex() + offset); - fieldPosition.setEndIndex(fpos.getEndIndex() + offset); + fieldPosition.setBeginIndex(fpos.getBeginIndex() + offsets[0]); + fieldPosition.setEndIndex(fpos.getEndIndex() + offsets[0]); } } return appendTo; @@ -685,12 +693,16 @@ public class MeasureFormat extends UFormat { return new MeasureProxy(getLocale(), formatWidth, numberFormat.get(), CURRENCY_FORMAT); } - private String formatMeasuresSlowTrack(ListFormatter listFormatter, FieldPosition fieldPosition, + private StringBuilder formatMeasuresSlowTrack( + ListFormatter listFormatter, + StringBuilder appendTo, + FieldPosition fieldPosition, Measure... measures) { String[] results = new String[measures.length]; // Zero out our field position so that we can tell when we find our field. - FieldPosition fpos = new FieldPosition(fieldPosition.getFieldAttribute(), fieldPosition.getField()); + FieldPosition fpos = new FieldPosition( + fieldPosition.getFieldAttribute(), fieldPosition.getField()); int fieldPositionFoundIndex = -1; for (int i = 0; i < measures.length; ++i) { @@ -708,10 +720,10 @@ public class MeasureFormat extends UFormat { // Fix up FieldPosition indexes if our field is found. if (builder.getOffset() != -1) { - fieldPosition.setBeginIndex(fpos.getBeginIndex() + builder.getOffset()); - fieldPosition.setEndIndex(fpos.getEndIndex() + builder.getOffset()); + fieldPosition.setBeginIndex(fpos.getBeginIndex() + builder.getOffset() + appendTo.length()); + fieldPosition.setEndIndex(fpos.getEndIndex() + builder.getOffset() + appendTo.length()); } - return builder.toString(); + return appendTo.append(builder.toString()); } // type is one of "hm", "ms" or "hms" diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java index a131ba052ea..d8a3ffded01 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeUnitFormat.java @@ -517,23 +517,15 @@ if ( searchPluralCount.equals("other") ) { // boilerplate code to make TimeUnitFormat otherwise follow the contract of // MeasureFormat - /** - * @draft ICU 53 - * @provisional - */ - @Override - public String formatMeasures(Measure... measures) { - return mf.formatMeasures(measures); - } /** * @draft ICU 53 * @provisional */ @Override - public T formatMeasures( - T appendable, FieldPosition fieldPosition, Measure... measures) { - return mf.formatMeasures(appendable, fieldPosition, measures); + public StringBuilder formatMeasures( + StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures) { + return mf.formatMeasures(appendTo, fieldPosition, measures); } /** @@ -545,16 +537,6 @@ if ( searchPluralCount.equals("other") ) { return mf.getWidth(); } - /** - * @draft ICU 53 - * @provisional - */ - @Override - public ULocale getLocale() { - return mf.getLocale(); - } - - /** * @draft ICU 53 * @provisional diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java index 221ff79ae90..842acf478ea 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java @@ -421,9 +421,9 @@ public class MeasureUnitTest extends TestFmwk { MeasureFormat fmt = MeasureFormat.getInstance( ULocale.ENGLISH, FormatWidth.SHORT); FieldPosition pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR); - fmt.format(new Measure(43.5, MeasureUnit.FOOT), new StringBuffer(), pos); - assertEquals("beginIndex", 2, pos.getBeginIndex()); - assertEquals("endIndex", 3, pos.getEndIndex()); + fmt.format(new Measure(43.5, MeasureUnit.FOOT), new StringBuffer("123456: "), pos); + assertEquals("beginIndex", 10, pos.getBeginIndex()); + assertEquals("endIndex", 11, pos.getEndIndex()); pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR); fmt.format(new Measure(43, MeasureUnit.FOOT), new StringBuffer(), pos); @@ -450,14 +450,14 @@ public class MeasureUnitTest extends TestFmwk { pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR); result = fmt.formatMeasures( - new StringBuilder(), + new StringBuilder("123456: "), pos, new Measure(354, MeasureUnit.METER), new Measure(23, MeasureUnit.CENTIMETER), new Measure(5.4, MeasureUnit.MILLIMETER)).toString(); - assertEquals("result", "354 m, 23 cm, 5.4 mm", result); - assertEquals("beginIndex", 15, pos.getBeginIndex()); - assertEquals("endIndex", 16, pos.getEndIndex()); + assertEquals("result", "123456: 354 m, 23 cm, 5.4 mm", result); + assertEquals("beginIndex", 23, pos.getBeginIndex()); + assertEquals("endIndex", 24, pos.getEndIndex()); result = fmt.formatMeasures( new StringBuilder(), @@ -471,15 +471,24 @@ public class MeasureUnitTest extends TestFmwk { pos = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR); result = fmt.formatMeasures( - new StringBuilder(), + new StringBuilder("123456: "), pos, new Measure(3, MeasureUnit.METER), new Measure(23, MeasureUnit.CENTIMETER), new Measure(5, MeasureUnit.MILLIMETER)).toString(); - assertEquals("result", "3 m, 23 cm, 5 mm", result); + assertEquals("result", "123456: 3 m, 23 cm, 5 mm", result); assertEquals("beginIndex", 0, pos.getBeginIndex()); assertEquals("endIndex", 0, pos.getEndIndex()); + pos = new FieldPosition(NumberFormat.Field.INTEGER); + result = fmt.formatMeasures( + new StringBuilder("123456: "), + pos, + new Measure(57, MeasureUnit.MILLIMETER)).toString(); + assertEquals("result", "123456: 57 mm", result); + assertEquals("beginIndex", 8, pos.getBeginIndex()); + assertEquals("endIndex", 10, pos.getEndIndex()); + } public void testOldFormatWithList() {