Lists { public static <E> List<E> ensureImmutable(List<E> list) { if (list.isEmpty()) return Collections.emptyList(); if (list.size() == 1) return Collections.singletonList(list.get(0)); if (isImmutable(list)) return list; return Collections.unmodifiableList(new ArrayList<>(list)); } 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(expected = UnsupportedOperationException.class) public void ensureImmutable_returnsImmutableEmptyList() { Lists.ensureImmutable(new ArrayList<>()).add("foo"); }
@Test public void ensureImmutable_convertsToSingletonList() { List<Object> list = new ArrayList<>(); list.add("foo"); assertThat(Lists.ensureImmutable(list).getClass().getSimpleName()) .isEqualTo("SingletonList"); }
@Test public void ensureImmutable_convertsToUnmodifiableList() { List<Long> list = new ArrayList<>(); list.add(1L); list.add(2L); assertThat(Lists.ensureImmutable(list).getClass().getSimpleName()) .startsWith("Unmodifiable"); }
@Test public void ensureImmutable_returnsEmptyList() { List<Object> list = Collections.emptyList(); assertThat(Lists.ensureImmutable(list)) .isSameAs(list); }
@Test public void ensureImmutable_returnsEmptyListForMutableInput() { List<Object> list = new ArrayList<>(); assertThat(Lists.ensureImmutable(list)).isSameAs(Collections.emptyList()); }
@Test public void ensureImmutable_singletonListStaysSingleton() { List<Object> list = Collections.singletonList("foo"); assertThat(Lists.ensureImmutable(list).getClass().getSimpleName()) .isEqualTo("SingletonList"); }
@Test public void ensureImmutable_doesntCopyUnmodifiableList() { List<Object> list = Collections.unmodifiableList(Arrays.asList("foo", "bar")); assertThat(Lists.ensureImmutable(list)) .isSameAs(list); }
@Test public void ensureImmutable_doesntCopyImmutableList() { List<Object> list = ImmutableList.of("foo", "bar"); assertThat(Lists.ensureImmutable(list)) .isSameAs(list); }