下面列出了java.text.FieldPosition#setEndIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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.
*/
@Override
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;
}
/**
* 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.
* @throws MathInternalError if {@code absIm} is not positive.
*/
private StringBuffer formatImaginary(double absIm,
StringBuffer toAppendTo,
FieldPosition pos) {
if (absIm < 0) {
throw new MathInternalError();
}
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;
}
/**
* 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 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 a {@link Complex} object to produce a string.
*
* @param complex 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(Complex complex, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format real
double re = complex.getReal();
formatDouble(re, getRealFormat(), toAppendTo, pos);
// format sign and imaginary
double im = complex.getImaginary();
if (im < 0.0) {
toAppendTo.append(" - ");
formatDouble(-im, getImaginaryFormat(), toAppendTo, pos);
toAppendTo.append(getImaginaryCharacter());
} else if (im > 0.0 || Double.isNaN(im)) {
toAppendTo.append(" + ");
formatDouble(im, getImaginaryFormat(), toAppendTo, pos);
toAppendTo.append(getImaginaryCharacter());
}
return toAppendTo;
}
/**
* Formats a {@link RealVector} 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(RealVector vector, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
for (int i = 0; i < vector.getDimension(); ++i) {
if (i > 0) {
toAppendTo.append(separator);
}
CompositeFormat.formatDouble(vector.getEntry(i), format, toAppendTo, pos);
}
// format suffix
toAppendTo.append(suffix);
return toAppendTo;
}
/**
* Formats a {@link RealVector} 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(RealVector vector, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format prefix
toAppendTo.append(prefix);
// format components
for (int i = 0; i < vector.getDimension(); ++i) {
if (i > 0) {
toAppendTo.append(separator);
}
formatDouble(vector.getEntry(i), 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.
*/
@Override
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 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
CompositeFormat.formatDouble(vector.getX(), format, toAppendTo, pos);
toAppendTo.append(separator);
CompositeFormat.formatDouble(vector.getY(), format, toAppendTo, pos);
toAppendTo.append(separator);
CompositeFormat.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.
*/
@Override
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 Complex} object to produce a string.
*
* @param complex 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(Complex complex, StringBuffer toAppendTo,
FieldPosition pos) {
pos.setBeginIndex(0);
pos.setEndIndex(0);
// format real
double re = complex.getReal();
formatDouble(re, getRealFormat(), toAppendTo, pos);
// format sign and imaginary
double im = complex.getImaginary();
if (im < 0.0) {
toAppendTo.append(" - ");
formatDouble(-im, getImaginaryFormat(), toAppendTo, pos);
toAppendTo.append(getImaginaryCharacter());
} else if (im > 0.0 || Double.isNaN(im)) {
toAppendTo.append(" + ");
formatDouble(im, getImaginaryFormat(), toAppendTo, pos);
toAppendTo.append(getImaginaryCharacter());
}
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;
}
/**
* @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, // NOPMD - PMD false positive, we cannot have @Override here
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 FieldPosition updateMetaData(AppendableWrapper dest, int prevLength,
FieldPosition fp, Object argId) {
if (dest.attributes != null && prevLength < dest.length) {
dest.attributes.add(new AttributeAndPosition(argId, prevLength, dest.length));
}
if (fp != null && Field.ARGUMENT.equals(fp.getFieldAttribute())) {
fp.setBeginIndex(prevLength);
fp.setEndIndex(dest.length);
return null;
}
return fp;
}
/**
* 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;
}
/**
* 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(Fraction fraction, StringBuffer toAppendTo,
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;
}