Class ComparatorManager

  • All Implemented Interfaces:
    JPropertiesListener

    public class ComparatorManager
    extends java.lang.Object
    implements JPropertiesListener
    Manager of all Comparators used in JCMS to sort Storable instances and their subclass.

    You can declare a comparator in properties by following this syntax :

     comparator.{Type short name}.{comparator name}: {comparator class Fully Qualified Name}
     
    Example :
      comparator.Article.intro: com.example.jcmsplugin.ArticleIntroComparator
     

    When working with classes outside the generated package (such as Abstract class), the fully qualified name associated with {Type short name} is required and must be declared using this syntax :

     
      comparator.{Type short name}: {type class Fully Qualified Name}
     
    Example :
     
      comparator.AbstractCalendarEvent: com.jalios.jcms.calendar.AbstractCalendarEvent
      comparator.AbstractCalendarEvent.duration: com.example.jcmsplugin.CalendarEventDurationComparator
     
     public class CalendarEventDurationComparator<T extends AbstractCalendarEvent> implements Comparator<T>
    
      public int compare(T cal1, T cal2) {
        long cal1Duration = cal1 == null ? 0 : (cal1.getEndDate().getTime() - cal1.getStartDate().getTime());
        long cal2Duration = cal2 == null ? 0 : (cal2.getEndDate().getTime() - cal2.getStartDate().getTime());
        long diff = cal2Duration - cal1Duration;
        return diff != 0 ? (int) diff : IdComparator.compareStorableId(cal1, cal2); 
      }
     }
     

    Portlet Query Foreach comparators :

    Custom Comparators can be added to Portlet Query Foreach edit form.
    To add your comparator, declare I18N labels in your plugin language properties by following this syntax :

     comparator.pqf.{comparator name}: My Comparator
     
    Warning : You MUST ensure that your comparator name does not conflicts with any existing comparator declared for Publication type.

    The order of comparators displayed in the sort menu of the Portlet Query Foreach edit form can be controlled by modifying the following property

     # Comparator order for Portlet Query Foreach 
     comparator-pqf.prefered-order: \
       cdate mdate pdate edate sdate adate udate \
       title author opauthor workspace reader \
       type pstatus contentType dimensions size duration  
     
    Since:
    jcms-7.1.0
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends Storable>
      java.util.Comparator<? super T>
      getComparator​(java.lang.Class<T> clazz, java.lang.String comparatorName)
      Retrieve a new Comparator from its short identifier to sort instances of the specified class.
      static <T extends Storable>
      java.util.Comparator<? super T>
      getComparator​(java.lang.Class<T> typeClazz, java.lang.String comparatorName, boolean reverse)
      Retrieve a new Comparator from its short identifier to sort instances of the specified class.
      static <T extends Storable>
      java.util.Comparator<? super T>
      getComparator​(java.lang.Class<T> typeClazz, java.lang.String comparatorName, boolean reverse, java.util.Map<java.lang.String,​java.lang.Object> contextMap)
      Retrieve a new Comparator from its short identifier to sort instances of the specified class.
      static <T extends java.util.Comparator<?>,​V extends Storable>
      java.lang.Class<T>
      getComparatorClass​(java.lang.Class<V> typeClazz, java.lang.String comparatorName)  
      static java.lang.String getComparatorLabel​(java.lang.String name, java.lang.String lang)  
      static java.util.Set<java.lang.String> getComparatorNameSet​(java.lang.Class<? extends Storable> clazz)  
      static ComparatorManager getInstance()  
      static java.util.Set<java.lang.Class<? extends Storable>> getTypeSet()  
      void loadComparators()
      Reload comparator from properties.
      void overridePortletQueryForeachOrders()  
      void propertiesChange​(JProperties properties)
      Invoked after properties have been modified in JCMS and save on disk.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • propertiesChange

        public void propertiesChange​(JProperties properties)
        Description copied from interface: JPropertiesListener
        Invoked after properties have been modified in JCMS and save on disk.

        You cannot alter the value received in parameters.

        Note that properties parameter may be null, a limited set of site properties, or all site properties.

        To check that a property has been modified, reload the "current" property instead using channel.getProperties or channel.getProperty.

        Specified by:
        propertiesChange in interface JPropertiesListener
        Parameters:
        properties - the properties which have been submitted to change
      • loadComparators

        public void loadComparators()
        Reload comparator from properties.
        Since:
        jcms-7.1.0
      • overridePortletQueryForeachOrders

        public void overridePortletQueryForeachOrders()
      • getComparator

        public static <T extends Storable> java.util.Comparator<? super T> getComparator​(java.lang.Class<T> clazz,
                                                                                         java.lang.String comparatorName)
        Retrieve a new Comparator from its short identifier to sort instances of the specified class.
        Type Parameters:
        T - specialization of Storable
        Parameters:
        clazz - the Class of Storable to sort
        comparatorName - the comparator name as declared in properties
        Returns:
        a new instance of the requested Comparator, never return null (defaults to CdateComparator)
        Since:
        jcms-7.1.0
      • getComparator

        public static <T extends Storable> java.util.Comparator<? super T> getComparator​(java.lang.Class<T> typeClazz,
                                                                                         java.lang.String comparatorName,
                                                                                         boolean reverse)
        Retrieve a new Comparator from its short identifier to sort instances of the specified class.
        Type Parameters:
        T - specialization of Storable
        Parameters:
        typeClazz - the Class of Storable to sort
        comparatorName - the comparator name as declared in properties
        reverse - a boolean indicating whether the comparator should have its behavior reversed (true) or unchanged (false)
        Returns:
        a new instance of the requested Comparator, never return null (defaults to CdateComparator)
        Since:
        jcms-7.1.0
      • getComparator

        public static <T extends Storable> java.util.Comparator<? super T> getComparator​(java.lang.Class<T> typeClazz,
                                                                                         java.lang.String comparatorName,
                                                                                         boolean reverse,
                                                                                         java.util.Map<java.lang.String,​java.lang.Object> contextMap)
        Retrieve a new Comparator from its short identifier to sort instances of the specified class.
        Type Parameters:
        T - specialization of Storable
        Parameters:
        typeClazz - the Class of Storable to sort
        comparatorName - the comparator name as declared in properties
        reverse - a boolean indicating whether the comparator should have its behavior reversed (true) or unchanged (false)
        contextMap - a Map containing any contextual information which might be needed by the Comparator implementation (see BasicComparator for usage).
        Warning : if your comparator is used for a "long term" collection, i.e. a collection that will be kept in memory for a long period of time (for example in a internal index), as opposed to a collection that would be discarded after a short period of time (for example after the end of the current HTTP request): DO NOT add Object to the contextMap that would retain pointer and memory to short lived Object (such as an Http request), otherwise you will create a memory leak.
        Returns:
        a new instance of the requested Comparator, never return null (defaults to CdateComparator)
        Since:
        jcms-7.1.0
      • getComparatorClass

        public static <T extends java.util.Comparator<?>,​V extends Storable> java.lang.Class<T> getComparatorClass​(java.lang.Class<V> typeClazz,
                                                                                                                         java.lang.String comparatorName)
        Type Parameters:
        T - specialization of Comparator
        V - specialization of Storable
        Parameters:
        typeClazz - the Class of Storable to sort
        comparatorName - the comparator name as declared in properties
        Returns:
        a Comparator class, or null if none could be found with the specified parameters
      • getComparatorLabel

        public static java.lang.String getComparatorLabel​(java.lang.String name,
                                                          java.lang.String lang)
        Parameters:
        name - the name of the comparator
        lang - the lang of the label to get
        Returns:
        the label of the comparator given its name, in the given lang.
        Since:
        jcms-8.0.2, 9.0.0
      • getComparatorNameSet

        public static java.util.Set<java.lang.String> getComparatorNameSet​(java.lang.Class<? extends Storable> clazz)
        Parameters:
        clazz - the clazz to retrieve its Comparators' names
        Returns:
        Set of the given clazz's Comparators' names
        Since:
        jcms-8.0.2, 9.0.0
      • getTypeSet

        public static java.util.Set<java.lang.Class<? extends Storable>> getTypeSet()
        Returns:
        the Storable classes having comparator(s).
        Since:
        jcms-8.0.2, 9.0.0