Class CollectionFormatter

  • All Implemented Interfaces:
    FormatConstants

    public class CollectionFormatter
    extends java.lang.Object
    implements FormatConstants

    CollectionFormatter is a standard formatter for arrays, Collections and Maps to output general information about the collection together with information about its elements for debugging/logging/toString()-purposes.

    The output starts with information about the collection-class and its number of elements (using ToStringFormatter), followed by the elements surrounded by curly braces.

    There are two modes formatting the elements :

    • verbose: Every element is formatted on a new line and indented
    • non-verbose (flat): The elements are formatted simply one after the other

    One can specify an optional header, that will prefix the formatting result.

    There are two different groups of methods:

    • format(...): The elements will be formatted in the order in which they are contained in the collection.
    • formatSorted(...): The elements will be formatted in their natural order (they must be Comparable ). For maps instead the keys-value-pairs will be converted to Strings first and than these strings are sorted.

    The specified collection always may be null in which case a corresponding string will be returned.

    Author:
    agz
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.String format​(java.util.Collection<T> collection, boolean verbose)
      Formats the specified Collection, verbose or not (as specified) into the standard collection-format.
      static <T> java.lang.String format​(java.util.Collection<T> collection, boolean verbose, java.lang.String header)
      Formats the specified Collection, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format.
      static <T1,​T2>
      java.lang.String
      format​(java.util.Map<T1,​T2> map, boolean verbose)
      Formats the specified Map, verbose or not (as specified)into the standard collection-format sorting its elements.
      static <T1,​T2>
      java.lang.String
      format​(java.util.Map<T1,​T2> map, boolean verbose, java.lang.String header)
      Formats the specified Map, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format.
      static <T> java.lang.String format​(T[] array, boolean verbose)
      Formats the specified array, verbose or not (as specified) into the standard collection-format.
      static <T> java.lang.String format​(T[] array, boolean verbose, java.lang.String header)
      Formats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format.
      static <T extends java.lang.Comparable<? super T>>
      java.lang.String
      formatSorted​(java.util.Collection<T> collection, boolean verbose)
      Formats the specified Collection, verbose or not (as specified)into the standard collection-format sorting its elements.
      static <T extends java.lang.Comparable<? super T>>
      java.lang.String
      formatSorted​(java.util.Collection<T> collection, boolean verbose, java.lang.String header)
      Formats the specified Collection, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format sorting its elements.
      static <T1,​T2>
      java.lang.String
      formatSorted​(java.util.Map<T1,​T2> map, boolean verbose)
      Formats the specified Map, verbose or not (as specified) into the standard collection-format sorting its key-value-pairs.
      static <T1,​T2>
      java.lang.String
      formatSorted​(java.util.Map<T1,​T2> map, boolean verbose, java.lang.String header)
      Formats the specified Map, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format sorting its key-value-pairs.
      static <T extends java.lang.Comparable<? super T>>
      java.lang.String
      formatSorted​(T[] array, boolean verbose)
      Formats the specified array, verbose or not (as specified) into the standard collection-format sorting its elements.
      static <T extends java.lang.Comparable<? super T>>
      java.lang.String
      formatSorted​(T[] array, boolean verbose, java.lang.String header)
      Formats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format sorting its elements.
      • Methods inherited from class java.lang.Object

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

      • format

        public static <T> java.lang.String format​(T[] array,
                                                  boolean verbose,
                                                  java.lang.String header)
        Formats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format.
        Type Parameters:
        T - type of the elements
        Parameters:
        array - the array to format
        verbose - whether the result should be verbose
        header - optional header (may be null)
        Returns:
        the formatted string
      • format

        public static <T> java.lang.String format​(T[] array,
                                                  boolean verbose)
        Formats the specified array, verbose or not (as specified) into the standard collection-format.
        Type Parameters:
        T - type of the elements
        Parameters:
        array - the array to format
        verbose - whether the result should be verbose
        Returns:
        the formatted string
      • formatSorted

        public static <T extends java.lang.Comparable<? super T>> java.lang.String formatSorted​(T[] array,
                                                                                                boolean verbose,
                                                                                                java.lang.String header)
        Formats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format sorting its elements.
        Type Parameters:
        T - type of the elements
        Parameters:
        array - the array to format
        verbose - whether the result should be verbose
        header - optional header (may be null)
        Returns:
        the formatted string
      • formatSorted

        public static <T extends java.lang.Comparable<? super T>> java.lang.String formatSorted​(T[] array,
                                                                                                boolean verbose)
        Formats the specified array, verbose or not (as specified) into the standard collection-format sorting its elements.
        Type Parameters:
        T - type of the elements
        Parameters:
        array - the array to format
        verbose - whether the result should be verbose
        Returns:
        the formatted string
      • format

        public static <T> java.lang.String format​(java.util.Collection<T> collection,
                                                  boolean verbose,
                                                  java.lang.String header)
        Formats the specified Collection, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format.
        Type Parameters:
        T - type of the elements
        Parameters:
        collection - the Collection to format
        verbose - whether the result should be verbose
        header - optional header (may be null)
        Returns:
        the formatted string
      • format

        public static <T> java.lang.String format​(java.util.Collection<T> collection,
                                                  boolean verbose)
        Formats the specified Collection, verbose or not (as specified) into the standard collection-format.
        Type Parameters:
        T - type of the elements
        Parameters:
        collection - the Collection to format
        verbose - whether the result should be verbose
        Returns:
        the formatted string
      • formatSorted

        public static <T extends java.lang.Comparable<? super T>> java.lang.String formatSorted​(java.util.Collection<T> collection,
                                                                                                boolean verbose,
                                                                                                java.lang.String header)
        Formats the specified Collection, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format sorting its elements.
        Type Parameters:
        T - type of the elements
        Parameters:
        collection - the Collection to format
        verbose - whether the result should be verbose
        header - optional header (may be null)
        Returns:
        the formatted string
      • formatSorted

        public static <T extends java.lang.Comparable<? super T>> java.lang.String formatSorted​(java.util.Collection<T> collection,
                                                                                                boolean verbose)
        Formats the specified Collection, verbose or not (as specified)into the standard collection-format sorting its elements.
        Type Parameters:
        T - type of the elements
        Parameters:
        collection - the Collection to format
        verbose - whether the result should be verbose
        Returns:
        the formatted string
      • format

        public static <T1,​T2> java.lang.String format​(java.util.Map<T1,​T2> map,
                                                            boolean verbose,
                                                            java.lang.String header)
        Formats the specified Map, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format.
        Type Parameters:
        T1 - type of the keys
        T2 - type of the elements
        Parameters:
        map - the Map to format
        verbose - whether the result should be verbose
        header - optional header (may be null)
        Returns:
        the formatted string
      • format

        public static <T1,​T2> java.lang.String format​(java.util.Map<T1,​T2> map,
                                                            boolean verbose)
        Formats the specified Map, verbose or not (as specified)into the standard collection-format sorting its elements.
        Type Parameters:
        T1 - type of the keys
        T2 - type of the elements
        Parameters:
        map - the Map to format
        verbose - whether the result should be verbose
        Returns:
        the formatted string
      • formatSorted

        public static <T1,​T2> java.lang.String formatSorted​(java.util.Map<T1,​T2> map,
                                                                  boolean verbose,
                                                                  java.lang.String header)
        Formats the specified Map, verbose or not (as specified) optionally prefixed with the specified header into the standard collection-format sorting its key-value-pairs.
        Type Parameters:
        T1 - type of the keys
        T2 - type of the elements
        Parameters:
        map - the Map to format
        verbose - whether the result should be verbose
        header - optional header (may be null)
        Returns:
        the formatted string
      • formatSorted

        public static <T1,​T2> java.lang.String formatSorted​(java.util.Map<T1,​T2> map,
                                                                  boolean verbose)
        Formats the specified Map, verbose or not (as specified) into the standard collection-format sorting its key-value-pairs.
        Type Parameters:
        T1 - type of the keys
        T2 - type of the elements
        Parameters:
        map - the Map to format
        verbose - whether the result should be verbose
        Returns:
        the formatted string