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

The following examples show how to use java.text.FieldPosition#getField() . 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: MeasureFormat.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: MeasureFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 */
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;
}
 
Example 3
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 4
Source File: MeasureFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * 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&lt;? extends Measure&gt;, 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;
}
 
Example 5
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 6
Source File: DateIntervalFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private final StringBuffer fallbackFormat(Calendar fromCalendar,
                                          Calendar toCalendar,
                                          boolean fromToOnSameDay,
                                          StringBuffer appendTo,
                                          FieldPosition pos)  {
        String fullPattern = null; // for saving the pattern in fDateFormat
        boolean formatDatePlusTimeRange = (fromToOnSameDay && fDatePattern != null && fTimePattern != null);
        // the fall back
        if (formatDatePlusTimeRange) {
            fullPattern = fDateFormat.toPattern(); // save current pattern, restore later
            fDateFormat.applyPattern(fTimePattern);
        }
        FieldPosition otherPos = new FieldPosition(pos.getField());
        StringBuffer earlierDate = new StringBuffer(64);
        earlierDate = fDateFormat.format(fromCalendar, earlierDate, pos);
        StringBuffer laterDate = new StringBuffer(64);
        laterDate = fDateFormat.format(toCalendar, laterDate, otherPos);
        String fallbackPattern = fInfo.getFallbackIntervalPattern();
        adjustPosition(fallbackPattern, earlierDate.toString(), pos, laterDate.toString(), otherPos, pos);
        String fallbackRange = SimpleFormatterImpl.formatRawPattern(
                fallbackPattern, 2, 2, earlierDate, laterDate);
        if (formatDatePlusTimeRange) {
            // fallbackRange has just the time range, need to format the date part and combine that
            fDateFormat.applyPattern(fDatePattern);
            StringBuffer datePortion = new StringBuffer(64);
            otherPos.setBeginIndex(0);
            otherPos.setEndIndex(0);
            datePortion = fDateFormat.format(fromCalendar, datePortion, otherPos);
            adjustPosition(fDateTimeFormat, fallbackRange, pos, datePortion.toString(), otherPos, pos);
            fallbackRange = SimpleFormatterImpl.formatRawPattern(
                    fDateTimeFormat, 2, 2, fallbackRange, datePortion);
        }
        appendTo.append(fallbackRange);
        if (formatDatePlusTimeRange) {
            // restore full pattern
            fDateFormat.applyPattern(fullPattern);
        }
        return appendTo;
}
 
Example 7
Source File: QuantityFormatter.java    From j2objc 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 8
Source File: MeasureFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Able to format Collection&lt;? extends Measure&gt;, 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&lt;? extends Measure&gt;, 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)
 */
@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;
}
 
Example 9
Source File: MeasureFormat.java    From j2objc 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 10
Source File: DateIntervalFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private final StringBuffer fallbackFormat(Calendar fromCalendar,
                                          Calendar toCalendar,
                                          boolean fromToOnSameDay,
                                          StringBuffer appendTo,
                                          FieldPosition pos)  {
        String fullPattern = null; // for saving the pattern in fDateFormat
        boolean formatDatePlusTimeRange = (fromToOnSameDay && fDatePattern != null && fTimePattern != null);
        // the fall back
        if (formatDatePlusTimeRange) {
            fullPattern = fDateFormat.toPattern(); // save current pattern, restore later
            fDateFormat.applyPattern(fTimePattern);
        }
        FieldPosition otherPos = new FieldPosition(pos.getField());
        StringBuffer earlierDate = new StringBuffer(64);
        earlierDate = fDateFormat.format(fromCalendar, earlierDate, pos);
        StringBuffer laterDate = new StringBuffer(64);
        laterDate = fDateFormat.format(toCalendar, laterDate, otherPos);
        String fallbackPattern = fInfo.getFallbackIntervalPattern();
        adjustPosition(fallbackPattern, earlierDate.toString(), pos, laterDate.toString(), otherPos, pos);
        String fallbackRange = SimpleFormatterImpl.formatRawPattern(
                fallbackPattern, 2, 2, earlierDate, laterDate);
        if (formatDatePlusTimeRange) {
            // fallbackRange has just the time range, need to format the date part and combine that
            fDateFormat.applyPattern(fDatePattern);
            StringBuffer datePortion = new StringBuffer(64);
            otherPos.setBeginIndex(0);
            otherPos.setEndIndex(0);
            datePortion = fDateFormat.format(fromCalendar, datePortion, otherPos);
            adjustPosition(fDateTimeFormat, fallbackRange, pos, datePortion.toString(), otherPos, pos);
            fallbackRange = SimpleFormatterImpl.formatRawPattern(
                    fDateTimeFormat, 2, 2, fallbackRange, datePortion);
        }
        appendTo.append(fallbackRange);
        if (formatDatePlusTimeRange) {
            // restore full pattern
            fDateFormat.applyPattern(fullPattern);
        }
        return appendTo;
}
 
Example 11
Source File: AngleFormat.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code *_FIELD} constant for the given field position, or -1 if none.
 */
private static int getField(final FieldPosition position) {
    if (position != null) {
        final Format.Field field = position.getFieldAttribute();
        if (field instanceof Field) {
            return ((Field) field).field;
        }
        return position.getField();
    }
    return -1;
}
 
Example 12
Source File: RangeFormat.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code *_FIELD} constant for the given field position, or -1 if none.
 */
private static int getField(final FieldPosition position) {
    if (position != null) {
        final Format.Field field = position.getFieldAttribute();
        if (field instanceof Field) {
            return ((Field) field).field;
        }
        return position.getField();
    }
    return -1;
}