Class Converters


  • public final class Converters
    extends Object

    Collection of EL functions for data conversion: of:iterableToList() (with alternative of:iterableToModel), of:setToList(), of:mapToList(), of:joinArray(), of:joinCollection(), of:joinMap(), of:splitArray(), of:splitList(), and of:toJson().

    The of:joinXxx() functions basically joins the elements of the array, collection or map to a string using the given separator. This may be helpful if you want to display the contents of a collection as a commaseparated string without the need for an <ui:repeat>.

    The of:splitXxx() functions basically splits an array or list into an array of subarrays or list of sublists of the given fragment size. This may be helpful if you want to create a two-dimensional matrix of a fixed width based on a single-dimensional array or list.

    The of:toJson() function encodes any object to a string in JSON format according the rules of Json.encode(Object).

    Author:
    Bauke Scholtz, Arjan Tijms, Radu Creanga <rdcrng@gmail.com>
    See Also:
    IterableDataModel, Json
    • Method Detail

      • setToList

        public static <E> List<E> setToList​(Set<E> set)
        Converts a Set<E> to a List<E>.
        Type Parameters:
        E - The generic set element type.
        Parameters:
        set - The set to be converted to list of its entries.
        Returns:
        The converted list.
      • mapToList

        public static <K,​V> List<Map.Entry<K,​V>> mapToList​(Map<K,​V> map)
        Converts a Map<K, V> to a List<Map.Entry<K, V>>. Each of the entries has the usual getKey() and getValue() methods.
        Type Parameters:
        K - The generic map key type.
        V - The generic map value type.
        Parameters:
        map - The map to be converted to list of its entries.
        Returns:
        The converted list.
      • iterableToList

        public static <E> List<E> iterableToList​(Iterable<E> iterable)
        Converts a Iterable<E> to a List<E>.

        When iterating specifically over a Set using the above mentioned components setToList(Set) is an alternative to this.

        Type Parameters:
        E - The generic iterable element type.
        Parameters:
        iterable - The Iterable to be converted to a List.
        Returns:
        The converted List.
        Since:
        1.5
      • iterableToModel

        public static <E> DataModel<E> iterableToModel​(Iterable<E> iterable)
        Converts an Iterable<E> to a DataModel<E>.

        When iterating specifically over a Set using the above mentioned components setToList(Set) is an alternative to this. Use this for more general cases or when the exact collection type is unknown.

        For those same components iterableToList(Iterable) is another alternative. Use this when a DataModel is specifically needed.

        Type Parameters:
        E - The generic iterable element type.
        Parameters:
        iterable - The Iterable to be converted to a DataModel.
        Returns:
        The converted DataModel.
        Since:
        1.5
      • joinArray

        public static String joinArray​(Object array,
                                       String separator)
        Joins all elements of the given array to a single string, separated by the given separator.
        Parameters:
        array - The array to be joined.
        separator - The separator to be used. If null, then it defaults to empty string.
        Returns:
        All elements of the given array as a single string, separated by the given separator.
        Throws:
        IllegalArgumentException - When the given array is not an array at all.
        Since:
        1.3
      • joinCollection

        public static <E> String joinCollection​(Collection<E> collection,
                                                String separator)
        Joins all elements of the given collection to a single string, separated by the given separator.
        Type Parameters:
        E - The generic collection element type.
        Parameters:
        collection - The collection to be joined.
        separator - The separator to be used. If null, then it defaults to empty string.
        Returns:
        All elements of the given collection as a single string, separated by the given separator.
        Since:
        1.3
      • joinMap

        public static <K,​V> String joinMap​(Map<K,​V> map,
                                                 String pairSeparator,
                                                 String entrySeparator)
        Joins all elements of the given map to a single string, separated by the given key-value pair separator and entry separator.
        Type Parameters:
        K - The generic map key type.
        V - The generic map value type.
        Parameters:
        map - The map to be joined.
        pairSeparator - The key-value pair separator to be used. If null, then it defaults to empty string.
        entrySeparator - The entry separator to be used. If null, then it defaults to empty string.
        Returns:
        All elements of the given map as a single string, separated by the given separators.
        Since:
        1.3
      • splitArray

        public static Object[][] splitArray​(Object array,
                                            int fragmentSize)
        Splits the given array into an array of subarrays of the given fragment size. This is useful for creating nested <ui:repeat> structures, for example, when positioning a list of items into a grid based layout system such as Twitter Bootstrap.
        Parameters:
        array - The array to be split.
        fragmentSize - The size of each subarray.
        Returns:
        A new array consisting of subarrays of the given array.
        Throws:
        IllegalArgumentException - When the fragment size is less than 1.
        Since:
        1.6
      • splitList

        public static <E> List<List<E>> splitList​(List<E> list,
                                                  int fragmentSize)
        Splits the given list into a list of sublists of the given fragment size. This is useful for creating nested <ui:repeat> structures, for example, when positioning a list of items into a grid based layout system such as Twitter Bootstrap.
        Type Parameters:
        E - The generic list element type.
        Parameters:
        list - The list to be split.
        fragmentSize - The size of each sublist.
        Returns:
        A new list consisting of sublists of the given list.
        Throws:
        IllegalArgumentException - When the fragment size is less than 1.
        Since:
        1.6
      • toJson

        public static String toJson​(Object object)
        Encode given object as JSON. Currently, this delegates directly to Json.encode(Object).
        Parameters:
        object - Object to be encoded as JSON.
        Returns:
        The encoded JSON string.
        Since:
        1.5
        See Also:
        Json.encode(Object)
      • printStackTrace

        public static String printStackTrace​(Throwable exception)
        Print the stack trace of the given exception.
        Parameters:
        exception - The exception to print the stack trace for.
        Returns:
        The printed stack trace.