|
| 1 | +package org.baeldung.guava; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.google.common.base.Function; |
| 12 | +import com.google.common.base.Functions; |
| 13 | +import com.google.common.base.Predicate; |
| 14 | +import com.google.common.base.Predicates; |
| 15 | +import com.google.common.collect.Collections2; |
| 16 | +import com.google.common.collect.FluentIterable; |
| 17 | +import com.google.common.collect.ListMultimap; |
| 18 | +import com.google.common.collect.Lists; |
| 19 | +import com.google.common.collect.Multimaps; |
| 20 | +import com.google.common.collect.Ordering; |
| 21 | + |
| 22 | +public class GuavaFilterCollectionsTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + public void whenFilterCollection_thenFiltered() { |
| 26 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 27 | + final Collection<String> result = Collections2.filter(names, Predicates.containsPattern("a")); |
| 28 | + |
| 29 | + assertEquals(2, result.size()); |
| 30 | + assertEquals("[Jane, Adam]", result.toString()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenFilterCollectionWithCustomPredicate_thenFiltered() { |
| 35 | + final Predicate<String> predicate = new Predicate<String>() { |
| 36 | + @Override |
| 37 | + public final boolean apply(final String input) { |
| 38 | + return input.startsWith("A") || input.startsWith("J"); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 43 | + final Collection<String> result = Collections2.filter(names, predicate); |
| 44 | + |
| 45 | + assertEquals(3, result.size()); |
| 46 | + assertEquals("[John, Jane, Adam]", result.toString()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenFilterUsingMultiplePredicates_thenFiltered() { |
| 51 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 52 | + final Collection<String> result = Collections2.filter(names, Predicates.or(Predicates.containsPattern("J"), Predicates.not(Predicates.containsPattern("a")))); |
| 53 | + |
| 54 | + assertEquals(3, result.size()); |
| 55 | + assertEquals("[John, Jane, Tom]", result.toString()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenTransformCollection_thenTransformed() { |
| 60 | + final Function<String, Integer> function = new Function<String, Integer>() { |
| 61 | + @Override |
| 62 | + public final Integer apply(final String input) { |
| 63 | + return input.length(); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 68 | + final Collection<Integer> result = Collections2.transform(names, function); |
| 69 | + |
| 70 | + assertEquals(4, result.size()); |
| 71 | + assertEquals("[4, 4, 4, 3]", result.toString()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void whenTransformUsingComposedFunction_thenTransformed() { |
| 76 | + final Function<String, Integer> f1 = new Function<String, Integer>() { |
| 77 | + @Override |
| 78 | + public final Integer apply(final String input) { |
| 79 | + return input.length(); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + final Function<Integer, Boolean> f2 = new Function<Integer, Boolean>() { |
| 84 | + @Override |
| 85 | + public final Boolean apply(final Integer input) { |
| 86 | + return input % 2 == 0; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 91 | + final Collection<Boolean> result = Collections2.transform(names, Functions.compose(f2, f1)); |
| 92 | + |
| 93 | + assertEquals(4, result.size()); |
| 94 | + assertEquals("[true, true, true, false]", result.toString()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void whenFilterAndTransformCollection_thenCorrect() { |
| 99 | + final Predicate<String> predicate = new Predicate<String>() { |
| 100 | + @Override |
| 101 | + public final boolean apply(final String input) { |
| 102 | + return input.startsWith("A") || input.startsWith("T"); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + final Function<String, Integer> func = new Function<String, Integer>() { |
| 107 | + @Override |
| 108 | + public final Integer apply(final String input) { |
| 109 | + return input.length(); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 114 | + final Collection<Integer> result = FluentIterable.from(names).filter(predicate).transform(func).toList(); |
| 115 | + |
| 116 | + assertEquals(2, result.size()); |
| 117 | + assertEquals("[4, 3]", result.toString()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void whenSortCollection_thenSorted() { |
| 122 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 123 | + Collections.sort(names, Ordering.natural()); |
| 124 | + assertEquals("[Adam, Jane, John, Tom]", names.toString()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void whenSortCollectionUsingCustomOrdering_thenSorted() { |
| 129 | + final Ordering<String> ordering = Ordering.natural().onResultOf(new Function<String, Integer>() { |
| 130 | + @Override |
| 131 | + public final Integer apply(final String input) { |
| 132 | + return input.length(); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 137 | + Collections.sort(names, ordering); |
| 138 | + assertEquals("[Tom, John, Jane, Adam]", names.toString()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void whenGroupCollection_thenGrouped() { |
| 143 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 144 | + final ListMultimap<Integer, String> groups = Multimaps.index(names, new Function<String, Integer>() { |
| 145 | + @Override |
| 146 | + public final Integer apply(final String input) { |
| 147 | + return input.length(); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + assertEquals(2, groups.keySet().size()); |
| 152 | + assertEquals(3, groups.get(4).size()); |
| 153 | + assertEquals("[Tom]", groups.get(3).toString()); |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | + |
0 commit comments