Class SimpleFormatterImpl


  • public final class SimpleFormatterImpl
    extends java.lang.Object
    Formats simple patterns like "{1} was born in {0}". Internal version of 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 Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  SimpleFormatterImpl.IterInternal
      Internal iterator interface for maximum efficiency.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SimpleFormatterImpl()
      Use only static methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String compileToStringMinMaxArguments​(java.lang.CharSequence pattern, java.lang.StringBuilder sb, int min, int max)
      Creates a compiled form of the pattern string, for use with appropriate static methods.
      private static java.lang.StringBuilder format​(java.lang.String compiledPattern, java.lang.CharSequence[] values, java.lang.StringBuilder result, java.lang.String resultCopy, boolean forbidResultAsValue, int[] offsets)  
      static java.lang.StringBuilder formatAndAppend​(java.lang.String compiledPattern, java.lang.StringBuilder appendTo, int[] offsets, java.lang.CharSequence... values)
      Formats the given values, appending to the appendTo builder.
      static java.lang.StringBuilder formatAndReplace​(java.lang.String compiledPattern, java.lang.StringBuilder result, int[] offsets, java.lang.CharSequence... values)
      Formats the given values, replacing the contents of the result builder.
      static java.lang.String formatCompiledPattern​(java.lang.String compiledPattern, java.lang.CharSequence... values)
      Formats the given values.
      static int formatPrefixSuffix​(java.lang.String compiledPattern, java.text.Format.Field field, int start, int end, FormattedStringBuilder output)
      Special case for using FormattedStringBuilder with patterns with 0 or 1 argument.
      static java.lang.String formatRawPattern​(java.lang.String pattern, int min, int max, java.lang.CharSequence... values)
      Formats the not-compiled pattern with the given values.
      static int getArgumentLimit​(java.lang.String compiledPattern)  
      static int getLength​(java.lang.String compiledPattern, boolean codePoints)
      Returns the length of the pattern text with none of the arguments.
      static int getPrefixLength​(java.lang.String compiledPattern)
      Returns the length in code units of the pattern text up until the first argument.
      static java.lang.String getTextWithNoArguments​(java.lang.String compiledPattern)
      Returns the pattern text with none of the arguments.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ARG_NUM_LIMIT

        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).
        See Also:
        Constant Field Values
      • SEGMENT_LENGTH_ARGUMENT_CHAR

        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.
        See Also:
        Constant Field Values
      • MAX_SEGMENT_LENGTH

        private static final int MAX_SEGMENT_LENGTH
        Maximum length of a text segment. Longer segments are split into shorter ones.
        See Also:
        Constant Field Values
      • COMMON_PATTERNS

        private static final java.lang.String[][] COMMON_PATTERNS
        "Intern" some common patterns.
    • Constructor Detail

      • SimpleFormatterImpl

        private SimpleFormatterImpl()
        Use only static methods.
    • Method Detail

      • compileToStringMinMaxArguments

        public static java.lang.String compileToStringMinMaxArguments​(java.lang.CharSequence pattern,
                                                                      java.lang.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 - The pattern string.
        sb - A StringBuilder instance which may or may not be used.
        min - The pattern must have at least this many arguments.
        max - The pattern must have at most this many arguments.
        Returns:
        The compiled-pattern string.
        Throws:
        java.lang.IllegalArgumentException - for bad argument syntax and too few or too many arguments.
      • getArgumentLimit

        public static int getArgumentLimit​(java.lang.String compiledPattern)
        Parameters:
        compiledPattern - Compiled form of a pattern string.
        Returns:
        The max argument number + 1.
      • formatCompiledPattern

        public static java.lang.String formatCompiledPattern​(java.lang.String compiledPattern,
                                                             java.lang.CharSequence... values)
        Formats the given values.
        Parameters:
        compiledPattern - Compiled form of a pattern string.
      • formatRawPattern

        public static java.lang.String formatRawPattern​(java.lang.String pattern,
                                                        int min,
                                                        int max,
                                                        java.lang.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 - Not-compiled form of a pattern string.
        min - The pattern must have at least this many arguments.
        max - The pattern must have at most this many arguments.
        Returns:
        The compiled-pattern string.
        Throws:
        java.lang.IllegalArgumentException - for bad argument syntax and too few or too many arguments.
      • formatAndAppend

        public static java.lang.StringBuilder formatAndAppend​(java.lang.String compiledPattern,
                                                              java.lang.StringBuilder appendTo,
                                                              int[] offsets,
                                                              java.lang.CharSequence... values)
        Formats the given values, appending to the appendTo builder.
        Parameters:
        compiledPattern - Compiled form of a pattern string.
        appendTo - Gets the formatted pattern and values appended.
        offsets - 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 - 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:
        appendTo
      • formatAndReplace

        public static java.lang.StringBuilder formatAndReplace​(java.lang.String compiledPattern,
                                                               java.lang.StringBuilder result,
                                                               int[] offsets,
                                                               java.lang.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 - Compiled form of a pattern string.
        result - Gets its contents replaced by the formatted pattern and values.
        offsets - 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 - The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().
        Returns:
        result
      • getTextWithNoArguments

        public static java.lang.String getTextWithNoArguments​(java.lang.String compiledPattern)
        Returns the pattern text with none of the arguments. Like formatting with all-empty string values.
        Parameters:
        compiledPattern - Compiled form of a pattern string.
      • getLength

        public static int getLength​(java.lang.String compiledPattern,
                                    boolean codePoints)
        Returns the length of the pattern text with none of the arguments.
        Parameters:
        compiledPattern - Compiled form of a pattern string.
        codePoints - true to count code points; false to count code units.
        Returns:
        The number of code points or code units.
      • getPrefixLength

        public static int getPrefixLength​(java.lang.String compiledPattern)
        Returns the length in code units of the pattern text up until the first argument.
        Parameters:
        compiledPattern - Compiled form of a pattern string.
        Returns:
        The number of code units.
      • formatPrefixSuffix

        public static int formatPrefixSuffix​(java.lang.String compiledPattern,
                                             java.text.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 - Compiled form of a pattern string.
        field - Field to use when adding chars to the output.
        start - The start index of the argument already in the output string.
        end - The end index of the argument already in the output string.
        output - Destination for formatted output.
        Returns:
        Net number of characters added to the formatted string.
      • format

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