Package com.jidesoft.comparator
Class ObjectComparatorManager
- java.lang.Object
-
- com.jidesoft.comparator.ObjectComparatorManager
-
public class ObjectComparatorManager extends java.lang.Object
A global object that can register comparator with a type and a ComparatorContext.
-
-
Constructor Summary
Constructors Constructor Description ObjectComparatorManager()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addRegistrationListener(RegistrationListener l)
Adds a listener to the list that's notified each time a change to the manager occurs.static void
clear()
static int
compare(java.lang.Object o1, java.lang.Object o2)
Compares the two objects.static int
compare(java.lang.Object o1, java.lang.Object o2, ComparatorContext context)
Compares the two objects.static int
compare(java.lang.Object o1, java.lang.Object o2, java.lang.Class<?> clazz)
Compares the two objects.static int
compare(java.lang.Object o1, java.lang.Object o2, java.lang.Class<?> clazz, ComparatorContext context)
Compares the two objects.static java.util.Comparator
getComparator(java.lang.Class<?> clazz)
Gets the registered comparator associated with class and default context.static java.util.Comparator
getComparator(java.lang.Class<?> clazz, ComparatorContext context)
Gets the comparator.static ComparatorContext[]
getComparatorContexts(java.lang.Class<?> clazz)
Gets the available ComparatorContexts registered with the class.static RegistrationListener[]
getRegistrationListeners()
Returns an array of all the registration listeners registered on this manager.static void
initDefaultComparator()
Initialize default comparator.static boolean
isAutoInit()
Checks the value of autoInit.static void
registerComparator(java.lang.Class<?> clazz, java.util.Comparator comparator)
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.static void
removeRegistrationListener(RegistrationListener l)
Removes a listener from the list that's notified each time a change to the manager occurs.static void
resetInit()
IfinitDefaultComparator()
is called once, calling it again will have no effect because an internal flag is set.static void
setAutoInit(boolean autoInit)
Sets autoInit to true or false.static void
unregisterAllComparators()
Unregisters all the comparators which registered before.static void
unregisterAllComparators(java.lang.Class<?> clazz)
Unregisters all comparators associated with the class.static void
unregisterComparator(java.lang.Class<?> clazz)
Unregisters comparator associated with the class and context.static void
unregisterComparator(java.lang.Class<?> clazz, ComparatorContext context)
Unregisters comparator associated with the class and context.
-
-
-
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 inObjectComparatorManager
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 inObjectComparatorManager
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 inObjectComparatorManager
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 inObjectComparatorManager
.- 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 usecompare(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 inObjectComparatorManager
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 usecompare(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 callinitDefaultComparator()
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 callinitDefaultComparator()
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
RegistrationListener
s or an empty array if no registration listeners are currently registered - See Also:
addRegistrationListener(com.jidesoft.utils.RegistrationListener)
,removeRegistrationListener(com.jidesoft.utils.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.
-
resetInit
public static void resetInit()
IfinitDefaultComparator()
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 callinitDefaultComparator()
in case you unregister all comparators usingunregisterAllComparators()
.
-
clear
public static void clear()
-
-