Package org.opencms.file.collectors
Class CmsDateResourceComparator
- java.lang.Object
-
- org.opencms.file.collectors.CmsDateResourceComparator
-
- All Implemented Interfaces:
java.util.Comparator<CmsResource>
public class CmsDateResourceComparator extends java.lang.Object implements java.util.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
Fields Modifier and Type Field Description static java.util.List<java.lang.String>
DATE_ATTRIBUTES_LIST
Possible keywords to read dates from the resource attributes in a List.
-
Constructor Summary
Constructors Constructor Description CmsDateResourceComparator(CmsObject cms, java.util.List<java.lang.String> dateIdentifiers, boolean asc)
Creates a new instance of this comparator key.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static long
calculateDate(CmsObject cms, CmsResource resource, java.util.List<java.lang.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.
-
-
-
Field Detail
-
DATE_ATTRIBUTES_LIST
public static final java.util.List<java.lang.String> DATE_ATTRIBUTES_LIST
Possible keywords to read dates from the resource attributes in a List.
-
-
Constructor Detail
-
CmsDateResourceComparator
public CmsDateResourceComparator(CmsObject cms, java.util.List<java.lang.String> dateIdentifiers, boolean asc)
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 Detail
-
calculateDate
public static long calculateDate(CmsObject cms, CmsResource resource, java.util.List<java.lang.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:
for a description about how the date identifieres are used
-
compare
public int compare(CmsResource res0, CmsResource res1)
- Specified by:
compare
in interfacejava.util.Comparator<CmsResource>
- See Also:
Comparator.compare(java.lang.Object, java.lang.Object)
-
getDate
public long getDate()
Returns the date of this resource comparator key.- Returns:
- the date of this resource comparator key
-
-