Lists { public static <E> List<E> ensureMutable(List<E> list) { if (list instanceof ArrayList) return list; int size = list.size(); ArrayList<E> mutable = new ArrayList<>(size); for (int i = 0; i < size; i++) { mutable.add(list.get(i)); } return mutable; } Lists(); static List<E> ensureMutable(List<E> list); static List<E> ensureImmutable(List<E> list); static List<E> concat(List<E> left, List<E> right);  }
@Test public void ensureMutable_leavesArrayList() { List<Object> list = new ArrayList<>(); assertThat(Lists.ensureMutable(list)) .isSameAs(list); }
@Test public void ensureMutable_copiesImmutable() { List<Object> list = Collections.unmodifiableList(Arrays.asList("foo", "bar")); assertThat(Lists.ensureMutable(list)) .isInstanceOf(ArrayList.class) .containsExactlyElementsOf(list); }