diff --git a/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java b/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java index 326a1964fb..b270653ffa 100644 --- a/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java +++ b/guava/src/test/java/org/baeldung/guava/collections/GuavaCollectionsExamplesTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; @@ -12,10 +13,16 @@ import org.junit.Test; import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.collect.Sets; +@SuppressWarnings("unused") public class GuavaCollectionsExamplesTest { // tests @@ -96,7 +103,7 @@ public class GuavaCollectionsExamplesTest { // @Test(expected = NoSuchElementException.class) - public final void givenNoSearchResult_whenFindingElementInIterable_thenNoException() { + public final void givenNoSearchResult_whenFindingElementInIterable_thenException() { final Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); final String found = Iterables.find(theCollection, new Predicate() { @@ -109,4 +116,61 @@ public class GuavaCollectionsExamplesTest { assertNull(found); } + @Test + public final void givenNoSearchResult_whenFindingElementInIterableWithSpecifiedReturn_thenNoException() { + final Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); + + final Predicate inputOfLengthOne = new Predicate() { + @Override + public final boolean apply(final String input) { + return input.length() == 1; + } + }; + final String found = Iterables.find(theCollection, inputOfLengthOne, null); + + assertNull(found); + } + + // purge of nulls + + @Test + public final void givenListContainsNulls_whenPurgedOfNulls_thenNoLongerContainsNulls() { + final List values = Lists.newArrayList("a", null, "b", "c"); + final Iterable withoutNulls = Iterables.filter(values, Predicates.notNull()); + System.out.println(withoutNulls); + } + + // immutable collections + + @Test + public final void whenCreatingImuutableCollections_thenNoExceptions() { + final ImmutableList immutableList = ImmutableList.of("a", "b", "c"); + final ImmutableSet immutableSet = ImmutableSet.of("a", "b", "c"); + final ImmutableMap imuttableMap = ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"); + } + + @Test + public final void whenTransformingCollectionsToImmutable_thenNoExceptions() { + final List muttableList = Lists.newArrayList(); + final ImmutableList immutableList = ImmutableList.copyOf(muttableList); + + final Set muttableSet = Sets.newHashSet(); + final ImmutableSet immutableSet = ImmutableSet.copyOf(muttableSet); + + final Map muttableMap = Maps.newHashMap(); + final ImmutableMap imuttableMap = ImmutableMap.copyOf(muttableMap); + } + + @Test + public final void whenTransformingCollectionsToImmutableViaBuilders_thenNoExceptions() { + final List muttableList = Lists.newArrayList(); + final ImmutableList immutableList = ImmutableList. builder().addAll(muttableList).build(); + + final Set muttableSet = Sets.newHashSet(); + final ImmutableSet immutableSet = ImmutableSet. builder().addAll(muttableSet).build(); + + final Map muttableMap = Maps.newHashMap(); + final ImmutableMap imuttableMap = ImmutableMap. builder().putAll(muttableMap).build(); + } + }