当前位置: 首页>>代码示例>>Java>>正文


Java FieldPosition.getEndIndex方法代码示例

本文整理汇总了Java中java.text.FieldPosition.getEndIndex方法的典型用法代码示例。如果您正苦于以下问题:Java FieldPosition.getEndIndex方法的具体用法?Java FieldPosition.getEndIndex怎么用?Java FieldPosition.getEndIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.text.FieldPosition的用法示例。


在下文中一共展示了FieldPosition.getEndIndex方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: format

import java.text.FieldPosition; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:19,代码来源:QuantityFormatter.java

示例2: formatMeasurePerUnit

import java.text.FieldPosition; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:36,代码来源:MeasureFormat.java

示例3: format

import java.text.FieldPosition; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:48,代码来源:MeasureFormat.java

示例4: formatMeasuresSlowTrack

import java.text.FieldPosition; //导入方法依赖的package包/类
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());
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:34,代码来源:MeasureFormat.java

示例5: adjustPosition

import java.text.FieldPosition; //导入方法依赖的package包/类
private void adjustPosition(String combiningPattern, // has {0} and {1} in it
                            String pat0, FieldPosition pos0, // pattern and pos corresponding to {0}
                            String pat1, FieldPosition pos1, // pattern and pos corresponding to {1}
                            FieldPosition posResult) {
    int index0 = combiningPattern.indexOf("{0}");
    int index1 = combiningPattern.indexOf("{1}");
    if (index0 < 0 || index1 < 0) {
        return;
    }
    int placeholderLen = 3; // length of "{0}" or "{1}"
    if (index0 < index1) {
        if (pos0.getEndIndex() > 0) {
            posResult.setBeginIndex(pos0.getBeginIndex() + index0);
            posResult.setEndIndex(pos0.getEndIndex() + index0);
        } else if (pos1.getEndIndex() > 0) {
            // here index1 >= 3
            index1 += pat0.length() - placeholderLen; // adjust for pat0 replacing {0}
            posResult.setBeginIndex(pos1.getBeginIndex() + index1);
            posResult.setEndIndex(pos1.getEndIndex() + index1);
        }
    } else {
        if (pos1.getEndIndex() > 0) {
            posResult.setBeginIndex(pos1.getBeginIndex() + index1);
            posResult.setEndIndex(pos1.getEndIndex() + index1);
        } else if (pos0.getEndIndex() > 0) {
            // here index0 >= 3
            index0 += pat1.length() - placeholderLen; // adjust for pat1 replacing {1}
            posResult.setBeginIndex(pos0.getBeginIndex() + index0);
            posResult.setEndIndex(pos0.getEndIndex() + index0);
        }
    }
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:33,代码来源:DateIntervalFormat.java

示例6: findText

import java.text.FieldPosition; //导入方法依赖的package包/类
/**
 * Searches a string for another string.  If lenient parsing is off,
 * this just calls indexOf().  If lenient parsing is on, this function
 * uses CollationElementIterator to match characters, and only
 * primary-order differences are significant in determining whether
 * there's a match.
 * @param str The string to search
 * @param key The string to search "str" for
 * @param startingAt The index into "str" where the search is to
 * begin
 * @return A two-element array of ints.  Element 0 is the position
 * of the match, or -1 if there was no match.  Element 1 is the
 * number of characters in "str" that matched (which isn't necessarily
 * the same as the length of "key")
 */
private int[] findText(String str, String key, PluralFormat pluralFormatKey, int startingAt) {
    RbnfLenientScanner scanner = formatter.getLenientScanner();
    if (pluralFormatKey != null) {
        FieldPosition position = new FieldPosition(NumberFormat.INTEGER_FIELD);
        position.setBeginIndex(startingAt);
        pluralFormatKey.parseType(str, scanner, position);
        int start = position.getBeginIndex();
        if (start >= 0) {
            int pluralRuleStart = ruleText.indexOf("$(");
            int pluralRuleSuffix = ruleText.indexOf(")$", pluralRuleStart) + 2;
            int matchLen = position.getEndIndex() - start;
            String prefix = ruleText.substring(0, pluralRuleStart);
            String suffix = ruleText.substring(pluralRuleSuffix);
            if (str.regionMatches(start - prefix.length(), prefix, 0, prefix.length())
                    && str.regionMatches(start + matchLen, suffix, 0, suffix.length()))
            {
                return new int[]{start - prefix.length(), matchLen + prefix.length() + suffix.length()};
            }
        }
        return new int[]{-1, 0};
    }

    if (scanner != null) {
        // if lenient parsing is turned ON, we've got some work
        // ahead of us
        return scanner.findText(str, key, startingAt);
    }
    // if lenient parsing is turned off, this is easy. Just call
    // String.indexOf() and we're done
    return new int[]{str.indexOf(key, startingAt), key.length()};
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:47,代码来源:NFRule.java

示例7: formatNumeric

import java.text.FieldPosition; //导入方法依赖的package包/类
private StringBuilder formatNumeric(
        Date duration,
        DateFormat formatter,
        DateFormat.Field smallestField,
        Number smallestAmount,
        StringBuilder appendTo) {
    // Format the smallest amount ahead of time.
    String smallestAmountFormatted;

    // Format the smallest amount using this object's number format, but keep track
    // of the integer portion of this formatted amount. We have to replace just the
    // integer part with the corresponding value from formatting the date. Otherwise
    // when formatting 0 minutes 9 seconds, we may get "00:9" instead of "00:09"
    FieldPosition intFieldPosition = new FieldPosition(NumberFormat.INTEGER_FIELD);
    smallestAmountFormatted = numberFormat.format(
            smallestAmount, new StringBuffer(), intFieldPosition).toString();
    // Give up if there is no integer field.
    if (intFieldPosition.getBeginIndex() == 0 && intFieldPosition.getEndIndex() == 0) {
        throw new IllegalStateException();
    }
    // Format our duration as a date, but keep track of where the smallest field is
    // so that we can use it to replace the integer portion of the smallest value.
    FieldPosition smallestFieldPosition = new FieldPosition(smallestField);
    String draft = formatter.format(
            duration, new StringBuffer(), smallestFieldPosition).toString();

    // If we find the smallest field
    if (smallestFieldPosition.getBeginIndex() != 0
            || smallestFieldPosition.getEndIndex() != 0) {
        // add everything up to the start of the smallest field in duration.
        appendTo.append(draft, 0, smallestFieldPosition.getBeginIndex());

        // add everything in the smallest field up to the integer portion
        appendTo.append(smallestAmountFormatted, 0, intFieldPosition.getBeginIndex());

        // Add the smallest field in formatted duration in lieu of the integer portion
        // of smallest field
        appendTo.append(
                draft,
                smallestFieldPosition.getBeginIndex(),
                smallestFieldPosition.getEndIndex());

        // Add the rest of the smallest field
        appendTo.append(
                smallestAmountFormatted,
                intFieldPosition.getEndIndex(),
                smallestAmountFormatted.length());
        appendTo.append(draft, smallestFieldPosition.getEndIndex(), draft.length());
    } else {
        // As fallback, just use the formatted duration.
        appendTo.append(draft);
    }
    return appendTo;
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:55,代码来源:MeasureFormat.java


注:本文中的java.text.FieldPosition.getEndIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。