Package io.microsphere.util
Class HierarchicalClassComparator
- java.lang.Object
-
- io.microsphere.util.HierarchicalClassComparator
-
- All Implemented Interfaces:
java.util.Comparator<java.lang.Class<?>>
public class HierarchicalClassComparator extends java.lang.Object implements java.util.Comparator<java.lang.Class<?>>
Comparator
for hierarchical class.- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Comparator
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.Comparator<java.lang.Class<?>>
ASCENT
The singleton for ascending order, from the top of the hierarchy to the bottomstatic java.util.Comparator<java.lang.Class<?>>
DESCENT
The singleton for descending order, from the bottom of the hierarchy to the top
-
Constructor Summary
Constructors Modifier Constructor Description protected
HierarchicalClassComparator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compare(java.lang.Class<?> o1, java.lang.Class<?> o2)
Compares two classes based on their hierarchical relationship.
-
-
-
Field Detail
-
ASCENT
public static final java.util.Comparator<java.lang.Class<?>> ASCENT
The singleton for ascending order, from the top of the hierarchy to the bottom
-
DESCENT
public static final java.util.Comparator<java.lang.Class<?>> DESCENT
The singleton for descending order, from the bottom of the hierarchy to the top
-
-
Method Detail
-
compare
public int compare(java.lang.Class<?> o1, java.lang.Class<?> o2)
Compares two classes based on their hierarchical relationship.The comparison follows these rules:
- If both classes are the same, returns 0.
- If
o1
is a superclass or interface ofo2
, returns -1 (in ascending order). - If
o2
is a superclass or interface ofo1
, returns 1 (in ascending order).
Examples (using ASCENT comparator):
compare(Object.class, String.class) => -1 // Object is superclass of String compare(String.class, Object.class) => 1 // String is subclass of Object compare(String.class, Integer.class) => 1 // No direct relation, but returns positive compare(String.class, String.class) => 0 // Same class
- Specified by:
compare
in interfacejava.util.Comparator<java.lang.Class<?>>
- Parameters:
o1
- the first class to be comparedo2
- the second class to be compared- Returns:
- a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second according to hierarchical ordering.
-
-