Class ParameterFormatter


  • final class ParameterFormatter
    extends java.lang.Object
    Supports parameter formatting as used in ParameterizedMessage and ReusableParameterizedMessage.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.time.format.DateTimeFormatter DATE_FORMATTER  
      private static char DELIM_START  
      private static char DELIM_STOP  
      (package private) static java.lang.String ERROR_MSG_SEPARATOR
      Separator for error messages.
      (package private) static java.lang.String ERROR_PREFIX
      Prefix for errors.
      (package private) static java.lang.String ERROR_SEPARATOR
      Separator for errors.
      (package private) static java.lang.String ERROR_SUFFIX
      Suffix for errors.
      private static char ESCAPE_CHAR  
      (package private) static java.lang.String RECURSION_PREFIX
      Prefix for recursion.
      (package private) static java.lang.String RECURSION_SUFFIX
      Suffix for recursion.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ParameterFormatter()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void appendArray​(java.lang.Object o, java.lang.StringBuilder str, java.util.Set<java.lang.Object> dejaVu, java.lang.Class<?> oClass)  
      private static void appendCollection​(java.lang.Object o, java.lang.StringBuilder str, java.util.Set<java.lang.Object> dejaVu)
      Specialized handler for Collections.
      private static boolean appendDate​(java.lang.Object o, java.lang.StringBuilder str)  
      private static void appendMap​(java.lang.Object o, java.lang.StringBuilder str, java.util.Set<java.lang.Object> dejaVu)
      Specialized handler for Maps.
      private static void appendPotentiallyRecursiveValue​(java.lang.Object o, java.lang.StringBuilder str, java.util.Set<java.lang.Object> dejaVu)  
      private static boolean appendSpecialTypes​(java.lang.Object o, java.lang.StringBuilder str)  
      private static java.util.Set<java.lang.Object> cloneDejaVu​(java.util.Set<java.lang.Object> dejaVu)  
      (package private) static int countArgumentPlaceholders​(java.lang.String messagePattern)
      Counts the number of unescaped placeholders in the given messagePattern.
      (package private) static int countArgumentPlaceholders2​(java.lang.String messagePattern, int[] indices)
      Counts the number of unescaped placeholders in the given messagePattern.
      (package private) static int countArgumentPlaceholders3​(char[] messagePattern, int length, int[] indices)
      Counts the number of unescaped placeholders in the given messagePattern.
      private static java.util.Set<java.lang.Object> createDejaVu()  
      (package private) static java.lang.String deepToString​(java.lang.Object o)
      This method performs a deep toString of the given Object.
      (package private) static java.lang.String format​(java.lang.String messagePattern, java.lang.Object[] arguments)
      Replace placeholders in the given messagePattern with arguments.
      (package private) static void formatMessage​(java.lang.StringBuilder buffer, java.lang.String messagePattern, java.lang.Object[] arguments, int argCount)
      Replace placeholders in the given messagePattern with arguments.
      (package private) static void formatMessage2​(java.lang.StringBuilder buffer, java.lang.String messagePattern, java.lang.Object[] arguments, int argCount, int[] indices)
      Replace placeholders in the given messagePattern with arguments.
      (package private) static void formatMessage3​(java.lang.StringBuilder buffer, char[] messagePattern, int patternLength, java.lang.Object[] arguments, int argCount, int[] indices)
      Replace placeholders in the given messagePattern with arguments.
      private static java.util.Set<java.lang.Object> getOrCreateDejaVu​(java.util.Set<java.lang.Object> dejaVu)  
      private static void handleErrorInObjectToString​(java.lang.Object o, java.lang.StringBuilder str, java.lang.Throwable t)  
      private static void handleLastChar​(java.lang.StringBuilder buffer, int escapeCounter, char curChar)
      Processes the last unprocessed character and returns the resulting position in the result char array.
      private static void handleLiteralChar​(java.lang.StringBuilder buffer, int escapeCounter, char curChar)
      Processes a literal char (neither an '\' escape char nor a "{}" delimiter pair) and returns the resulting position.
      private static void handleRemainingCharIfAny​(java.lang.String messagePattern, int len, java.lang.StringBuilder buffer, int escapeCounter, int i)
      Detects whether the message pattern has been fully processed or if an unprocessed character remains and processes it if necessary, returning the resulting position in the result char array.
      (package private) static java.lang.String identityToString​(java.lang.Object obj)
      This method returns the same as if Object.toString() would not have been overridden in obj.
      private static boolean isDelimPair​(char curChar, java.lang.String messagePattern, int curCharIndex)
      Returns true if the specified char and the char at curCharIndex + 1 in the specified message pattern together form a "{}" delimiter pair, returns false otherwise.
      private static boolean isMaybeRecursive​(java.lang.Object o)
      Returns true if the specified object is an array, a Map or a Collection.
      private static boolean isOdd​(int number)
      Returns true if the specified parameter is odd.
      (package private) static void recursiveDeepToString​(java.lang.Object o, java.lang.StringBuilder str)
      This method performs a deep toString() of the given Object.
      private static void recursiveDeepToString​(java.lang.Object o, java.lang.StringBuilder str, java.util.Set<java.lang.Object> dejaVu)
      This method performs a deep toString() of the given Object.
      private static void tryObjectToString​(java.lang.Object o, java.lang.StringBuilder str)  
      private static void writeArgOrDelimPair​(java.lang.Object[] arguments, int argCount, int currentArgument, java.lang.StringBuilder buffer)
      Appends the argument at the specified argument index (or, if no such argument exists, the "{}" delimiter pair) to the specified result char array at the specified position and returns the resulting position.
      private static void writeDelimPair​(java.lang.StringBuilder buffer)
      Writes "{}" to the specified result array at the specified position and returns the resulting position.
      private static void writeEscapedEscapeChars​(int escapeCounter, java.lang.StringBuilder buffer)
      Writes a '\' char to the specified result array (starting at the specified position) for each pair of '\' escape chars encountered in the message format and returns the resulting position.
      private static void writeUnescapedEscapeChars​(int escapeCounter, java.lang.StringBuilder buffer)
      Writes the specified number of '\' chars to the specified result array (starting at the specified position) and returns the resulting position.
      • Methods inherited from class java.lang.Object

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

      • RECURSION_PREFIX

        static final java.lang.String RECURSION_PREFIX
        Prefix for recursion.
        See Also:
        Constant Field Values
      • RECURSION_SUFFIX

        static final java.lang.String RECURSION_SUFFIX
        Suffix for recursion.
        See Also:
        Constant Field Values
      • ERROR_PREFIX

        static final java.lang.String ERROR_PREFIX
        Prefix for errors.
        See Also:
        Constant Field Values
      • ERROR_SEPARATOR

        static final java.lang.String ERROR_SEPARATOR
        Separator for errors.
        See Also:
        Constant Field Values
      • ERROR_MSG_SEPARATOR

        static final java.lang.String ERROR_MSG_SEPARATOR
        Separator for error messages.
        See Also:
        Constant Field Values
      • ERROR_SUFFIX

        static final java.lang.String ERROR_SUFFIX
        Suffix for errors.
        See Also:
        Constant Field Values
      • DATE_FORMATTER

        private static final java.time.format.DateTimeFormatter DATE_FORMATTER
    • Constructor Detail

      • ParameterFormatter

        private ParameterFormatter()
    • Method Detail

      • countArgumentPlaceholders

        static int countArgumentPlaceholders​(java.lang.String messagePattern)
        Counts the number of unescaped placeholders in the given messagePattern.
        Parameters:
        messagePattern - the message pattern to be analyzed.
        Returns:
        the number of unescaped placeholders.
      • countArgumentPlaceholders2

        static int countArgumentPlaceholders2​(java.lang.String messagePattern,
                                              int[] indices)
        Counts the number of unescaped placeholders in the given messagePattern.
        Parameters:
        messagePattern - the message pattern to be analyzed.
        Returns:
        the number of unescaped placeholders.
      • countArgumentPlaceholders3

        static int countArgumentPlaceholders3​(char[] messagePattern,
                                              int length,
                                              int[] indices)
        Counts the number of unescaped placeholders in the given messagePattern.
        Parameters:
        messagePattern - the message pattern to be analyzed.
        Returns:
        the number of unescaped placeholders.
      • format

        static java.lang.String format​(java.lang.String messagePattern,
                                       java.lang.Object[] arguments)
        Replace placeholders in the given messagePattern with arguments.
        Parameters:
        messagePattern - the message pattern containing placeholders.
        arguments - the arguments to be used to replace placeholders.
        Returns:
        the formatted message.
      • formatMessage2

        static void formatMessage2​(java.lang.StringBuilder buffer,
                                   java.lang.String messagePattern,
                                   java.lang.Object[] arguments,
                                   int argCount,
                                   int[] indices)
        Replace placeholders in the given messagePattern with arguments.
        Parameters:
        buffer - the buffer to write the formatted message into
        messagePattern - the message pattern containing placeholders.
        arguments - the arguments to be used to replace placeholders.
      • formatMessage3

        static void formatMessage3​(java.lang.StringBuilder buffer,
                                   char[] messagePattern,
                                   int patternLength,
                                   java.lang.Object[] arguments,
                                   int argCount,
                                   int[] indices)
        Replace placeholders in the given messagePattern with arguments.
        Parameters:
        buffer - the buffer to write the formatted message into
        messagePattern - the message pattern containing placeholders.
        arguments - the arguments to be used to replace placeholders.
      • formatMessage

        static void formatMessage​(java.lang.StringBuilder buffer,
                                  java.lang.String messagePattern,
                                  java.lang.Object[] arguments,
                                  int argCount)
        Replace placeholders in the given messagePattern with arguments.
        Parameters:
        buffer - the buffer to write the formatted message into
        messagePattern - the message pattern containing placeholders.
        arguments - the arguments to be used to replace placeholders.
      • isDelimPair

        private static boolean isDelimPair​(char curChar,
                                           java.lang.String messagePattern,
                                           int curCharIndex)
        Returns true if the specified char and the char at curCharIndex + 1 in the specified message pattern together form a "{}" delimiter pair, returns false otherwise.
      • handleRemainingCharIfAny

        private static void handleRemainingCharIfAny​(java.lang.String messagePattern,
                                                     int len,
                                                     java.lang.StringBuilder buffer,
                                                     int escapeCounter,
                                                     int i)
        Detects whether the message pattern has been fully processed or if an unprocessed character remains and processes it if necessary, returning the resulting position in the result char array.
      • handleLastChar

        private static void handleLastChar​(java.lang.StringBuilder buffer,
                                           int escapeCounter,
                                           char curChar)
        Processes the last unprocessed character and returns the resulting position in the result char array.
      • handleLiteralChar

        private static void handleLiteralChar​(java.lang.StringBuilder buffer,
                                              int escapeCounter,
                                              char curChar)
        Processes a literal char (neither an '\' escape char nor a "{}" delimiter pair) and returns the resulting position.
      • writeDelimPair

        private static void writeDelimPair​(java.lang.StringBuilder buffer)
        Writes "{}" to the specified result array at the specified position and returns the resulting position.
      • isOdd

        private static boolean isOdd​(int number)
        Returns true if the specified parameter is odd.
      • writeEscapedEscapeChars

        private static void writeEscapedEscapeChars​(int escapeCounter,
                                                    java.lang.StringBuilder buffer)
        Writes a '\' char to the specified result array (starting at the specified position) for each pair of '\' escape chars encountered in the message format and returns the resulting position.
      • writeUnescapedEscapeChars

        private static void writeUnescapedEscapeChars​(int escapeCounter,
                                                      java.lang.StringBuilder buffer)
        Writes the specified number of '\' chars to the specified result array (starting at the specified position) and returns the resulting position.
      • writeArgOrDelimPair

        private static void writeArgOrDelimPair​(java.lang.Object[] arguments,
                                                int argCount,
                                                int currentArgument,
                                                java.lang.StringBuilder buffer)
        Appends the argument at the specified argument index (or, if no such argument exists, the "{}" delimiter pair) to the specified result char array at the specified position and returns the resulting position.
      • deepToString

        static java.lang.String deepToString​(java.lang.Object o)
        This method performs a deep toString of the given Object. Primitive arrays are converted using their respective Arrays.toString methods while special handling is implemented for "container types", i.e. Object[], Map and Collection because those could contain themselves.

        It should be noted that neither AbstractMap.toString() nor AbstractCollection.toString() implement such a behavior. They only check if the container is directly contained in itself, but not if a contained container contains the original one. Because of that, Arrays.toString(Object[]) isn't safe either. Confusing? Just read the last paragraph again and check the respective toString() implementation.

        This means, in effect, that logging would produce a usable output even if an ordinary System.out.println(o) would produce a relatively hard-to-debug StackOverflowError.

        Parameters:
        o - The object.
        Returns:
        The String representation.
      • recursiveDeepToString

        static void recursiveDeepToString​(java.lang.Object o,
                                          java.lang.StringBuilder str)
        This method performs a deep toString() of the given Object.

        Primitive arrays are converted using their respective Arrays.toString() methods, while special handling is implemented for container types, i.e. Object[], Map and Collection, because those could contain themselves.

        It should be noted that neither AbstractMap.toString() nor AbstractCollection.toString() implement such a behavior. They only check if the container is directly contained in itself, but not if a contained container contains the original one. Because of that, Arrays.toString(Object[]) isn't safe either. Confusing? Just read the last paragraph again and check the respective toString() implementation.

        This means, in effect, that logging would produce a usable output even if an ordinary System.out.println(o) would produce a relatively hard-to-debug StackOverflowError.

        Parameters:
        o - the Object to convert into a String
        str - the StringBuilder that o will be appended to
      • recursiveDeepToString

        private static void recursiveDeepToString​(java.lang.Object o,
                                                  java.lang.StringBuilder str,
                                                  java.util.Set<java.lang.Object> dejaVu)
        This method performs a deep toString() of the given Object.

        Primitive arrays are converted using their respective Arrays.toString() methods, while special handling is implemented for container types, i.e. Object[], Map and Collection, because those could contain themselves.

        dejaVu is used in case of those container types to prevent an endless recursion.

        It should be noted that neither AbstractMap.toString() nor AbstractCollection.toString() implement such a behavior. They only check if the container is directly contained in itself, but not if a contained container contains the original one. Because of that, Arrays.toString(Object[]) isn't safe either. Confusing? Just read the last paragraph again and check the respective toString() implementation.

        This means, in effect, that logging would produce a usable output even if an ordinary System.out.println(o) would produce a relatively hard-to-debug StackOverflowError.

        Parameters:
        o - the Object to convert into a String
        str - the StringBuilder that o will be appended to
        dejaVu - a set of container objects directly or transitively containing o
      • appendSpecialTypes

        private static boolean appendSpecialTypes​(java.lang.Object o,
                                                  java.lang.StringBuilder str)
      • appendDate

        private static boolean appendDate​(java.lang.Object o,
                                          java.lang.StringBuilder str)
      • isMaybeRecursive

        private static boolean isMaybeRecursive​(java.lang.Object o)
        Returns true if the specified object is an array, a Map or a Collection.
      • appendPotentiallyRecursiveValue

        private static void appendPotentiallyRecursiveValue​(java.lang.Object o,
                                                            java.lang.StringBuilder str,
                                                            java.util.Set<java.lang.Object> dejaVu)
      • appendArray

        private static void appendArray​(java.lang.Object o,
                                        java.lang.StringBuilder str,
                                        java.util.Set<java.lang.Object> dejaVu,
                                        java.lang.Class<?> oClass)
      • appendMap

        private static void appendMap​(java.lang.Object o,
                                      java.lang.StringBuilder str,
                                      java.util.Set<java.lang.Object> dejaVu)
        Specialized handler for Maps.
      • appendCollection

        private static void appendCollection​(java.lang.Object o,
                                             java.lang.StringBuilder str,
                                             java.util.Set<java.lang.Object> dejaVu)
        Specialized handler for Collections.
      • getOrCreateDejaVu

        private static java.util.Set<java.lang.Object> getOrCreateDejaVu​(java.util.Set<java.lang.Object> dejaVu)
      • createDejaVu

        private static java.util.Set<java.lang.Object> createDejaVu()
      • cloneDejaVu

        private static java.util.Set<java.lang.Object> cloneDejaVu​(java.util.Set<java.lang.Object> dejaVu)
      • tryObjectToString

        private static void tryObjectToString​(java.lang.Object o,
                                              java.lang.StringBuilder str)
      • handleErrorInObjectToString

        private static void handleErrorInObjectToString​(java.lang.Object o,
                                                        java.lang.StringBuilder str,
                                                        java.lang.Throwable t)
      • identityToString

        static java.lang.String identityToString​(java.lang.Object obj)
        This method returns the same as if Object.toString() would not have been overridden in obj.

        Note that this isn't 100% secure as collisions can always happen with hash codes.

        Copied from Object.hashCode():

        As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
        Parameters:
        obj - the Object that is to be converted into an identity string.
        Returns:
        the identity string as also defined in Object.toString()