Java Distance Calculate distanceToAncestor(Class entity, Class ancestor)

Here you can find the source of distanceToAncestor(Class entity, Class ancestor)

Description

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

License

LGPL

Parameter

Parameter Description
entity the entity to check.
ancestor the ancestor.

Return

The hierarchical distance is equals to 0 if the two parameters are equals, to 1 if the class directly extends or implements the ancestor and is incremented by 1 for each ancestor between the class and its ancestor.
If the given class is not a sub class nor an implementation of the ancestor, the result is -1.

Declaration

public static int distanceToAncestor(Class<?> entity, Class<?> ancestor) 

Method Source Code

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

public class Main {
    /**/*from w  w w .j  ava  2s  .  co  m*/
     * Compute the hierarchical distance between the given class and an ancestor. 
     * The hierarchical distance is equals to <code>0</code> if the two parameters are equals, 
     * to <code>1</code> if the class directly extends or implements 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 nor an implementation of the ancestor, the result is <code>-1</code>.
     * @param entity the entity to check.
     * @param ancestor the ancestor.
     * @return The hierarchical distance is equals to <code>0</code> if the two parameters are equals, 
     * to <code>1</code> if the class directly extends or implements 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 nor an implementation of the ancestor, the result is <code>-1</code>.
     * @see #distanceToClass(Class, Class)
     * @see #distanceToInterface(Class, Class)
     */
    public static int distanceToAncestor(Class<?> entity, Class<?> ancestor) {
        if ((entity != null) && (ancestor != null)) {
            if (ancestor.isInterface()) {
                return distanceToInterface(entity, ancestor);
            } else {
                return distanceToClass(entity, ancestor);
            }
        }
        return -1;
    }

    /**
     * Compute the hierarchical distance between the given class and the given interface. 
     * The hierarchical distance is equals to <code>0</code> if the two parameters are equals, 
     * to <code>1</code> if the class directly implements the interface
     * and is incremented by <code>1</code> for each ancestor between the class and the interface.<br>
     * If the given class do not implements the given interface, the result is <code>-1</code>.
     * @param theClass the class to check.
     * @param theInterface the interface.
     * @return <code>0</code> if the two parameters are equals, 
     * to <code>1</code> if the class directly implements the interface
     * and is incremented by <code>1</code> for each ancestor between the class and the interface.
     * @see #isImplements(Class, Class)
     */
    public static int distanceToInterface(Class<?> theClass, Class<?> theInterface) {

        int tmpDistance = 0;

        Class<?>[] interfaces = null;

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

            // If the given class and the interface are the same.
            if (theInterface.equals(theClass)) {
                return 0;
            } else {

                // Search if the interface is directly declared on the class itself. In this case,
                // the method return true. If the interface is not declared within the given class, 
                // its interface hierarchy is recursively processed.
                interfaces = theClass.getInterfaces();
                if ((interfaces != null) && (interfaces.length > 0)) {
                    for (int j = 0; j < interfaces.length; j++) {
                        // The given interface is declared directly by the class
                        if (theInterface.equals(interfaces[j])) {
                            return 1;
                        }
                    }

                    // The interface was not found within the class hierarchy, we now look at the implementation hierarchy.
                    for (int j = 0; j < interfaces.length; j++) {

                        tmpDistance = distanceToInterface(interfaces[j], theInterface);
                        if (tmpDistance > -1) {
                            return 1 + tmpDistance;
                        }
                    }

                    interfaces = null;
                }

                // the interface was not found within the class declared interface,
                // the search is done following the class hierarchy
                tmpDistance = distanceToInterface(theClass.getSuperclass(), theInterface);

                if (tmpDistance > -1) {
                    return 1 + tmpDistance;
                }
            }
        }

        return -1;
    }

    /**
     * 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. distanceSquaredPointToPoint(float x1, float y1, float x2, float y2)
  2. distanceSqured(int x, int y, int z, int x1, int y1, int z1)
  3. distanceSV(double[] sv1, double[] sv2)
  4. distanceTo(double latitudeGiven, double longitudeGiven, double latitudeTaken, double longitudeTaken)
  5. distanceTo(final int x, final int y, final int thatx, final int thaty)
  6. distanceToClass(Class theClass, Class theAncestor)
  7. distanceToInterface(Class theClass, Class theInterface)
  8. distanceToPoint(float x1, float y1, float x2, float y2)
  9. distanceToSegment(float ax, float ay, float bx, float by, float px, float py)