Top Description Inners Fields Constructors Methods
com.ibm.icu.impl

public final Class SimpleFormatterImpl

extends Object
Class Inheritance
Imports
java.io.IOException, java.text.Format, com.ibm.icu.util.ICUUncheckedIOException

Formats simple patterns like "{1} was born in {0}". Internal version of com.ibm.icu.text.SimpleFormatter with only static methods, to avoid wrapper objects.

This class "compiles" pattern strings into a binary format and implements formatting etc. based on that.

Format: Index 0: One more than the highest argument number. Followed by zero or more arguments or literal-text segments.

An argument is stored as its number, less than ARG_NUM_LIMIT. A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT, followed by that many chars.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SimpleFormatterImpl.IterInternal

Internal iterator interface for maximum efficiency.

Field Summary

Modifier and TypeField and Description
private static final int
ARG_NUM_LIMIT

Argument numbers must be smaller than this limit.

private static final String[][]
COMMON_PATTERNS

"Intern" some common patterns.

private static final char
private static final char
private static final char
private static final int
MAX_SEGMENT_LENGTH

Maximum length of a text segment.

private static final char
SEGMENT_LENGTH_ARGUMENT_CHAR

Initial and maximum char/UChar value set for a text segment.

Constructor Summary

AccessConstructor and Description
private
SimpleFormatterImpl()

Use only static methods.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

The compiled-pattern string.
compileToStringMinMaxArguments
(CharSequence
The pattern string.
pattern
,
StringBuilder
A StringBuilder instance which may or may not be used.
sb
,
int
The pattern must have at least this many arguments.
min
,
int
The pattern must have at most this many arguments.
max
)

Creates a compiled form of the pattern string, for use with appropriate static methods.

private static StringBuilder
format(String compiledPattern, CharSequence[] values, StringBuilder result, String resultCopy, boolean forbidResultAsValue, int[] offsets)

public static StringBuilder

Returns:

appendTo
formatAndAppend
(String
Compiled form of a pattern string.
compiledPattern
,
StringBuilder
Gets the formatted pattern and values appended.
appendTo
,
int[]
offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.
offsets
,
CharSequence...
The argument values. An argument value must not be the same object as appendTo. values.length must be at least getArgumentLimit(). Can be null if getArgumentLimit()==0.
values
)

Formats the given values, appending to the appendTo builder.

public static StringBuilder

Returns:

result
formatAndReplace
(String
Compiled form of a pattern string.
compiledPattern
,
StringBuilder
Gets its contents replaced by the formatted pattern and values.
result
,
int[]
offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.
offsets
,
CharSequence...
The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().
values
)

Formats the given values, replacing the contents of the result builder.

public static String
formatCompiledPattern(String
Compiled form of a pattern string.
compiledPattern
,
CharSequence... values)

Formats the given values.

public static int

Returns:

Net number of characters added to the formatted string.
formatPrefixSuffix
(String
Compiled form of a pattern string.
compiledPattern
,
Format.Field
Field to use when adding chars to the output.
field
,
int
The start index of the argument already in the output string.
start
,
int
The end index of the argument already in the output string.
end
,
FormattedStringBuilder
Destination for formatted output.
output
)

Special case for using FormattedStringBuilder with patterns with 0 or 1 argument.

public static String

Returns:

The compiled-pattern string.
formatRawPattern
(String
Not-compiled form of a pattern string.
pattern
,
int
The pattern must have at least this many arguments.
min
,
int
The pattern must have at most this many arguments.
max
,
CharSequence... values)

Formats the not-compiled pattern with the given values.

public static int

Returns:

The max argument number + 1.
getArgumentLimit
(String
Compiled form of a pattern string.
compiledPattern
)

public static int

Returns:

The number of code points or code units.
getLength
(String
Compiled form of a pattern string.
compiledPattern
,
boolean
true to count code points; false to count code units.
codePoints
)

Returns the length of the pattern text with none of the arguments.

public static int

Returns:

The number of code units.
getPrefixLength
(String
Compiled form of a pattern string.
compiledPattern
)

Returns the length in code units of the pattern text up until the first argument.

public static String
getTextWithNoArguments(String
Compiled form of a pattern string.
compiledPattern
)

Returns the pattern text with none of the arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ARG_NUM_LIMITback to summary
private static final int ARG_NUM_LIMIT

Argument numbers must be smaller than this limit. Text segment lengths are offset by this much. This is currently the only unused char value in compiled patterns, except it is the maximum value of the first unit (max arg +1).

COMMON_PATTERNSback to summary
private static final String[][] COMMON_PATTERNS

"Intern" some common patterns.

LEN1_CHARback to summary
private static final char LEN1_CHAR
LEN2_CHARback to summary
private static final char LEN2_CHAR
LEN3_CHARback to summary
private static final char LEN3_CHAR
MAX_SEGMENT_LENGTHback to summary
private static final int MAX_SEGMENT_LENGTH

Maximum length of a text segment. Longer segments are split into shorter ones.

SEGMENT_LENGTH_ARGUMENT_CHARback to summary
private static final char SEGMENT_LENGTH_ARGUMENT_CHAR

Initial and maximum char/UChar value set for a text segment. Segment length char values are from ARG_NUM_LIMIT+1 to this value here. Normally 0xffff, but can be as small as ARG_NUM_LIMIT+1 for testing.

Constructor Detail

