ClassUtils { public static <T> List<T> filterByClass(Class<T> clazz, Enumeration enumeration) { List<T> list = new ArrayList<>(); if (enumeration != null) { while (enumeration.hasMoreElements()) { Object obj = enumeration.nextElement(); if (clazz.isInstance(obj)) { list.add(clazz.cast(obj)); } } } return list; } private  ClassUtils(); static List<T> filterByClass(Class<T> clazz, Enumeration enumeration);  }
@Test public void shouldCastObjects() throws Exception { List<String> actual = ClassUtils.filterByClass(String.class, new Vector(strings).elements()); assertEquals(strings.size(), actual.size()); assertEquals(strings, actual); List list = new ArrayList(strings); list.add(5); list.add(6); list.add(7); actual = ClassUtils.filterByClass(String.class, new Vector(list).elements()); assertEquals(strings.size(), actual.size()); assertEquals(strings, actual); }
@Test public void shouldReturnEmptyList() { assertTrue(ClassUtils.filterByClass(Integer.class, null).isEmpty()); assertTrue(ClassUtils.filterByClass(Integer.class, new Vector().elements()).isEmpty()); assertTrue(ClassUtils.filterByClass(Integer.class, new Vector(strings).elements()).isEmpty()); }