下面列出了java.text.FieldPosition#setBeginIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Objects.requireNonNull(obj, "obj");
Objects.requireNonNull(toAppendTo, "toAppendTo");
Objects.requireNonNull(pos, "pos");
if (obj instanceof TemporalAccessor == false) {
throw new IllegalArgumentException("Format target must implement TemporalAccessor");
}
pos.setBeginIndex(0);
pos.setEndIndex(0);
try {
formatter.formatTo((TemporalAccessor) obj, toAppendTo);
} catch (RuntimeException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
return toAppendTo;
}
/**
* Formats the coordinates of a {@link Vector} to produce a string.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @param coordinates coordinates of the object to format.
* @return the value passed in as toAppendTo.
*/
protected StringBuffer format(StringBuffer toAppendTo, FieldPosition pos,
double ... coordinates) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
for (int i = 0; i < coordinates.length; ++i) {
if (i > 0) {
toAppendTo.append(separator);
}
CompositeFormat.formatDouble(coordinates[i], format, toAppendTo, pos);
}
// format suffix
toAppendTo.append(suffix);
return toAppendTo;
}
public void test_hashCode() {
// Test for method int java.text.FieldPosition.hashCode()
FieldPosition fpos1 = new FieldPosition(1);
FieldPosition fpos2 = new FieldPosition(1);
assertTrue("test 1: hash codes are not equal for equal objects.",
fpos1.hashCode() == fpos2.hashCode());
fpos1.setBeginIndex(5);
fpos1.setEndIndex(110);
assertTrue("test 2: hash codes are equal for non equal objects.",
fpos1.hashCode() != fpos2.hashCode());
fpos2.setBeginIndex(5);
fpos2.setEndIndex(110);
assertTrue("test 3: hash codes are not equal for equal objects.",
fpos1.hashCode() == fpos2.hashCode());
FieldPosition fpos3 = new FieldPosition(
DateFormat.Field.DAY_OF_WEEK_IN_MONTH);
assertTrue("test 4: hash codes are equal for non equal objects.",
fpos2.hashCode() != fpos3.hashCode());
}
/**
* Formats a {@link Vector3D} object to produce a string.
* @param vector the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(Vector3D vector, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
formatDouble(vector.getX(), format, toAppendTo, pos);
toAppendTo.append(separator);
formatDouble(vector.getY(), format, toAppendTo, pos);
toAppendTo.append(separator);
formatDouble(vector.getZ(), format, toAppendTo, pos);
// format suffix
toAppendTo.append(suffix);
return toAppendTo;
}
/**
* Formats a {@link Vector3D} object to produce a string.
* @param vector the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(Vector3D vector, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
formatDouble(vector.getX(), format, toAppendTo, pos);
toAppendTo.append(separator);
formatDouble(vector.getY(), format, toAppendTo, pos);
toAppendTo.append(separator);
formatDouble(vector.getZ(), format, toAppendTo, pos);
// format suffix
toAppendTo.append(suffix);
return toAppendTo;
}
/**
* Formats a {@link Fraction} object to produce a string. The fraction
* is output in proper format.
*
* @param fraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(Fraction fraction, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
int num = fraction.getNumerator();
int den = fraction.getDenominator();
int whole = num / den;
num = num % den;
if (whole != 0) {
getWholeFormat().format(whole, toAppendTo, pos);
toAppendTo.append(' ');
num = Math.abs(num);
}
getNumeratorFormat().format(num, toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(den, toAppendTo,
pos);
return toAppendTo;
}
/**
* Formats a {@link BigFraction} object to produce a string. The BigFraction
* is output in proper format.
*
* @param fraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
@Override
public StringBuffer format(final BigFraction fraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
BigInteger num = fraction.getNumerator();
BigInteger den = fraction.getDenominator();
BigInteger whole = num.divide(den);
num = num.remainder(den);
if (!BigInteger.ZERO.equals(whole)) {
getWholeFormat().format(whole, toAppendTo, pos);
toAppendTo.append(' ');
if (num.compareTo(BigInteger.ZERO) < 0) {
num = num.negate();
}
}
getNumeratorFormat().format(num, toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(den, toAppendTo, pos);
return toAppendTo;
}
/**
* Formats the pattern with the value and adjusts the FieldPosition.
*/
public static StringBuilder format(String compiledPattern, CharSequence value,
StringBuilder appendTo, FieldPosition pos) {
int[] offsets = new int[1];
SimpleFormatterImpl.formatAndAppend(compiledPattern, appendTo, offsets, value);
if (pos.getBeginIndex() != 0 || pos.getEndIndex() != 0) {
if (offsets[0] >= 0) {
pos.setBeginIndex(pos.getBeginIndex() + offsets[0]);
pos.setEndIndex(pos.getEndIndex() + offsets[0]);
} else {
pos.setBeginIndex(0);
pos.setEndIndex(0);
}
}
return appendTo;
}
/**
* Formats the coordinates of a {@link Vector} to produce a string.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @param coordinates coordinates of the object to format.
* @return the value passed in as toAppendTo.
*/
protected StringBuffer format(StringBuffer toAppendTo, FieldPosition pos,
double ... coordinates) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
for (int i = 0; i < coordinates.length; ++i) {
if (i > 0) {
toAppendTo.append(separator);
}
CompositeFormat.formatDouble(coordinates[i], format, toAppendTo, pos);
}
// format suffix
toAppendTo.append(suffix);
return toAppendTo;
}
/**
* @tests java.text.FieldPosition#equals(java.lang.Object)
*/
public void test_equalsLjava_lang_Object() {
// Test for method boolean
// java.text.FieldPosition.equals(java.lang.Object)
FieldPosition fpos = new FieldPosition(1);
FieldPosition fpos1 = new FieldPosition(1);
assertTrue("Identical objects were not equal!", fpos.equals(fpos1));
FieldPosition fpos2 = new FieldPosition(2);
assertTrue("Objects with a different ID should not be equal!", !fpos
.equals(fpos2));
fpos.setBeginIndex(1);
fpos1.setBeginIndex(2);
assertTrue("Objects with a different beginIndex were still equal!",
!fpos.equals(fpos1));
fpos1.setBeginIndex(1);
fpos1.setEndIndex(2);
assertTrue("Objects with a different endIndex were still equal!", !fpos
.equals(fpos1));
FieldPosition fpos3 = new FieldPosition(DateFormat.Field.ERA, 1);
assertTrue("Objects with a different attribute should not be equal!",
!fpos.equals(fpos3));
FieldPosition fpos4 = new FieldPosition(DateFormat.Field.AM_PM, 1);
assertTrue("Objects with a different attribute should not be equal!",
!fpos3.equals(fpos4));
}
/**
* Formats a {@link BigFraction} object to produce a string. The BigFraction is
* output in improper format.
*
* @param BigFraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(final BigFraction BigFraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
getNumeratorFormat().format(BigFraction.getNumerator(), toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(BigFraction.getDenominator(), toAppendTo, pos);
return toAppendTo;
}
/**
* Formats a {@link Fraction} object to produce a string. The fraction is
* output in improper format.
*
* @param fraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(final Fraction fraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
getNumeratorFormat().format(fraction.getNumerator(), toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(fraction.getDenominator(), toAppendTo,
pos);
return toAppendTo;
}
/**
* 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);
}
/**
* Formats a {@link BigFraction} object to produce a string. The BigFraction is
* output in improper format.
*
* @param BigFraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(final BigFraction BigFraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
getNumeratorFormat().format(BigFraction.getNumerator(), toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(BigFraction.getDenominator(), toAppendTo, pos);
return toAppendTo;
}
/**
* Formats a {@link Fraction} object to produce a string. The fraction is
* output in improper format.
*
* @param fraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(final Fraction fraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
getNumeratorFormat().format(fraction.getNumerator(), toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(fraction.getDenominator(), toAppendTo,
pos);
return toAppendTo;
}
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());
}
/**
* Formats a {@link Fraction} object to produce a string. The fraction is
* output in improper format.
*
* @param fraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(final Fraction fraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
getNumeratorFormat().format(fraction.getNumerator(), toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(fraction.getDenominator(), toAppendTo,
pos);
return toAppendTo;
}
/**
* Formats a {@link Fraction} object to produce a string. The fraction is
* output in improper format.
*
* @param fraction the object to format.
* @param toAppendTo where the text is to be appended
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field
* @return the value passed in as toAppendTo.
*/
public StringBuffer format(final Fraction fraction,
final StringBuffer toAppendTo, final FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
getNumeratorFormat().format(fraction.getNumerator(), toAppendTo, pos);
toAppendTo.append(" / ");
getDenominatorFormat().format(fraction.getDenominator(), toAppendTo,
pos);
return toAppendTo;
}
/**
* Format the absolute value of the imaginary part.
*
* @param absIm Absolute value of the imaginary part of a complex number.
* @param toAppendTo where the text is to be appended.
* @param pos On input: an alignment field, if desired. On output: the
* offsets of the alignment field.
* @return the value passed in as toAppendTo.
*/
private StringBuffer formatImaginary(double absIm,
StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos);
if (toAppendTo.toString().equals("1")) {
// Remove the character "1" if it is the only one.
toAppendTo.setLength(0);
}
return toAppendTo;
}
private StringBuffer format(Calendar cal, DisplayContext capitalizationContext,
StringBuffer toAppendTo, FieldPosition pos, List<FieldPosition> attributes) {
// Initialize
pos.setBeginIndex(0);
pos.setEndIndex(0);
// Careful: For best performance, minimize the number of calls
// to StringBuffer.append() by consolidating appends when
// possible.
Object[] items = getPatternItems();
for (int i = 0; i < items.length; i++) {
if (items[i] instanceof String) {
toAppendTo.append((String)items[i]);
} else {
PatternItem item = (PatternItem)items[i];
int start = 0;
if (attributes != null) {
// Save the current length
start = toAppendTo.length();
}
if (useFastFormat) {
subFormat(toAppendTo, item.type, item.length, toAppendTo.length(),
i, capitalizationContext, pos, cal);
} else {
toAppendTo.append(subFormat(item.type, item.length, toAppendTo.length(),
i, capitalizationContext, pos, cal));
}
if (attributes != null) {
// Check the sub format length
int end = toAppendTo.length();
if (end - start > 0) {
// Append the attribute to the list
DateFormat.Field attr = patternCharToDateFormatField(item.type);
FieldPosition fp = new FieldPosition(attr);
fp.setBeginIndex(start);
fp.setEndIndex(end);
attributes.add(fp);
}
}
}
}
return toAppendTo;
}