/** creates an {@link ImmutableList} which is a copy of this list. note that the list should not contain nulls. */ public List<V> asImmutableCopy() { try { return ImmutableList.copyOf(this); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.warn("Error converting list to Immutable, using unmodifiable instead: "+e, e); return asUnmodifiableCopy(); } } /** creates a {@link Collections#unmodifiableList(List)} wrapper around this list. the method is efficient,
public void testContainingNullAndUnmodifiable() { MutableList<Object> x = MutableList.<Object>of("x", null); Assert.assertTrue(x.contains(null)); List<Object> x1 = x.asUnmodifiable(); List<Object> x2 = x.asUnmodifiableCopy(); List<Object> x3 = x.asImmutableCopy(); x.remove(null); Assert.assertFalse(x.contains(null)); Assert.assertFalse(x1.contains(null)); Assert.assertTrue(x2.contains(null)); Assert.assertTrue(x3.contains(null)); try { x1.remove("x"); Assert.fail(); } catch (Exception e) { /* expected */ } try { x2.remove("x"); Assert.fail(); } catch (Exception e) { /* expected */ } try { x3.remove("x"); Assert.fail(); } catch (Exception e) { /* expected */ } Assert.assertTrue(x1.contains("x")); Assert.assertTrue(x2.contains("x")); Assert.assertTrue(x3.contains("x")); }