Class ObjectComparatorManager

java.lang.Object
com.jidesoft.comparator.ObjectComparatorManager

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

    • ObjectComparatorManager

      public ObjectComparatorManager()
  • Method Details

    • registerComparator

      public static void registerComparator(Class<?> clazz, Comparator comparator)
    • registerComparator

      public static void registerComparator(Class<?> clazz, 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(Class<?> clazz)
      Unregisters comparator associated with the class and context.
      Parameters:
      clazz - the data type.
    • unregisterComparator

      public static void unregisterComparator(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(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 Comparator getComparator(Class<?> clazz)
      Gets the registered comparator associated with class and default context.
      Parameters:
      clazz - the data type.
      Returns:
      the registered comparator.
    • getComparator

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

      public static int compare(Object o1, 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(Object o1, 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(Object o1, Object o2, 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(Object o1, Object o2, 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

      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
    • getRegistrationListeners

      public static RegistrationListener[] getRegistrationListeners()
      Returns an array of all the registration listeners registered on this manager.
      Returns:
      all of this registration's RegistrationListeners or an empty array if no registration listeners are currently registered
      See Also:
    • getComparatorContexts

      public static ComparatorContext[] getComparatorContexts(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.
    • resetInit

      public static void resetInit()
      If initDefaultComparator() is called once, calling it again will have no effect because an internal flag is set. This method will reset the internal flag so that you can call initDefaultComparator() in case you unregister all comparators using unregisterAllComparators().
    • clear

      public static void clear()