Java Distance Calculate distanceToClass(Class theClass, Class theAncestor)

Here you can find the source of distanceToClass(Class theClass, Class theAncestor)

Description

Compute the hierarchical distance between the given class and an ancestor class.

License

LGPL

Parameter

Parameter Description
theClass the class to check.
theAncestor the ancestor class.

Return

0 if the two parameters are equals, to 1 if the class directly extends the ancestor and is incremented by 1 for each ancestor between the class and its ancestor.

Declaration

public static int distanceToClass(Class<?> theClass, Class<?> theAncestor) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**//from   w  w  w .j  a v  a 2  s.c  o m
     * Compute the hierarchical distance between the given class and an ancestor class. 
     * The hierarchical distance is equals to <code>0</code> if the two parameters are equals, 
     * to <code>1</code> if the class directly extends the ancestor
     * and is incremented by <code>1</code> for each ancestor between the class and its ancestor.<br>
     * If the given class is not a sub class of the ancestor, the result is <code>-1</code>.
     * @param theClass the class to check.
     * @param theAncestor the ancestor class.
     * @return <code>0</code> if the two parameters are equals, 
     * to <code>1</code> if the class directly extends the ancestor
     * and is incremented by <code>1</code> for each ancestor between the class and its ancestor.
     * @see #isSubClass(Class, Class)
     */
    public static int distanceToClass(Class<?> theClass, Class<?> theAncestor) {

        Class<?> superClass = null;

        if ((theClass != null) && (theAncestor != null) && (!theAncestor.isInterface())) {

            // If the ancestor and the current class are equals, the distance is 0
            if (theAncestor.equals(theClass)) {
                return 0;
            }

            // Search if the ancestor is directly declared on the class itself as super class. In this case,
            // the method return true. If the ancestor is not declared within the given class, 
            // its class hierarchy is recursively processed.
            superClass = theClass.getSuperclass();

            if (superClass != null) {

                if (superClass.equals(theAncestor)) {
                    return 1;
                } else {
                    // the ancestor was not the direct super class,
                    // the search is done following the class hierarchy
                    int tmp = distanceToClass(theClass.getSuperclass(), theAncestor);

                    if (tmp > -1) {
                        return 1 + tmp;
                    }
                }
            }
            superClass = null;
        }

        return -1;
    }
}

Related

  1. distanceSqured(int x, int y, int z, int x1, int y1, int z1)
  2. distanceSV(double[] sv1, double[] sv2)
  3. distanceTo(double latitudeGiven, double longitudeGiven, double latitudeTaken, double longitudeTaken)
  4. distanceTo(final int x, final int y, final int thatx, final int thaty)
  5. distanceToAncestor(Class entity, Class ancestor)
  6. distanceToInterface(Class theClass, Class theInterface)
  7. distanceToPoint(float x1, float y1, float x2, float y2)
  8. distanceToSegment(float ax, float ay, float bx, float by, float px, float py)
  9. distanceToSegmentSquared(float px, float py, float vx, float vy, float wx, float wy)