java.text.FieldPosition#getField ( )源码实例Demo

下面列出了java.text.FieldPosition#getField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: fitnotifications   文件: MeasureFormat.java
/**
 * 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 项目: j2objc   文件: MeasureFormat.java
/**
 * 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;
}
 
源代码3 项目: fitnotifications   文件: QuantityFormatter.java
/**
 * 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);
}
 
源代码4 项目: fitnotifications   文件: MeasureFormat.java
/**
 * 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;
}
 
源代码5 项目: fitnotifications   文件: MeasureFormat.java
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());
}
 
源代码6 项目: fitnotifications   文件: DateIntervalFormat.java
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;
}
 
源代码7 项目: j2objc   文件: QuantityFormatter.java
/**
 * 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);
}
 
源代码8 项目: j2objc   文件: MeasureFormat.java
/**
 * 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;
}
 
源代码9 项目: j2objc   文件: MeasureFormat.java
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());
}
 
源代码10 项目: j2objc   文件: DateIntervalFormat.java
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;
}
 
源代码11 项目: sis   文件: AngleFormat.java
/**
 * 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;
}
 
源代码12 项目: sis   文件: RangeFormat.java
/**
 * 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;
}