Class Converters
- java.lang.Object
-
- org.omnifaces.el.functions.Converters
-
public final class Converters extends Object
Collection of EL functions for data conversion:
of:iterableToList()
(with alternativeof:iterableToModel
),of:setToList()
,of:mapToList()
,of:joinArray()
,of:joinCollection()
,of:joinMap()
,of:splitArray()
,of:splitList()
, andof: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 ofJson.encode(Object)
.- Author:
- Bauke Scholtz, Arjan Tijms, Radu Creanga <rdcrng@gmail.com>
- See Also:
IterableDataModel
,Json
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
convert(String converterId, Object object)
Convert given object to string usingConverter.getAsString(FacesContext, UIComponent, Object)
of the converter identified by the given converter ID, invoked withFacesContext.getCurrentInstance()
,UIComponent.getCurrentComponent(FacesContext)
and given object as arguments.static <E> List<E>
iterableToList(Iterable<E> iterable)
Converts aIterable<E>
to aList<E>
.static <E> DataModel<E>
iterableToModel(Iterable<E> iterable)
Converts anIterable<E>
to aDataModel<E>
.static String
joinArray(Object array, String separator)
Joins all elements of the given array to a single string, separated by the given separator.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.static <K,V>
StringjoinMap(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.static <K,V>
List<Map.Entry<K,V>>mapToList(Map<K,V> map)
Converts aMap<K, V>
to aList<Map.Entry<K, V>>
.static String
printStackTrace(Throwable exception)
Print the stack trace of the given exception.static <E> List<E>
setToList(Set<E> set)
Converts aSet<E>
to aList<E>
.static Object[][]
splitArray(Object array, int fragmentSize)
Splits the given array into an array of subarrays of the given fragment size.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.static String
toJson(Object object)
Encode given object as JSON.
-
-
-
Method Detail
-
setToList
public static <E> List<E> setToList(Set<E> set)
Converts aSet<E>
to aList<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 aMap<K, V>
to aList<Map.Entry<K, V>>
. Each of the entries has the usualgetKey()
andgetValue()
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 aIterable<E>
to aList<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 anIterable<E>
to aDataModel<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 toJson.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.
-
convert
public static String convert(String converterId, Object object)
Convert given object to string usingConverter.getAsString(FacesContext, UIComponent, Object)
of the converter identified by the given converter ID, invoked withFacesContext.getCurrentInstance()
,UIComponent.getCurrentComponent(FacesContext)
and given object as arguments.- Parameters:
converterId
- The converter ID of the desired converter instance.object
- The object to be converted toString
by the converter identified by given converter ID.- Returns:
- The result of
Converter.getAsString(FacesContext, UIComponent, Object)
of the converter identified by the given converter ID, invoked withFacesContext.getCurrentInstance()
,UIComponent.getCurrentComponent(FacesContext)
and given object as arguments. - Since:
- 3.10
-
-