Package com.saperion.common.lang.format
Class CollectionFormatter
java.lang.Object
com.saperion.common.lang.format.CollectionFormatter
- All Implemented Interfaces:
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 beComparable). For maps instead the keys-value-pairs will be converted toStrings 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
-
Field Summary
Fields inherited from interface com.saperion.common.lang.format.FormatConstants
LS, LS_LENGTH, NULL, TAB, TAB_LENGTH -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Stringformat(Collection<T> collection, boolean verbose) Formats the specifiedCollection, verbose or not (as specified) into the standardcollection-format.static <T> Stringformat(Collection<T> collection, boolean verbose, String header) Formats the specifiedCollection, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.static <T1,T2> String Formats the specifiedMap, verbose or not (as specified)into the standardcollection-formatsorting its elements.static <T1,T2> String Formats the specifiedMap, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.static <T> Stringformat(T[] array, boolean verbose) Formats the specified array, verbose or not (as specified) into the standardcollection-format.static <T> StringFormats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.static <T extends Comparable<? super T>>
StringformatSorted(Collection<T> collection, boolean verbose) Formats the specifiedCollection, verbose or not (as specified)into the standardcollection-formatsorting its elements.static <T extends Comparable<? super T>>
StringformatSorted(Collection<T> collection, boolean verbose, String header) Formats the specifiedCollection, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-formatsorting its elements.static <T1,T2> String formatSorted(Map<T1, T2> map, boolean verbose) Formats the specifiedMap, verbose or not (as specified) into the standardcollection-formatsorting its key-value-pairs.static <T1,T2> String formatSorted(Map<T1, T2> map, boolean verbose, String header) Formats the specifiedMap, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-formatsorting its key-value-pairs.static <T extends Comparable<? super T>>
StringformatSorted(T[] array, boolean verbose) Formats the specified array, verbose or not (as specified) into the standardcollection-formatsorting its elements.static <T extends Comparable<? super T>>
StringformatSorted(T[] array, boolean verbose, String header) Formats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-formatsorting its elements.
-
Method Details
-
format
Formats the specified array, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.- Type Parameters:
T- type of the elements- Parameters:
array- the array to formatverbose- whether the result should be verboseheader- optional header (may benull)- Returns:
- the formatted string
-
format
Formats the specified array, verbose or not (as specified) into the standardcollection-format.- Type Parameters:
T- type of the elements- Parameters:
array- the array to formatverbose- 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 standardcollection-formatsorting its elements.- Type Parameters:
T- type of the elements- Parameters:
array- the array to formatverbose- whether the result should be verboseheader- optional header (may benull)- Returns:
- the formatted string
-
formatSorted
Formats the specified array, verbose or not (as specified) into the standardcollection-formatsorting its elements.- Type Parameters:
T- type of the elements- Parameters:
array- the array to formatverbose- whether the result should be verbose- Returns:
- the formatted string
-
format
Formats the specifiedCollection, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.- Type Parameters:
T- type of the elements- Parameters:
collection- theCollectionto formatverbose- whether the result should be verboseheader- optional header (may benull)- Returns:
- the formatted string
-
format
Formats the specifiedCollection, verbose or not (as specified) into the standardcollection-format.- Type Parameters:
T- type of the elements- Parameters:
collection- theCollectionto formatverbose- 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 specifiedCollection, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-formatsorting its elements.- Type Parameters:
T- type of the elements- Parameters:
collection- theCollectionto formatverbose- whether the result should be verboseheader- optional header (may benull)- Returns:
- the formatted string
-
formatSorted
public static <T extends Comparable<? super T>> String formatSorted(Collection<T> collection, boolean verbose) Formats the specifiedCollection, verbose or not (as specified)into the standardcollection-formatsorting its elements.- Type Parameters:
T- type of the elements- Parameters:
collection- theCollectionto formatverbose- whether the result should be verbose- Returns:
- the formatted string
-
format
Formats the specifiedMap, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.- Type Parameters:
T1- type of the keysT2- type of the elements- Parameters:
map- theMapto formatverbose- whether the result should be verboseheader- optional header (may benull)- Returns:
- the formatted string
-
format
Formats the specifiedMap, verbose or not (as specified)into the standardcollection-formatsorting its elements.- Type Parameters:
T1- type of the keysT2- type of the elements- Parameters:
map- theMapto formatverbose- whether the result should be verbose- Returns:
- the formatted string
-
formatSorted
Formats the specifiedMap, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-formatsorting its key-value-pairs.- Type Parameters:
T1- type of the keysT2- type of the elements- Parameters:
map- theMapto formatverbose- whether the result should be verboseheader- optional header (may benull)- Returns:
- the formatted string
-
formatSorted
Formats the specifiedMap, verbose or not (as specified) into the standardcollection-formatsorting its key-value-pairs.- Type Parameters:
T1- type of the keysT2- type of the elements- Parameters:
map- theMapto formatverbose- whether the result should be verbose- Returns:
- the formatted string
-