本文整理汇总了Java中java.text.FieldPosition.setEndIndex方法的典型用法代码示例。如果您正苦于以下问题:Java FieldPosition.setEndIndex方法的具体用法?Java FieldPosition.setEndIndex怎么用?Java FieldPosition.setEndIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.FieldPosition
的用法示例。
在下文中一共展示了FieldPosition.setEndIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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;
}
示例3: format
import java.text.FieldPosition; //导入方法依赖的package包/类
@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;
}
示例4: selectPlural
import java.text.FieldPosition; //导入方法依赖的package包/类
/**
* 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);
}
示例5: 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<? extends Measure>, 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;
}
示例6: 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());
}
示例7: 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);
}
}
}
示例8: fallbackFormat
import java.text.FieldPosition; //导入方法依赖的package包/类
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;
}
示例9: addPadding
import java.text.FieldPosition; //导入方法依赖的package包/类
private final void addPadding(StringBuffer result, FieldPosition fieldPosition, int prefixLen,
int suffixLen) {
if (formatWidth > 0) {
int len = formatWidth - result.length();
if (len > 0) {
char[] padding = new char[len];
for (int i = 0; i < len; ++i) {
padding[i] = pad;
}
switch (padPosition) {
case PAD_AFTER_PREFIX:
result.insert(prefixLen, padding);
break;
case PAD_BEFORE_PREFIX:
result.insert(0, padding);
break;
case PAD_BEFORE_SUFFIX:
result.insert(result.length() - suffixLen, padding);
break;
case PAD_AFTER_SUFFIX:
result.append(padding);
break;
}
if (padPosition == PAD_BEFORE_PREFIX || padPosition == PAD_AFTER_PREFIX) {
fieldPosition.setBeginIndex(fieldPosition.getBeginIndex() + len);
fieldPosition.setEndIndex(fieldPosition.getEndIndex() + len);
}
}
}
}
示例10: addAttribute
import java.text.FieldPosition; //导入方法依赖的package包/类
/**
* [Spark/CDL] Use this method to add attribute.
*/
private void addAttribute(Field field, int begin, int end) {
FieldPosition pos = new FieldPosition(field);
pos.setBeginIndex(begin);
pos.setEndIndex(end);
attributes.add(pos);
}
示例11: updateMetaData
import java.text.FieldPosition; //导入方法依赖的package包/类
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;
}
示例12: if
import java.text.FieldPosition; //导入方法依赖的package包/类
/**
* This method returns the PluralRules type found from parsing.
* @param source the string to be parsed.
* @param pos defines the position where parsing is to begin,
* and upon return, the position where parsing left off. If the position
* is a negative index, then parsing failed.
* @return Returns the PluralRules type. For example, it could be "zero", "one", "two", "few", "many" or "other")
*/
/*package*/ String parseType(String source, RbnfLenientScanner scanner, FieldPosition pos) {
// If no pattern was applied, return null.
if (msgPattern == null || msgPattern.countParts() == 0) {
pos.setBeginIndex(-1);
pos.setEndIndex(-1);
return null;
}
int partIndex = 0;
int currMatchIndex;
int count=msgPattern.countParts();
int startingAt = pos.getBeginIndex();
if (startingAt < 0) {
startingAt = 0;
}
// The keyword is null until we need to match against a non-explicit, not-"other" value.
// Then we get the keyword from the selector.
// (In other words, we never call the selector if we match against an explicit value,
// or if the only non-explicit keyword is "other".)
String keyword = null;
String matchedWord = null;
int matchedIndex = -1;
// Iterate over (ARG_SELECTOR ARG_START message ARG_LIMIT) tuples
// until the end of the plural-only pattern.
while (partIndex < count) {
MessagePattern.Part partSelector=msgPattern.getPart(partIndex++);
if (partSelector.getType() != MessagePattern.Part.Type.ARG_SELECTOR) {
// Bad format
continue;
}
MessagePattern.Part partStart=msgPattern.getPart(partIndex++);
if (partStart.getType() != MessagePattern.Part.Type.MSG_START) {
// Bad format
continue;
}
MessagePattern.Part partLimit=msgPattern.getPart(partIndex++);
if (partLimit.getType() != MessagePattern.Part.Type.MSG_LIMIT) {
// Bad format
continue;
}
String currArg = pattern.substring(partStart.getLimit(), partLimit.getIndex());
if (scanner != null) {
// If lenient parsing is turned ON, we've got some time consuming parsing ahead of us.
int[] scannerMatchResult = scanner.findText(source, currArg, startingAt);
currMatchIndex = scannerMatchResult[0];
}
else {
currMatchIndex = source.indexOf(currArg, startingAt);
}
if (currMatchIndex >= 0 && currMatchIndex >= matchedIndex && (matchedWord == null || currArg.length() > matchedWord.length())) {
matchedIndex = currMatchIndex;
matchedWord = currArg;
keyword = pattern.substring(partStart.getLimit(), partLimit.getIndex());
}
}
if (keyword != null) {
pos.setBeginIndex(matchedIndex);
pos.setEndIndex(matchedIndex + matchedWord.length());
return keyword;
}
// Not found!
pos.setBeginIndex(-1);
pos.setEndIndex(-1);
return null;
}
示例13: format
import java.text.FieldPosition; //导入方法依赖的package包/类
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;
}
示例14: format
import java.text.FieldPosition; //导入方法依赖的package包/类
@Override
public StringBuffer format(
Color color,
StringBuffer toAppendTo,
FieldPosition pos)
{
// initialize
pos.setBeginIndex(0);
pos.setEndIndex(0);
// do not attempt to format null color
if (color == null)
return toAppendTo;
boolean inQuote = false; // true when between single quotes
char prevCh = 0; // previous pattern character
int count = 0; // number of time prevCh repeated
for (int i=0; i < _pattern.length(); ++i)
{
char ch = _pattern.charAt(i);
// Use subFormat() to format a repeated pattern character
// when a different pattern or non-pattern character is seen
if (ch != prevCh && count > 0)
{
toAppendTo = _subFormat(color, prevCh, count, toAppendTo);
count = 0;
}
if (ch == '\'')
{
// Consecutive single quotes are a single quote literal,
// either outside of quotes or between quotes
if ((i+1) < _pattern.length() &&
_pattern.charAt(i+1) == '\'')
{
toAppendTo.append('\'');
++i;
}
else
{
inQuote = !inQuote;
}
}
else if (!inQuote &&
(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'))
{
// ch is a date-time pattern character to be interpreted
// by subFormat(); count the number of times it is repeated
prevCh = ch;
++count;
}
else
{
// Append quoted characters and unquoted non-pattern characters
toAppendTo.append(ch);
}
}
// Format the last item in the pattern, if any
if (count > 0)
{
toAppendTo = _subFormat(color, prevCh, count, toAppendTo);
}
return toAppendTo;
}
示例15: format
import java.text.FieldPosition; //导入方法依赖的package包/类
@Override
public StringBuffer format(
Color color,
StringBuffer toAppendTo,
FieldPosition pos)
{
// initialize
pos.setBeginIndex(0);
pos.setEndIndex(0);
// do not attempt to format null color
if (color == null)
return toAppendTo;
boolean inQuote = false; // true when between single quotes
char prevCh = 0; // previous pattern character
int count = 0; // number of time prevCh repeated
for (int i=0; i < _pattern.length(); ++i)
{
char ch = _pattern.charAt(i);
// Use subFormat() to format a repeated pattern character
// when a different pattern or non-pattern character is seen
if (ch != prevCh && count > 0)
{
toAppendTo = _subFormat(color, prevCh, count, toAppendTo);
count = 0;
}
if (ch == '\'')
{
// Consecutive single quotes are a single quote literal,
// either outside of quotes or between quotes
if ((i+1) < _pattern.length() &&
_pattern.charAt(i+1) == '\'')
{
toAppendTo.append('\'');
++i;
}
else
{
inQuote = !inQuote;
}
}
else if (!inQuote &&
(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'))
{
// ch is a date-time pattern character to be interpreted
// by subFormat(); count the number of times it is repeated
prevCh = ch;
++count;
}
else
{
// Append quoted characters and unquoted non-pattern characters
toAppendTo.append(ch);
}
}
// Format the last item in the pattern, if any
if (count > 0)
{
toAppendTo = _subFormat(color, prevCh, count, toAppendTo);
}
return toAppendTo;
}