@DefaultKey(value="collection") public class CollectionTool extends SafeConfig implements Serializable
CollectionTool allows a user to sort a collection (or array, iterator, etc) on any arbitrary set of properties exposed by the objects contained within the collection, and to generate arrays by splitting strings.
The sort functionality is specifically designed to use within a #foreach but you may find other uses for it.
The sort methods can handle all of the collection types supported by #foreach and the same constraints apply as well as the following. Every object in the collection must support the set of properties selected to sort on. Each property which is to be sorted on must return one of the follow:
During the sort operation all properties are compared by calling compareTo() with the exception of Strings for which compareToIgnoreCase() is called.
The sort is performed by calling Collections.sort() after marshalling the collection to sort into an appropriate collection type. The original collection will not be re-ordered; a new list containing the sorted elements will always be returned.
The tool is used as follows:
Single Property Sort #foreach($obj in $sorter.sort($objects, "name")) $obj.name Ordinal= $obj.ordinal #end End Multiple Property Sort #foreach($obj in $sorter.sort($objects, ["name", "ordinal"])) $obj.name, $obj.ordinal #end End
The sort method takes two parameters, a collection and a property name or an array of property names. The property names and corresponding methods must conform to java bean standards since commons-beanutils is used to extract the property values.
By default the sort tool sorts ascending, you can override this by adding a sort type suffix to any property name.
The supported suffixes are:
For ascending :asc For descending :desc Example #foreach($obj in $sorter.sort($objects, ["name:asc", "ordinal:desc"])) $obj.name, $obj.ordinal #end
This will sort first by Name in ascending order and then by Ordinal in descending order, of course you could have left the :asc off of the 'Name' property as ascending is always the default.
Example tools.xml config (if you want to use this with VelocityView):
<tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.SortTool"/> </toolbox> </tools>
Modifier and Type | Class and Description |
---|---|
static class |
CollectionTool.PropertiesComparator
Does all of the comparisons
|
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_STRINGS_DELIMITER |
static boolean |
DEFAULT_STRINGS_TRIM |
static String |
STRINGS_DELIMITER_FORMAT_KEY |
static String |
STRINGS_TRIM_KEY |
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
CollectionTool() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure(ValueParser values)
Does the actual configuration.
|
protected static Comparable |
getComparable(Object object,
String property)
Safely retrieves the comparable value for the specified property
from the specified object.
|
String |
getStringsDelimiter()
Gets the configured strings delimiter
|
boolean |
getStringsTrim()
Gets whether to trim strings
|
protected Collection |
internalSort(List list,
List properties)
Internal sorting method.
|
protected void |
setStringsDelimiter(String stringsDelimiter)
Sets the delimiter used for separating values in a single String value.
|
protected void |
setStringsTrim(boolean stringsTrim)
Sets whether strings should be trimmed when separated from
a delimited string value.
|
Collection |
sort(Collection collection)
Sort a collection
|
<T> Collection<T> |
sort(Collection<T> c,
Comparator<T> comparator)
Sorts a Collection using a Comparator.
|
Collection |
sort(Collection collection,
List properties)
Sorts the collection on several properties.
|
Collection |
sort(Map map)
Sort map values
|
<T> Collection<T> |
sort(Map<?,T> map,
Comparator<T> comparator)
Sorts a Map's values using a Comparator.
|
Collection |
sort(Map map,
List properties)
Sorts map values on several properties.
|
Collection |
sort(Object object)
Sort a collection, array or map
|
Collection |
sort(Object[] array)
Sort an array
|
Collection |
sort(Object[] array,
List properties)
Sorts array on several properties.
|
Collection<?> |
sort(Object o,
Comparator<?> comparator)
Sorts a Collection (or array, or Map's values)
using a Comparator.
|
Collection |
sort(Object object,
String property)
Sorts the collection on a single property.
|
<T> T[] |
sort(T[] a,
Comparator<T> comparator)
Sorts an array using a Comparator.
|
String[] |
split(String value) |
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final String STRINGS_DELIMITER_FORMAT_KEY
public static final String STRINGS_TRIM_KEY
public static final String DEFAULT_STRINGS_DELIMITER
public static final boolean DEFAULT_STRINGS_TRIM
protected final void setStringsDelimiter(String stringsDelimiter)
stringsDelimiter
- strings delimitersplit(String)
public final String getStringsDelimiter()
protected final void setStringsTrim(boolean stringsTrim)
stringsTrim
- flag valuesplit(String)
public final boolean getStringsTrim()
protected void configure(ValueParser values)
configure
in class SafeConfig
values
- configuration valuespublic String[] split(String value)
value
- the value to be convertedpublic <T> Collection<T> sort(Collection<T> c, Comparator<T> comparator)
T
- collection content classc
- The Collection to sort.comparator
- The comparator to use for sorting.public <T> T[] sort(T[] a, Comparator<T> comparator)
T
- array content classa
- The array to sort.comparator
- The comparator to use for sorting.public <T> Collection<T> sort(Map<?,T> map, Comparator<T> comparator)
T
- Map values classmap
- The Map whose values should be sorted.comparator
- The comparator to use for sorting.public Collection<?> sort(Object o, Comparator<?> comparator)
null
return value.o
- The Collection to sort.comparator
- The comparator to use for sorting.public Collection sort(Collection collection)
collection
- collection to sort, left unchangedpublic Collection sort(Object[] array)
array
- array to sort, left unchangedpublic Collection sort(Map map)
map
- map to sortpublic Collection sort(Object object)
object
- collection, array or map to sortpublic Collection sort(Object object, String property)
object
- the collection to be sorted, left unchanged.property
- the property to sort on.public Collection sort(Collection collection, List properties)
collection
- the collection to be sorted, left unchanged.properties
- the properties to sort on.public Collection sort(Map map, List properties)
map
- the map to be sorted, left unchanged.properties
- the properties to sort on.public Collection sort(Object[] array, List properties)
array
- array to be sorted, left unchanged.properties
- the properties to sort on.protected Collection internalSort(List list, List properties)
list
- values to sort.properties
- the properties to sort on.protected static Comparable getComparable(Object object, String property)
object
- target objectproperty
- target propertyCopyright © 2002–2018 The Apache Software Foundation. All rights reserved.