Package org.opencms.file.collectors
Class CmsDateResourceComparator
java.lang.Object
org.opencms.file.collectors.CmsDateResourceComparator
- All Implemented Interfaces:
Comparator<CmsResource>
Comparator for sorting resource objects based on dates.
The comparator can be configured to use any date based on resource attributes or properties.
The user must in the constructor CmsDateResourceComparator(CmsObject, List, boolean)
provide a list of one or more date identifiers that should be checked, in the order they
should be checked. This list of dates identifiers must be Strings which tell the comparator which dates to use.
The first valid date identifier that is found for a resource is used as date for
comparing this resource to other resources.
The following date identifiers can be used
to access the corresponding value of a CmsResource
:
"dateCreated"
, which meansCmsResource.getDateCreated()
."dateLastModified"
, which meansCmsResource.getDateLastModified()
."dateContent"
, which meansCmsResource.getDateContent()
."dateReleased"
, which meansCmsResource.getDateReleased()
."dateExpired"
, which meansCmsResource.getDateExpired()
.- Anything else will be treated as property name, so it will be attempted to read a property
with that name from the resource, and convert that value to a long. Should this fail
for any reason, the next entry from the list of dates will be processed.
- If no match is found at all,
CmsResource.getDateCreated()
is used as default.
Comparator
for resources and as comparator key for the resource
at the same time. Uses lazy initializing of comparator keys for a resource.- Since:
- 6.0.0
-
Field Summary
Modifier and TypeFieldDescriptionPossible keywords to read dates from the resource attributes in a List. -
Constructor Summary
ConstructorDescriptionCmsDateResourceComparator
(CmsObject cms, List<String> dateIdentifiers, boolean asc) Creates a new instance of this comparator key. -
Method Summary
Modifier and TypeMethodDescriptionstatic long
calculateDate
(CmsObject cms, CmsResource resource, List<String> dateIdentifiers, long defaultValue) Calculates the date to use for comparison of this resource based on the given date identifiers.int
compare
(CmsResource res0, CmsResource res1) long
getDate()
Returns the date of this resource comparator key.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
DATE_ATTRIBUTES_LIST
Possible keywords to read dates from the resource attributes in a List.
-
-
Constructor Details
-
CmsDateResourceComparator
Creates a new instance of this comparator key.- Parameters:
cms
- the current OpenCms user contextdateIdentifiers
- the names of the dates to checkasc
- if true, the date sort order is ascending, otherwise descending
-
-
Method Details
-
calculateDate
public static long calculateDate(CmsObject cms, CmsResource resource, List<String> dateIdentifiers, long defaultValue) Calculates the date to use for comparison of this resource based on the given date identifiers.- Parameters:
cms
- the current OpenCms user contextresource
- the resource to create the key fordateIdentifiers
- the date identifiers to use for selecting the datedefaultValue
- the default value to use in case no value can be calculated- Returns:
- the calculated date
- See Also:
-
compare
- Specified by:
compare
in interfaceComparator<CmsResource>
- See Also:
-
getDate
Returns the date of this resource comparator key.- Returns:
- the date of this resource comparator key
-