Class CollectionFormatter

java.lang.Object
com.saperion.common.lang.format.CollectionFormatter
All Implemented Interfaces:
FormatConstants

public class CollectionFormatter extends 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 Details

    • format

      public static <T> String format(T[] array, boolean verbose, 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> 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 Comparable<? super T>> String formatSorted(T[] array, boolean verbose, 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 Comparable<? super T>> 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> String format(Collection<T> collection, boolean verbose, 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> String format(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 Comparable<? super T>> String formatSorted(Collection<T> collection, boolean verbose, 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 Comparable<? super T>> String formatSorted(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> String format(Map<T1,T2> map, boolean verbose, 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> String format(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> String formatSorted(Map<T1,T2> map, boolean verbose, 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> String formatSorted(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