CollectionUtils { public static <T extends Set<?>> Set<?> createNewSet(Class<T> destType) { Set<Object> result; if (SortedSet.class.isAssignableFrom(destType)) { result = new TreeSet<>(); } else { result = new HashSet<>(); } return result; } private  CollectionUtils(); static boolean isArray(Class<?> aClass); static boolean isCollection(Class<?> aClass); static boolean isList(Class<?> aClass); static boolean isSet(Class<?> aClass); static boolean isPrimitiveArray(Class<?> aClass); static int getLengthOfCollection(Object value); static Object getValueFromCollection(Object collection, int index); static Set<?> createNewSet(Class<T> destType); static Set<?> createNewSet(Class<T> destSetType, Collection<?> srcValue); static Object convertListToArray(List<T> list, Class<T> destEntryType); static List<Object> convertPrimitiveArrayToList(Object primitiveArray); static Set<E> intersection(final Set<E> set1, final Set<?> set2);  }
@Test public void testCreateNewSet_Default() { Set<?> result = CollectionUtils.createNewSet(Set.class); assertNotNull("new set should not be null", result); }
@Test public void testCreateNewSet_SortedSetDefault() { Set<?> result = CollectionUtils.createNewSet(SortedSet.class); assertNotNull("new set should not be null", result); assertTrue("new set should be instance of SortedSet", result instanceof SortedSet); }
@Test public void testCreateNewSet_FromExistingSet() { Set<String> src = new HashSet<>(); src.add("test1"); src.add("test2"); Set<?> result = CollectionUtils.createNewSet(Set.class, src); assertNotNull("new set should not be null", result); assertEquals("new set should equal src set", src, result); }
@Test public void testCreateNewSet_ExistingValue() { Collection<String> src = new HashSet<>(); src.add("a"); src.add("b"); Set<?> result = CollectionUtils.createNewSet(TreeSet.class, src); assertEquals("wrong result value", src, result); }
@Test public void testCreateNewSet() { Set<?> result = CollectionUtils.createNewSet(HashSet.class); assertNotNull("should be not null", result); assertEquals("shoulb be size zero", 0, result.size()); }