/** * <p>Returns the number of inheritance hops between two classes.</p> * * @param child the child class, may be {@code null} * @param parent the parent class, may be {@code null} * @return the number of generations between the child and parent; 0 if the same class; * -1 if the classes are not related as child and parent (includes where either class is null) * @since 3.2 */ 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 = BooleanUtils.toInteger(parent.equals(cParent)); if (d == 1) { return d; } d += distance(cParent, parent); return d > 0 ? d + 1 : -1; } }
@Test public void testDistanceGreaterThanZero() { assertEquals(1, InheritanceUtils.distance(AnotherChild.class, AnotherParent.class)); assertEquals(1, InheritanceUtils.distance(Grandchild.class, AnotherChild.class)); assertEquals(2, InheritanceUtils.distance(Grandchild.class, AnotherParent.class)); assertEquals(3, InheritanceUtils.distance(Grandchild.class, Object.class)); }
@Test public void testDistanceNullParent() { assertEquals(-1, InheritanceUtils.distance(Object.class, null)); }
@Test public void testDistanceReverseParentChild() { assertEquals(-1, InheritanceUtils.distance(Object.class, Grandchild.class)); } }
@Test public void testDistanceEqual() { assertEquals(0, InheritanceUtils.distance(AnotherChild.class, AnotherChild.class)); }
@Test public void testDistanceEqualObject() { assertEquals(0, InheritanceUtils.distance(Object.class, Object.class)); }
@Test public void testDistanceNullChild() { assertEquals(-1, InheritanceUtils.distance(null, Object.class)); }
@Test public void testDistanceNullParentNullChild() { assertEquals(-1, InheritanceUtils.distance(null, null)); }
@Test public void testDistanceDisjoint() { assertEquals(-1, InheritanceUtils.distance(Boolean.class, String.class)); }
/** * <p>Returns the number of inheritance hops between two classes.</p> * * @param child the child class, may be {@code null} * @param parent the parent class, may be {@code null} * @return the number of generations between the child and parent; 0 if the same class; * -1 if the classes are not related as child and parent (includes where either class is null) * @since 3.2 */ 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 = BooleanUtils.toInteger(parent.equals(cParent)); if (d == 1) { return d; } d += distance(cParent, parent); return d > 0 ? d + 1 : -1; } }
/** * <p>Returns the number of inheritance hops between two classes.</p> * * @param child the child class, may be {@code null} * @param parent the parent class, may be {@code null} * @return the number of generations between the child and parent; 0 if the same class; * -1 if the classes are not related as child and parent (includes where either class is null) * @since 3.2 */ 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 = BooleanUtils.toInteger(parent.equals(cParent)); if (d == 1) { return d; } d += distance(cParent, parent); return d > 0 ? d + 1 : -1; } }
/** * <p>Returns the number of inheritance hops between two classes.</p> * * @param child the child class, may be {@code null} * @param parent the parent class, may be {@code null} * @return the number of generations between the child and parent; 0 if the same class; * -1 if the classes are not related as child and parent (includes where either class is null) * @since 3.2 */ 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 = BooleanUtils.toInteger(parent.equals(cParent)); if (d == 1) { return d; } d += distance(cParent, parent); return d > 0 ? d + 1 : -1; } }