Class ObjectComparatorManager


  • public class ObjectComparatorManager
    extends java.lang.Object
    A global object that can register comparator with a type and a ComparatorContext.
    • Constructor Detail

      • ObjectComparatorManager

        public ObjectComparatorManager()
    • Method Detail

      • registerComparator

        public static void registerComparator​(java.lang.Class<?> clazz,
                                              java.util.Comparator comparator)
      • registerComparator

        public static void registerComparator​(java.lang.Class<?> clazz,
                                              java.util.Comparator comparator,
                                              ComparatorContext context)
        Registers a comparator with the type specified as class and a comparator context specified as context.
        Parameters:
        clazz - type.
        comparator - the comparator to be registered.
        context - the comparator context.
      • unregisterComparator

        public static void unregisterComparator​(java.lang.Class<?> clazz)
        Unregisters comparator associated with the class and context.
        Parameters:
        clazz - the data type.
      • unregisterComparator

        public static void unregisterComparator​(java.lang.Class<?> clazz,
                                                ComparatorContext context)
        Unregisters comparator associated with the class and context.
        Parameters:
        clazz - the data type.
        context - the comparator context.
      • unregisterAllComparators

        public static void unregisterAllComparators​(java.lang.Class<?> clazz)
        Unregisters all comparators associated with the class.
        Parameters:
        clazz - the data type.
      • unregisterAllComparators

        public static void unregisterAllComparators()
        Unregisters all the comparators which registered before.
      • getComparator

        public static java.util.Comparator getComparator​(java.lang.Class<?> clazz)
        Gets the registered comparator associated with class and default context.
        Parameters:
        clazz - the data type.
        Returns:
        the registered comparator.
      • getComparator

        public static java.util.Comparator getComparator​(java.lang.Class<?> clazz,
                                                         ComparatorContext context)
        Gets the comparator.
        Parameters:
        clazz - the data type.
        context - the comparator context.
        Returns:
        the comparator.
      • compare

        public static int compare​(java.lang.Object o1,
                                  java.lang.Object o2)
        Compares the two objects. It will look up in ObjectComparatorManager to find the comparator and compare.
        Parameters:
        o1 - the first object to be compared.
        o2 - the second object to be compared.
        Returns:
        the compare result as defined in Comparator.compare(Object, Object)
      • compare

        public static int compare​(java.lang.Object o1,
                                  java.lang.Object o2,
                                  ComparatorContext context)
        Compares the two objects. It will look up in ObjectComparatorManager to find the comparator and compare.
        Parameters:
        o1 - the first object to be compared.
        o2 - the second object to be compared.
        context - the comparator context
        Returns:
        the compare result as defined in Comparator.compare(Object, Object)
      • compare

        public static int compare​(java.lang.Object o1,
                                  java.lang.Object o2,
                                  java.lang.Class<?> clazz)
        Compares the two objects. It will look up in ObjectComparatorManager to find the comparator and compare. This method needs a third parameter which is the data type. This is useful when you have two objects that have different data types but both extend the same super class. In this case, you may want the super class as the key to look up in ObjectComparatorManager.
        Parameters:
        o1 - the first object to be compared.
        o2 - the second object to be compared.
        clazz - the data type of the two objects. If your two objects have the same type, you may just use compare(Object, Object) methods.
        Returns:
        the compare result as defined in Comparator.compare(Object, Object)
      • compare

        public static int compare​(java.lang.Object o1,
                                  java.lang.Object o2,
                                  java.lang.Class<?> clazz,
                                  ComparatorContext context)
        Compares the two objects. It will look up in ObjectComparatorManager to find the comparator and compare. If it is not found, we will convert the object to string and compare the two strings.
        Parameters:
        o1 - the first object to be compared.
        o2 - the second object to be compared.
        clazz - the data type of the two objects. If your two objects have the same type, you may just use compare(Object, Object) methods.
        context - the comparator context
        Returns:
        the compare result as defined in Comparator.compare(Object, Object)
      • isAutoInit

        public static boolean isAutoInit()
        Checks the value of autoInit.
        Returns:
        true or false.
        See Also:
        setAutoInit(boolean)
      • setAutoInit

        public static void setAutoInit​(boolean autoInit)
        Sets autoInit to true or false. If autoInit is true, whenever someone tries to call methods like as toString or fromString, initDefaultComparator() will be called if it has never be called. By default, autoInit is true.

        This might affect the behavior if users provide their own comparators and want to overwrite default comparators. In this case, instead of depending on autoInit to initialize default comparators, you should call initDefaultComparator() first, then call registerComparator to add your own comparators.

        Parameters:
        autoInit - false if you want to disable autoInit which means you either don't want those default comparators registered or you will call initDefaultComparator() yourself.
      • addRegistrationListener

        public static void addRegistrationListener​(RegistrationListener l)
        Adds a listener to the list that's notified each time a change to the manager occurs.
        Parameters:
        l - the RegistrationListener
      • removeRegistrationListener

        public static void removeRegistrationListener​(RegistrationListener l)
        Removes a listener from the list that's notified each time a change to the manager occurs.
        Parameters:
        l - the RegistrationListener
      • getComparatorContexts

        public static ComparatorContext[] getComparatorContexts​(java.lang.Class<?> clazz)
        Gets the available ComparatorContexts registered with the class.
        Parameters:
        clazz - the class.
        Returns:
        the available ComparatorContext.
      • initDefaultComparator

        public static void initDefaultComparator()
        Initialize default comparator. Please make sure you call this method before you use any comparator related classes such as SortableTableModel.
      • clear

        public static void clear()