Immutables { @Nonnull public static <T> Collection<T> copyOf(@Nonnull final Collection<? extends T> elts) { assert elts != null; if (elts instanceof Set) { return ImmutableSet.copyOf(elts); } else { return ImmutableList.copyOf(elts); } } private  Immutables(); @Nonnull static Collection<T> copyOf(@Nonnull final Collection<? extends T> elts); @SafeVarargs @Nonnull static Collection<T> copyOf(@Nonnull final Collection<? extends T> elts1, @Nonnull final T... elts2); @Nonnull static List<T> emptyList(); @Nonnull static List<T> listOfSingle(@Nonnull final T elt); @SafeVarargs @Nonnull static List<T> listOf(@Nonnull final T... elts); @Nonnull static List<T> listOf(@Nonnull final Collection<? extends T> values); @SafeVarargs @Nonnull static List<T> listOf(@Nonnull final Collection<? extends T> elts1, @Nonnull final T... elts2); @Nonnull static Map<K, V> emptyMap(); @Nonnull static Map<K, V> mapOf(@Nonnull final Map<? extends K, ? extends V> values); @Nonnull static Set<T> emptySet(); @SafeVarargs @Nonnull static Set<T> setOf(@Nonnull final T... elts); @Nonnull static Set<T> setOf(@Nonnull final Collection<? extends T> elts); @SafeVarargs @Nonnull static Set<T> setOf(@Nonnull final Collection<? extends T> elts1, @Nonnull final T... elts2); static List<T> replace(final List<T> elts, final T oldElt, final T newElt); static List<T> replaceOrAdd(final List<T> elts, final T oldElt, final T newElt);  }
@Test public void testCopyOf() { LOG.info("testCopyOf"); final Collection<Integer> x = Immutables.listOf(1); Assert.assertTrue("Array " + x + " should contain 1", x.contains(1)); final Collection<Integer> x2 = Immutables.copyOf(x); Assert.assertTrue("Array " + x2 + " should equal ", x2.equals(x)); final Collection<Integer> a = Immutables.copyOf(x, 2); Assert.assertTrue("Array " + a + " should contain 1", a.contains(1)); Assert.assertTrue("Array " + a + " should contain 2", a.contains(2)); @SuppressWarnings("unchecked") final Set<Integer> b = new LinkedHashSet<>(); final Collection<Integer> b1 = Immutables.copyOf(b); Assert.assertTrue("Array " + b1 + " should be empty", b1.isEmpty()); final Collection<Integer> c = Immutables.copyOf(a, 3, 4); final Collection<Integer> d = Immutables.copyOf(b, 1, 2, 3, 4); final Collection<Integer> e = Immutables.copyOf(b, 2, 1, 3, 4); Assert.assertTrue("Array " + c + " should equal " + d, d.equals(c)); Assert.assertFalse("Array " + c + " should not equal " + e, e.equals(c)); final Collection<Integer> u0 = Immutables.copyOf(Immutables.<Integer>emptyList()); @SuppressWarnings("unchecked") final Collection<Integer> v0 = Immutables.copyOf(new ArrayList<>()); Assert.assertTrue("Array " + u0 + " should equal " + v0, u0.equals(v0)); }