本文整理汇总了Java中java.text.FieldPosition.getFieldAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java FieldPosition.getFieldAttribute方法的具体用法?Java FieldPosition.getFieldAttribute怎么用?Java FieldPosition.getFieldAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.FieldPosition
的用法示例。
在下文中一共展示了FieldPosition.getFieldAttribute方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatMeasurePerUnit
import java.text.FieldPosition; //导入方法依赖的package包/类
/**
* Formats a single measure per unit.
*
* An example of such a formatted string is "3.5 meters per second."
*
* @param measure the measure object. In above example, 3.5 meters.
* @param perUnit the per unit. In above example, it is MeasureUnit.SECOND
* @param appendTo formatted string appended here.
* @param pos The field position.
* @return appendTo.
* @stable ICU 55
*/
public StringBuilder formatMeasurePerUnit(
Measure measure,
MeasureUnit perUnit,
StringBuilder appendTo,
FieldPosition pos) {
MeasureUnit resolvedUnit = MeasureUnit.resolveUnitPerUnit(
measure.getUnit(), perUnit);
if (resolvedUnit != null) {
Measure newMeasure = new Measure(measure.getNumber(), resolvedUnit);
return formatMeasure(newMeasure, numberFormat, appendTo, pos);
}
FieldPosition fpos = new FieldPosition(
pos.getFieldAttribute(), pos.getField());
int offset = withPerUnitAndAppend(
formatMeasure(measure, numberFormat, new StringBuilder(), fpos),
perUnit,
appendTo);
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
pos.setBeginIndex(fpos.getBeginIndex() + offset);
pos.setEndIndex(fpos.getEndIndex() + offset);
}
return appendTo;
}
示例2: selectPlural
import java.text.FieldPosition; //导入方法依赖的package包/类
/**
* Selects the standard plural form for the number/formatter/rules.
*/
public static StandardPlural selectPlural(
Number number, NumberFormat fmt, PluralRules rules,
StringBuffer formattedNumber, FieldPosition pos) {
UFieldPosition fpos = new UFieldPosition(pos.getFieldAttribute(), pos.getField());
fmt.format(number, formattedNumber, fpos);
// TODO: Long, BigDecimal & BigInteger may not fit into doubleValue().
FixedDecimal fd = new FixedDecimal(
number.doubleValue(),
fpos.getCountVisibleFractionDigits(), fpos.getFractionDigits());
String pluralKeyword = rules.select(fd);
pos.setBeginIndex(fpos.getBeginIndex());
pos.setEndIndex(fpos.getEndIndex());
return StandardPlural.orOtherFromString(pluralKeyword);
}
示例3: format
import java.text.FieldPosition; //导入方法依赖的package包/类
/**
* Able to format Collection<? extends Measure>, Measure[], and Measure
* by delegating to formatMeasures.
* If the pos argument identifies a NumberFormat field,
* then its indices are set to the beginning and end of the first such field
* encountered. MeasureFormat itself does not supply any fields.
*
* Calling a
* <code>formatMeasures</code> method is preferred over calling
* this method as they give better performance.
*
* @param obj must be a Collection<? extends Measure>, Measure[], or Measure object.
* @param toAppendTo Formatted string appended here.
* @param pos Identifies a field in the formatted text.
* @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
*
* @stable ICU53
*/
@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()];
int idx = 0;
for (Object o : coll) {
if (!(o instanceof Measure)) {
throw new IllegalArgumentException(obj.toString());
}
measures[idx++] = (Measure) o;
}
toAppendTo.append(formatMeasures(new StringBuilder(), fpos, measures));
} else if (obj instanceof Measure[]) {
toAppendTo.append(formatMeasures(new StringBuilder(), fpos, (Measure[]) obj));
} else if (obj instanceof Measure){
toAppendTo.append(formatMeasure((Measure) obj, numberFormat, 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;
}
示例4: formatMeasuresSlowTrack
import java.text.FieldPosition; //导入方法依赖的package包/类
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());
int fieldPositionFoundIndex = -1;
for (int i = 0; i < measures.length; ++i) {
ImmutableNumberFormat nf = (i == measures.length - 1 ? numberFormat : integerFormat);
if (fieldPositionFoundIndex == -1) {
results[i] = formatMeasure(measures[i], nf, new StringBuilder(), fpos).toString();
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
fieldPositionFoundIndex = i;
}
} else {
results[i] = formatMeasure(measures[i], nf);
}
}
ListFormatter.FormattedListBuilder builder =
listFormatter.format(Arrays.asList(results), fieldPositionFoundIndex);
// Fix up FieldPosition indexes if our field is found.
if (builder.getOffset() != -1) {
fieldPosition.setBeginIndex(fpos.getBeginIndex() + builder.getOffset() + appendTo.length());
fieldPosition.setEndIndex(fpos.getEndIndex() + builder.getOffset() + appendTo.length());
}
return appendTo.append(builder.toString());
}
示例5: formatToCharacterIterator
import java.text.FieldPosition; //导入方法依赖的package包/类
AttributedCharacterIterator formatToCharacterIterator(Object obj, Unit unit) {
if (!(obj instanceof Number))
throw new IllegalArgumentException();
Number number = (Number) obj;
StringBuffer text = new StringBuffer();
unit.writePrefix(text);
attributes.clear();
if (obj instanceof BigInteger) {
format((BigInteger) number, text, new FieldPosition(0), true);
} else if (obj instanceof java.math.BigDecimal) {
format((java.math.BigDecimal) number, text, new FieldPosition(0)
, true);
} else if (obj instanceof Double) {
format(number.doubleValue(), text, new FieldPosition(0), true);
} else if (obj instanceof Integer || obj instanceof Long) {
format(number.longValue(), text, new FieldPosition(0), true);
} else {
throw new IllegalArgumentException();
}
unit.writeSuffix(text);
AttributedString as = new AttributedString(text.toString());
// add NumberFormat field attributes to the AttributedString
for (int i = 0; i < attributes.size(); i++) {
FieldPosition pos = attributes.get(i);
Format.Field attribute = pos.getFieldAttribute();
as.addAttribute(attribute, attribute, pos.getBeginIndex(), pos.getEndIndex());
}
// return the CharacterIterator from AttributedString
return as.getIterator();
}