CollectionInstanceUtils { public static Collection<?> getCollection(Class<?> collectionType) { if (HashSet.class.equals(collectionType)) { return new HashSet<>(); } else if (TreeSet.class.equals(collectionType)) { return new TreeSet<>(); } else if (CopyOnWriteArraySet.class.equals(collectionType)) { return new CopyOnWriteArraySet<>(); } else if (LinkedHashSet.class.equals(collectionType)) { return new LinkedHashSet<>(); } else if (ArrayList.class.equals(collectionType)) { return new ArrayList<>(); } else if (LinkedList.class.equals(collectionType)) { return new LinkedList<>(); } else if (Vector.class.equals(collectionType)) { return new Vector<>(); } else if (Stack.class.equals(collectionType)) { return new Stack<>(); } else if (PriorityQueue.class.equals(collectionType)) { return new PriorityQueue<>(); } else if (PriorityBlockingQueue.class.equals(collectionType)) { return new PriorityBlockingQueue<>(); } else if (ArrayDeque.class.equals(collectionType)) { return new ArrayDeque<>(); } else if (ConcurrentLinkedQueue.class.equals(collectionType)) { return new ConcurrentLinkedQueue<>(); } else if (LinkedBlockingQueue.class.equals(collectionType)) { return new LinkedBlockingQueue<>(); } else if (LinkedBlockingDeque.class.equals(collectionType)) { return new LinkedBlockingDeque<>(); } else if (List.class.equals(collectionType)) { return new LinkedList<>(); } else if (Set.class.equals(collectionType)) { return new HashSet<>(); } else if (Queue.class.equals(collectionType)) { return new PriorityQueue<>(); } else if (Deque.class.equals(collectionType)) { return new ArrayDeque<>(); } else if (Collection.class.equals(collectionType)) { return new LinkedList<>(); } throw new IllegalArgumentException("Unsupported collection type: " + collectionType); } private  CollectionInstanceUtils(); static Collection<?> getCollection(Class<?> collectionType);  }
@Test public void testSupportedConcreteTypes() { final List<? extends Class<?>> collectionTypes = Arrays.asList(HashSet.class, TreeSet.class, CopyOnWriteArraySet.class, LinkedHashSet.class, ArrayList.class, LinkedList.class, Vector.class, Stack.class, PriorityQueue.class, PriorityBlockingQueue.class, ArrayDeque.class, ConcurrentLinkedQueue.class, LinkedBlockingQueue.class, LinkedBlockingDeque.class); for (Class<?> collectionType : collectionTypes) { final Collection<?> collection = CollectionInstanceUtils.getCollection(collectionType); assertThat(collection, is(notNullValue())); assertThat(collection, is(instanceOf(collectionType))); } }
@Test public void testSupportedAbstractTypes() { assertThat(CollectionInstanceUtils.getCollection(Set.class), is(instanceOf(Set.class))); assertThat(CollectionInstanceUtils.getCollection(List.class), is(instanceOf(List.class))); assertThat(CollectionInstanceUtils.getCollection(Queue.class), is(instanceOf(Queue.class))); assertThat(CollectionInstanceUtils.getCollection(Deque.class), is(instanceOf(Deque.class))); assertThat(CollectionInstanceUtils.getCollection(Collection.class), is(instanceOf(Collection.class))); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testUnknownType() { CollectionInstanceUtils.getCollection(Class.class); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullValue() { CollectionInstanceUtils.getCollection(null); }