SimpleFormatterImplback to summary
private SimpleFormatterImpl()

Use only static methods.

Method Detail

compileToStringMinMaxArgumentsback to summary
public static String compileToStringMinMaxArguments(CharSequence pattern, StringBuilder sb, int min, int max)

Creates a compiled form of the pattern string, for use with appropriate static methods. The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.

Parameters
pattern:CharSequence

The pattern string.

sb:StringBuilder

A StringBuilder instance which may or may not be used.

min:int

The pattern must have at least this many arguments.

max:int

The pattern must have at most this many arguments.

Returns:String

The compiled-pattern string.

Exceptions
IllegalArgumentException:
for bad argument syntax and too few or too many arguments.
formatback to summary
private static StringBuilder format(String compiledPattern, CharSequence[] values, StringBuilder result, String resultCopy, boolean forbidResultAsValue, int[] offsets)
formatAndAppendback to summary
public static StringBuilder formatAndAppend(String compiledPattern, StringBuilder appendTo, int[] offsets, CharSequence... values)

Formats the given values, appending to the appendTo builder.

Parameters
compiledPattern:String

Compiled form of a pattern string.

appendTo:StringBuilder

Gets the formatted pattern and values appended.

offsets:int[]

offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.

values:CharSequence[]

The argument values. An argument value must not be the same object as appendTo. values.length must be at least getArgumentLimit(). Can be null if getArgumentLimit()==0.

Returns:StringBuilder

appendTo

formatAndReplaceback to summary
public static StringBuilder formatAndReplace(String compiledPattern, StringBuilder result, int[] offsets, CharSequence... values)

Formats the given values, replacing the contents of the result builder. May optimize by actually appending to the result if it is the same object as the value corresponding to the initial argument in the pattern.

Parameters
compiledPattern:String

Compiled form of a pattern string.

result:StringBuilder

Gets its contents replaced by the formatted pattern and values.

offsets:int[]

offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.

values:CharSequence[]

The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().

Returns:StringBuilder

result

formatCompiledPatternback to summary
public static String formatCompiledPattern(String compiledPattern, CharSequence... values)

Formats the given values.

Parameters
compiledPattern:String

Compiled form of a pattern string.

formatPrefixSuffixback to summary
public static int formatPrefixSuffix(String compiledPattern, Format.Field field, int start, int end, FormattedStringBuilder output)

Special case for using FormattedStringBuilder with patterns with 0 or 1 argument. With 1 argument, treat the current contents of the FormattedStringBuilder between start and end as the argument {0}. Insert the extra strings from compiledPattern to surround the argument in the output. With 0 arguments, overwrite the entire contents of the FormattedStringBuilder between start and end.

Parameters
compiledPattern:String

Compiled form of a pattern string.

field:Format.Field

Field to use when adding chars to the output.

start:int

The start index of the argument already in the output string.

end:int

The end index of the argument already in the output string.

output:FormattedStringBuilder

Destination for formatted output.

Returns:int

Net number of characters added to the formatted string.

formatRawPatternback to summary
public static String formatRawPattern(String pattern, int min, int max, CharSequence... values)

Formats the not-compiled pattern with the given values. Equivalent to compileToStringMinMaxArguments() followed by formatCompiledPattern(). The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.

Parameters
pattern:String

Not-compiled form of a pattern string.

min:int

The pattern must have at least this many arguments.

max:int

The pattern must have at most this many arguments.

Returns:String

The compiled-pattern string.

Exceptions
IllegalArgumentException:
for bad argument syntax and too few or too many arguments.
getArgumentLimitback to summary
public static int getArgumentLimit(String compiledPattern)
Parameters
compiledPattern:String

Compiled form of a pattern string.

Returns:int

The max argument number + 1.

getLengthback to summary
public static int getLength(String compiledPattern, boolean codePoints)

Returns the length of the pattern text with none of the arguments.

Parameters
compiledPattern:String

Compiled form of a pattern string.

codePoints:boolean

true to count code points; false to count code units.

Returns:int

The number of code points or code units.

getPrefixLengthback to summary
public static int getPrefixLength(String compiledPattern)

Returns the length in code units of the pattern text up until the first argument.

Parameters
compiledPattern:String

Compiled form of a pattern string.

Returns:int

The number of code units.

getTextWithNoArgumentsback to summary
public static String getTextWithNoArguments(String compiledPattern)

Returns the pattern text with none of the arguments. Like formatting with all-empty string values.

Parameters
compiledPattern:String

Compiled form of a pattern string.

com.ibm.icu.impl back to summary

public Class SimpleFormatterImpl.IterInternal

extends Object
Class Inheritance

Internal iterator interface for maximum efficiency. Usage boilerplate:
long state = 0;
while (true) {
    state = IterInternal.step(state, compiledPattern, output);
    if (state == IterInternal.DONE) {
        break;
    }
    int argIndex = IterInternal.getArgIndex(state);
    // Append the string corresponding to argIndex to output
}

Field Summary

Modifier and TypeField and Description
public static final long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static int
getArgIndex(long state)

public static long
step(long state, CharSequence compiledPattern, Appendable output)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DONEback to summary
public static final long DONE

Constructor Detail

IterInternalback to summary
public IterInternal()

Method Detail

getArgIndexback to summary
public static int getArgIndex(long state)
stepback to summary
public static long step(long state, CharSequence compiledPattern, Appendable output)