/*
**********************************************************************
-* Copyright (c) 2004-2013, International Business Machines
+* Copyright (c) 2004-2014, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
* @provisional
*/
@Override
- public String formatMeasures(Measure... measures) {
- return mf.formatMeasures(measures);
- }
-
- /**
- * @draft ICU 53
- * @provisional
- */
- @Override
- public <T extends Appendable> 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);
}
/**
return mf.getWidth();
}
- /**
- * @draft ICU 53
- * @provisional
- */
- @Override
- public ULocale getLocale() {
- return mf.getLocale();
- }
-
/**
* @draft ICU 53
* @provisional
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;
}
*/
@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()];
}
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;
}
/**
* @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
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];
* @draft ICU 53
* @provisional
*/
- public ULocale getLocale() {
+ public final ULocale getLocale() {
return getLocale(ULocale.VALID_LOCALE);
}
Map<FormatWidth, QuantityFormatter> 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;
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) {
// 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"
// 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 extends Appendable> 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);
}
/**
return mf.getWidth();
}
- /**
- * @draft ICU 53
- * @provisional
- */
- @Override
- public ULocale getLocale() {
- return mf.getLocale();
- }
-
-
/**
* @draft ICU 53
* @provisional
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);
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(),
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() {