I am designing a simple Data Access Object for my Java application. I have a few classes (records) that represents a single row in tables like User and Fr
I believe what you are trying to do is possible with a bit of generics magic. I had to solve the same problem just now and this is what I did:
public class ListArrayUtils{
@SuppressWarnings("unchecked") // It is checked.
public static List filterByType(List aList, Class aClass){
List ans = new ArrayList<>();
for(E e: aList){
if(aClass.isAssignableFrom(e.getClass())){
ans.add((T)e);
}
}
return ans;
}
}
And unit tests:
public class ListArrayUtilsTest{
interface IfA{/*nothing*/}
interface IfB{/*nothing*/}
class A implements IfA{/*nothing*/}
class B implements IfB{/*nothing*/}
class C extends A implements IfB{/*nothing*/}
@Test
public void testFilterByType(){
List