Iterables { public static <T> List<T> toList(Iterable<T> iterable) { if (iterable instanceof Collection) { return new ArrayList<T>((Collection<T>)iterable); } else { List<T> list = new LinkedList<T>(); for (T element : iterable) { list.add(element); } return list; } } Iterables(); static List<T> toList(Iterable<T> iterable); static Set<T> toSet(Iterable<T> iterable); static String toString(Iterable<?> iterable); static int count(Iterable<?> iterable);  }
@Test public void testToList() { Assert.assertTrue("should be an instance of ArrayList", Iterables.toList(DUMMY_COLLECTION) instanceof ArrayList); Assert.assertTrue("should have 4 elements", Iterables.count(Iterables.toList(DUMMY_COLLECTION)) == 4); Assert.assertEquals("[1, 2, 3, 4]", Iterables.toString(Iterables.toList(DUMMY_COLLECTION))); Assert.assertTrue("should be an instance of LinkedList", Iterables.toList(DUMMY_ITERABLE) instanceof LinkedList); Assert.assertTrue("should have 4 elements", Iterables.count(Iterables.toList(DUMMY_ITERABLE)) == 4); Assert.assertEquals("[1, 2, 3, 4]", Iterables.toString(Iterables.toList(DUMMY_ITERABLE))); }