Class 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 bottom
      static java.util.Comparator<java.lang.Class<?>> DESCENT
      The singleton for descending order, from the bottom of the hierarchy to the top
    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • 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
    • Constructor Detail

      • HierarchicalClassComparator

        protected HierarchicalClassComparator()
    • 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 of o2, returns -1 (in ascending order).
        • If o2 is a superclass or interface of o1, 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 interface java.util.Comparator<java.lang.Class<?>>
        Parameters:
        o1 - the first class to be compared
        o2 - 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.