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()
.
Regarding the of:xxxToList()
functions; <ui:repeat>
(and <h:dataTable>
)
doesn't support Iterable
(such as Set
) and Map
directly, so those functions may be
handy for them. If however EL 2.2 is used, then e.g. #{bean.set.toArray()}
(for Collection
types) and
#{bean.map.entrySet().toArray()}
could be used instead.
But if EL 2.2 is not supported or for a general Iterable
the provided EL functions can be used.
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)
.
Note that since JSF 2.2 it should be possible to use #{bean.set}
directly in <h:dataTable>
, but not
in <ui:repeat>
.
The of:setToList()
thus remains useful for JSF 2.0 and 2.1 in all cases, but is not needed for
<h:dataTable>
in JSF 2.2.
IterableDataModel
,
Json
Modifier and Type | Method and Description |
---|---|
static String |
convert(String converterId,
Object object)
Convert given object to string using
Converter.getAsString(FacesContext, UIComponent, Object) of the
converter identified by the given converter ID, invoked with FacesContext.getCurrentInstance() ,
UIComponent.getCurrentComponent(FacesContext) and given object as arguments. |
static <E> List<E> |
iterableToList(Iterable<E> iterable)
Converts a
Iterable<E> to a List<E> . |
static <E> DataModel<E> |
iterableToModel(Iterable<E> iterable)
Converts an
Iterable<E> to a DataModel<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> 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.
|
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>> . |
static String |
printStackTrace(Throwable exception)
Print the stack trace of the given exception.
|
static <E> List<E> |
setToList(Set<E> set)
Converts a
Set<E> to a List<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.
|
public static <E> List<E> setToList(Set<E> set)
Set<E>
to a List<E>
. Useful when you want to iterate over a
Set
in for example <ui:repeat>
.E
- The generic set element type.set
- The set to be converted to list of its entries.public static <K,V> List<Map.Entry<K,V>> mapToList(Map<K,V> map)
Map<K, V>
to a List<Map.Entry<K, V>>
. Useful when you want
to iterate over a Map
in for example <ui:repeat>
. Each of the entries has the
usual getKey()
and getValue()
methods.K
- The generic map key type.V
- The generic map value type.map
- The map to be converted to list of its entries.public static <E> List<E> iterableToList(Iterable<E> iterable)
Iterable<E>
to a List<E>
. Useful when you want to iterate over an
Iterable
, which includes any type of Collection
(which includes e.g. a Set
)
in for example <ui:repeat>
and <h:dataTable>
.
When iterating specifically over a Set using the above mentioned components setToList(Set)
is
an alternative to this.
E
- The generic iterable element type.iterable
- The Iterable to be converted to a List.public static <E> DataModel<E> iterableToModel(Iterable<E> iterable)
Iterable<E>
to a DataModel<E>
. Useful when you want to iterate over an
Iterable
, which includes any type of Collection
(which includes e.g. a Set
)
in for example <ui:repeat>
and <h:dataTable>
.
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.
E
- The generic iterable element type.iterable
- The Iterable to be converted to a DataModel.public static String joinArray(Object array, String separator)
array
- The array to be joined.separator
- The separator to be used. If null, then it defaults to empty string.IllegalArgumentException
- When the given array is not an array at all.public static <E> String joinCollection(Collection<E> collection, String separator)
E
- The generic collection element type.collection
- The collection to be joined.separator
- The separator to be used. If null, then it defaults to empty string.public static <K,V> String joinMap(Map<K,V> map, String pairSeparator, String entrySeparator)
K
- The generic map key type.V
- The generic map value type.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.public static Object[][] splitArray(Object array, int fragmentSize)
<ui:repeat>
structures, for example, when positioning a list of items into a grid based
layout system such as Twitter Bootstrap.array
- The array to be split.fragmentSize
- The size of each subarray.IllegalArgumentException
- When the fragment size is less than 1.public static <E> List<List<E>> splitList(List<E> list, int fragmentSize)
<ui:repeat>
structures, for example, when positioning a list of items into a grid based
layout system such as Twitter Bootstrap.E
- The generic list element type.list
- The list to be split.fragmentSize
- The size of each sublist.IllegalArgumentException
- When the fragment size is less than 1.public static String toJson(Object object)
Json.encode(Object)
.object
- Object to be encoded as JSON.Json.encode(Object)
public static String printStackTrace(Throwable exception)
exception
- The exception to print the stack trace for.public static String convert(String converterId, Object object)
Converter.getAsString(FacesContext, UIComponent, Object)
of the
converter identified by the given converter ID, invoked with FacesContext.getCurrentInstance()
,
UIComponent.getCurrentComponent(FacesContext)
and given object as arguments.converterId
- The converter ID of the desired converter instance.object
- The object to be converted to String
by the converter identified by given converter ID.Converter.getAsString(FacesContext, UIComponent, Object)
of the converter
identified by the given converter ID, invoked with FacesContext.getCurrentInstance()
,
UIComponent.getCurrentComponent(FacesContext)
and given object as arguments.Copyright © 2012–2021 OmniFaces. All rights reserved.