|
1 | 1 | package org.baeldung.guava.collections; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertNull; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.Collection; |
3 | 7 | import java.util.List; |
| 8 | +import java.util.Set; |
4 | 9 |
|
5 | 10 | import org.junit.Test; |
6 | 11 |
|
7 | 12 | import com.google.common.base.Function; |
| 13 | +import com.google.common.base.Predicate; |
| 14 | +import com.google.common.collect.Iterables; |
8 | 15 | import com.google.common.collect.Lists; |
| 16 | +import com.google.common.collect.Sets; |
9 | 17 |
|
10 | 18 | public class GuavaCollectionsExamplesTest { |
11 | 19 |
|
@@ -35,8 +43,69 @@ public final void whenDowncastingGenerifiedCollectionToNewGenerifiedCollection_t |
35 | 43 | } |
36 | 44 |
|
37 | 45 | @Test |
38 | | - public final void when_then() { |
39 | | - // |
| 46 | + public final void whenAddingAnIterableToACollection_thenAddedOK() { |
| 47 | + final Iterable<String> iter = Lists.newArrayList(); |
| 48 | + final Collection<String> collector = Lists.newArrayList(); |
| 49 | + Iterables.addAll(collector, iter); |
| 50 | + } |
| 51 | + |
| 52 | + // |
| 53 | + |
| 54 | + @Test |
| 55 | + public final void whenCheckingIfCollectionContainsElementsByCustomMatch1_thenContains() { |
| 56 | + final Iterable<String> theCollection = Lists.newArrayList("a", "bc", "def"); |
| 57 | + final boolean contains = Iterables.any(theCollection, new Predicate<String>() { |
| 58 | + @Override |
| 59 | + public final boolean apply(final String input) { |
| 60 | + return input.length() == 1; |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + assertTrue(contains); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public final void whenCheckingIfCollectionContainsElementsByCustomMatch2_thenContains() { |
| 69 | + final Set<String> theCollection = Sets.newHashSet("a", "bc", "def"); |
| 70 | + |
| 71 | + final boolean contains = !Sets.filter(theCollection, new Predicate<String>() { |
| 72 | + @Override |
| 73 | + public final boolean apply(final String input) { |
| 74 | + return input.length() == 1; |
| 75 | + } |
| 76 | + }).isEmpty(); |
| 77 | + |
| 78 | + assertTrue(contains); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public final void whenCheckingIfCollectionContainsElementsByCustomMatch3_thenContains() { |
| 83 | + final Iterable<String> theCollection = Sets.newHashSet("a", "bc", "def"); |
| 84 | + |
| 85 | + final boolean contains = Iterables.find(theCollection, new Predicate<String>() { |
| 86 | + @Override |
| 87 | + public final boolean apply(final String input) { |
| 88 | + return input.length() == 1; |
| 89 | + } |
| 90 | + }) != null; |
| 91 | + |
| 92 | + assertTrue(contains); |
| 93 | + } |
| 94 | + |
| 95 | + // |
| 96 | + |
| 97 | + @Test |
| 98 | + public final void givenNoSearchResult_whenFindingElementInIterable_thenNoException() { |
| 99 | + final Iterable<String> theCollection = Sets.newHashSet("abcd", "efgh", "ijkl"); |
| 100 | + |
| 101 | + final String found = Iterables.find(theCollection, new Predicate<String>() { |
| 102 | + @Override |
| 103 | + public final boolean apply(final String input) { |
| 104 | + return input.length() == 1; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + assertNull(found); |
40 | 109 | } |
41 | 110 |
|
42 | 111 | } |
0 commit comments