类 CollectionTool
- 所有已实现的接口:
Serializable
- 直接已知子类:
SortTool
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:
- Primitive type: e.g. int, char, long etc
- Standard Object: e.g. String, Integer, Long etc
- Object which implements the Comparable interface.
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>
- 从以下版本开始:
- VelocityTools 3.0
- 版本:
- $Id$
- 作者:
- S. Brett Sutton, Nathan Bubna
- 另请参阅:
-
嵌套类概要
嵌套类 -
字段概要
字段从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected voidconfigure(ValueParser values) Does the actual configuration.protected static ComparablegetComparable(Object object, String property) Safely retrieves the comparable value for the specified property from the specified object.final StringGets the configured strings delimiterfinal booleanGets whether to trim stringsprotected CollectioninternalSort(List list, List properties) Internal sorting method.protected final voidsetStringsDelimiter(String stringsDelimiter) Sets the delimiter used for separating values in a single String value.protected final voidsetStringsTrim(boolean stringsTrim) Sets whether strings should be trimmed when separated from a delimited string value.Sort a collection, array or mapSort an arraySorts array on several properties.Sorts the collection on a single property.Collection<?>sort(Object o, Comparator<?> comparator) Sorts a Collection (or array, or Map's values) using a Comparator.sort(Collection collection) Sort a collection<T> Collection<T>sort(Collection<T> c, Comparator<T> comparator) Sorts a Collection using a Comparator.sort(Collection collection, List properties) Sorts the collection on several properties.Sort map values<T> Collection<T>sort(Map<?, T> map, Comparator<T> comparator) Sorts a Map's values using a Comparator.Sorts map values on several properties.<T> T[]sort(T[] a, Comparator<T> comparator) Sorts an array using a Comparator.String[]从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
构造器详细资料
-
CollectionTool
public CollectionTool()
-
-
方法详细资料
-
setStringsDelimiter
Sets the delimiter used for separating values in a single String value. The default string delimiter is a comma.- 参数:
stringsDelimiter- strings delimiter- 另请参阅:
-
getStringsDelimiter
Gets the configured strings delimiter- 返回:
- strings delimiter
-
setStringsTrim
protected final void setStringsTrim(boolean stringsTrim) Sets whether strings should be trimmed when separated from a delimited string value. The default is true.- 参数:
stringsTrim- flag value- 另请参阅:
-
getStringsTrim
public final boolean getStringsTrim()Gets whether to trim strings- 返回:
- whether to trim strings
-
configure
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- 覆盖:
configure在类中SafeConfig- 参数:
values- configuration values
-
split
- 参数:
value- the value to be converted- 返回:
- an array of String objects containing all of the values derived from the specified array, Collection, or delimited String
-
sort
Sorts a Collection using a Comparator. A defensive copy is made of the Collection beforehand, so the original Collection is left untouched and null elements filtered out.- 类型参数:
T- collection content class- 参数:
c- The Collection to sort.comparator- The comparator to use for sorting.- 返回:
- A copy of the original Collection, sorted using the supplied Comparator.
- 从以下版本开始:
- VelocityTools 2.0.1
-
sort
Sorts an array using a Comparator. A defensive copy is made of the array beforehand, so the original array is left untouched and null elements filtered out.- 类型参数:
T- array content class- 参数:
a- The array to sort.comparator- The comparator to use for sorting.- 返回:
- A copy of the original array, sorted using the supplied Comparator.
- 从以下版本开始:
- VelocityTools 2.0.1
-
sort
Sorts a Map's values using a Comparator. A defensive copy is made of the values beforehand, so the original Map is left untouched.- 类型参数:
T- Map values class- 参数:
map- The Map whose values should be sorted.comparator- The comparator to use for sorting.- 返回:
- A copy of the original Map's values, sorted using the supplied Comparator.
- 从以下版本开始:
- VelocityTools 2.0.1
-
sort
Sorts a Collection (or array, or Map's values) using a Comparator. A defensive copy is made of the original beforehand, so the original is left untouched. Unsupported collection objects result in anullreturn value.- 参数:
o- The Collection to sort.comparator- The comparator to use for sorting.- 返回:
- A copy of the original Collection, sorted using the supplied Comparator.
- 从以下版本开始:
- VelocityTools 2.0.1
-
sort
Sort a collection- 参数:
collection- collection to sort, left unchanged- 返回:
- new sorted collection
-
sort
Sort an array- 参数:
array- array to sort, left unchanged- 返回:
- new sorted collection
-
sort
Sort map values- 参数:
map- map to sort- 返回:
- new sorted collection of map values
-
sort
Sort a collection, array or map- 参数:
object- collection, array or map to sort- 返回:
- new sorted collection, or null of object type is not supported
-
sort
Sorts the collection on a single property.- 参数:
object- the collection to be sorted, left unchanged.property- the property to sort on.- 返回:
- new collection, sorted.
-
sort
Sorts the collection on several properties.- 参数:
collection- the collection to be sorted, left unchanged.properties- the properties to sort on.- 返回:
- new collection, sorted.
-
sort
Sorts map values on several properties.- 参数:
map- the map to be sorted, left unchanged.properties- the properties to sort on.- 返回:
- new collection, sorted.
-
sort
Sorts array on several properties.- 参数:
array- array to be sorted, left unchanged.properties- the properties to sort on.- 返回:
- new collection, sorted.
-
internalSort
Internal sorting method.- 参数:
list- values to sort.properties- the properties to sort on.- 返回:
- new collection, sorted.
-
getComparable
Safely retrieves the comparable value for the specified property from the specified object. Subclasses that wish to perform more advanced, efficient, or just different property retrieval methods should override this method to do so.- 参数:
object- target objectproperty- target property- 返回:
- comparable
-