Package com.saperion.common.lang.format
Class CollectionFormatter
- java.lang.Object
-
- com.saperion.common.lang.format.CollectionFormatter
-
- All Implemented Interfaces:
FormatConstants
public class CollectionFormatter extends java.lang.Object implements FormatConstants
CollectionFormatteris a standard formatter for arrays,Collections andMaps 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
nullin 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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.lang.Stringformat(java.util.Collection<T> collection, boolean verbose)Formats the specifiedCollection, verbose or not (as specified) into the standardcollection-format.static <T> java.lang.Stringformat(java.util.Collection<T> collection, boolean verbose, java.lang.String header)Formats the specifiedCollection, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.static <T1,T2>
java.lang.Stringformat(java.util.Map<T1,T2> map, boolean verbose)Formats the specifiedMap, verbose or not (as specified)into the standardcollection-formatsorting its elements.static <T1,T2>
java.lang.Stringformat(java.util.Map<T1,T2> map, boolean verbose, java.lang.String header)Formats the specifiedMap, verbose or not (as specified) optionally prefixed with the specified header into the standardcollection-format.static <T> java.lang.Stringformat(T[] array, boolean verbose)Formats the specified array, verbose or not (as specified) into the standardcollection-format.static <T> java.lang.Stringformat(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 standardcollection-format.static <T extends java.lang.Comparable<? super T>>
java.lang.StringformatSorted(java.util.Collection<T> collection, boolean verbose)Formats the specifiedCollection, verbose or not (as specified)into the standardcollection-formatsorting its elements.static <T extends java.lang.Comparable<? super T>>
java.lang.StringformatSorted(java.util.Collection<T> collection, boolean verbose, java.lang.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>
java.lang.StringformatSorted(java.util.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>
java.lang.StringformatSorted(java.util.Map<T1,T2> map, boolean verbose, java.lang.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 java.lang.Comparable<? super T>>
java.lang.StringformatSorted(T[] array, boolean verbose)Formats the specified array, verbose or not (as specified) into the standardcollection-formatsorting its elements.static <T extends java.lang.Comparable<? super T>>
java.lang.StringformatSorted(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 standardcollection-formatsorting its elements.
-
-
-
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 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
public static <T> java.lang.String format(T[] array, boolean verbose)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 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 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
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 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
public static <T> java.lang.String format(java.util.Collection<T> collection, boolean verbose, java.lang.String header)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
public static <T> java.lang.String format(java.util.Collection<T> collection, boolean verbose)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 java.lang.Comparable<? super T>> java.lang.String formatSorted(java.util.Collection<T> collection, boolean verbose, java.lang.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 java.lang.Comparable<? super T>> java.lang.String formatSorted(java.util.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
public static <T1,T2> java.lang.String format(java.util.Map<T1,T2> map, boolean verbose, java.lang.String header)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
public static <T1,T2> java.lang.String format(java.util.Map<T1,T2> map, boolean verbose)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
public static <T1,T2> java.lang.String formatSorted(java.util.Map<T1,T2> map, boolean verbose, java.lang.String header)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
public static <T1,T2> java.lang.String formatSorted(java.util.Map<T1,T2> map, boolean verbose)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
-
-