package org.baeldung.guava; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; 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 GuavaCollectionsExamplesUnitTest { // tests @Test public final void whenDowncastingGenerifiedCollectionToNewGenerifiedCollection_thenCastIsOK() { final class CastFunction implements Function { @SuppressWarnings("unchecked") @Override public final T apply(final F from) { return (T) from; } } final List originalList = Lists.newArrayList(); final List selectedProducts = Lists.transform(originalList, new CastFunction()); System.out.println(selectedProducts); } @SuppressWarnings({ "unchecked" }) @Test public final void whenDowncastingGenerifiedCollectionToNewGenerifiedCollection_thenCastIsOK2() { final List originalList = Lists.newArrayList(); final List selectedProducts = (List) (List) originalList; System.out.println(selectedProducts); } @Test public final void whenAddingAnIterableToACollection_thenAddedOK() { final Iterable iter = Lists.newArrayList(); final Collection collector = Lists.newArrayList(); Iterables.addAll(collector, iter); } // @Test public final void whenCheckingIfCollectionContainsElementsByCustomMatch1_thenContains() { final Iterable theCollection = Lists.newArrayList("a", "bc", "def"); final boolean contains = Iterables.any(theCollection, new Predicate() { @Override public final boolean apply(final String input) { return input.length() == 1; } }); assertTrue(contains); } @Test public final void whenCheckingIfCollectionContainsElementsByCustomMatch2_thenContains() { final Set theCollection = Sets.newHashSet("a", "bc", "def"); final boolean contains = !Sets.filter(theCollection, new Predicate() { @Override public final boolean apply(final String input) { return input.length() == 1; } }).isEmpty(); assertTrue(contains); } @Test public final void whenCheckingIfCollectionContainsElementsByCustomMatch3_thenContains() { final Iterable theCollection = Sets.newHashSet("a", "bc", "def"); final boolean contains = Iterables.find(theCollection, new Predicate() { @Override public final boolean apply(final String input) { return input.length() == 1; } }) != null; assertTrue(contains); } // @Test(expected = NoSuchElementException.class) public final void givenNoSearchResult_whenFindingElementInIterable_thenException() { final Iterable theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); final String found = Iterables.find(theCollection, new Predicate() { @Override public final boolean apply(final String input) { return input.length() == 1; } }); 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(); } // unmodifiable @Test(expected = UnsupportedOperationException.class) public final void givenUnmodifiableViewOverIterable_whenTryingToRemove_thenNotAllowed() { final List numbers = Lists.newArrayList(1, 2, 3); final Iterable unmodifiableIterable = Iterables.unmodifiableIterable(numbers); final Iterator iterator = unmodifiableIterable.iterator(); if (iterator.hasNext()) { iterator.remove(); } } }