Java Code Examples for java.text.FieldPosition#setBeginIndex()

The following examples show how to use java.text.FieldPosition#setBeginIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VectorFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;

}
 
Example 2
Source File: DateTimeFormatter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: VectorFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;

}
 
Example 4
Source File: FieldPositionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 5
Source File: Vector3DFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;

}
 
Example 6
Source File: QuantityFormatter.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: ProperBigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: ProperFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: Vector3DFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;

}
 
Example 10
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: ComplexFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: MeasureFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 15
Source File: FieldPositionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @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));
}
 
Example 16
Source File: BigFractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 17
Source File: QuantityFormatter.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: BigFractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: SimpleDateFormat.java    From j2objc with Apache License 2.0 4 votes vote down vote up
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;

}