Java Distance Calculate distance(final Class child, final Class parent)

Here you can find the source of distance(final Class child, final Class parent)

Description

Gets derivation distance of an object<br> Possible values are:<br> - -1 = child !instanceof parent<br> - 0 = child is the same class as the parent one<br> - greater than 0 = child is the nth of the parent<br> <br> Copied from<br> https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java

License

Open Source License

Parameter

Parameter Description
child Child class
parent Parent class

Declaration

public static int distance(final Class<?> child, final Class<?> parent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   ww w  .j av a 2 s .c  o  m
     * Gets derivation distance of an object<br>
     * Possible values are:<br>
     * - -1 = child !instanceof parent<br>
     * - 0 = child is the same class as the parent one<br>
     * - greater than 0 = child is the nth of the parent<br>
     * <br>
     * Copied from<br>
     * https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java
     * @param child Child class
     * @param parent Parent class
     * @return
     */
    public static int distance(final Class<?> child, final Class<?> parent) {
        if (child == null || parent == null) {
            return -1;
        }

        if (child.equals(parent)) {
            return 0;
        }

        final Class<?> cParent = child.getSuperclass();
        int d = parent.equals(cParent) ? 1 : 0;

        if (cParent != null) {
            Class<?>[] interfaces = cParent.getInterfaces();
            for (Class<?> i : interfaces) {
                if (parent.equals(i))
                    d = 1;

            }
        }

        if (d == 1) {
            return d;
        }
        d += distance(cParent, parent);
        return d > 0 ? d + 1 : -1;
    }
}

Related

  1. distance(double[] a, double[] b)
  2. distance(double[] a, double[] b)
  3. distance(double[] p1, double[] p2)
  4. distance(double[] p1, double[] p2)
  5. distance(double[] x, int p, int q, int dim)
  6. distance(final double x, final double y)
  7. distance(final double x1, final double y1, final double x2, final double y2)
  8. distance(final double x1, final double y1, final double x2, final double y2)
  9. distance(final double[] a, final double[] b)