getClassHierarchy function v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 13:21:26 +0200
branchv_0
changeset 21339d154429f7a
parent 212 d154d6012cbe
child 214 1fb3c7953d8a
getClassHierarchy function
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sat Aug 15 11:52:38 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sat Aug 15 13:21:26 2015 +0200
     1.3 @@ -24,9 +24,13 @@
     1.4  import java.io.InputStream;
     1.5  import java.io.InputStreamReader;
     1.6  import java.io.PrintWriter;
     1.7 +import java.util.ArrayDeque;
     1.8 +import java.util.ArrayList;
     1.9  import java.util.Arrays;
    1.10  import java.util.Collection;
    1.11  import java.util.Collections;
    1.12 +import java.util.Deque;
    1.13 +import java.util.List;
    1.14  import java.util.Map;
    1.15  
    1.16  /**
    1.17 @@ -164,4 +168,21 @@
    1.18  			return result.toString();
    1.19  		}
    1.20  	}
    1.21 +
    1.22 +	/**
    1.23 +	 * @param <P> type of the last parent
    1.24 +	 * @param <T> type of the examined class
    1.25 +	 * @param type examined class
    1.26 +	 * @param lastParent the last parent type to stop at
    1.27 +	 * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
    1.28 +	 */
    1.29 +	public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
    1.30 +		List<Class<? extends P>> hierarchy = new ArrayList<>();
    1.31 +
    1.32 +		for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
    1.33 +			hierarchy.add(current);
    1.34 +		}
    1.35 +
    1.36 +		return hierarchy;
    1.37 +	}
    1.38  }
     2.1 --- a/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java	Sat Aug 15 11:52:38 2015 +0200
     2.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java	Sat Aug 15 13:21:26 2015 +0200
     2.3 @@ -19,6 +19,7 @@
     2.4  
     2.5  import java.util.ArrayList;
     2.6  import java.util.Collection;
     2.7 +import java.util.List;
     2.8  import static org.testng.Assert.*;
     2.9  import org.testng.annotations.*;
    2.10  
    2.11 @@ -74,4 +75,22 @@
    2.12  		assertEquals(Functions.repeat('f', 0), "");
    2.13  		assertEquals(Functions.repeat('f', 3), "fff");
    2.14  	}
    2.15 +
    2.16 +	@Test
    2.17 +	public void testGetClassHierarchy() {
    2.18 +		List<Class<? extends HierarchyMockClass2>> hierarchy = Functions.getClassHierarchy(HierarchyMockClass0.class, HierarchyMockClass2.class);
    2.19 +		assertEquals(hierarchy.size(), 3, "invalid number of classes in the hierarchy");
    2.20 +		assertEquals(hierarchy.get(0), HierarchyMockClass0.class);
    2.21 +		assertEquals(hierarchy.get(1), HierarchyMockClass1.class);
    2.22 +		assertEquals(hierarchy.get(2), HierarchyMockClass2.class);
    2.23 +	}
    2.24 +
    2.25 +	private static class HierarchyMockClass0 extends HierarchyMockClass1 {
    2.26 +	}
    2.27 +
    2.28 +	private static class HierarchyMockClass1 extends HierarchyMockClass2 {
    2.29 +	}
    2.30 +
    2.31 +	private static class HierarchyMockClass2 {
    2.32 +	}
    2.33  }