|
| 1 | +package org.baeldung.guava; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.contains; |
| 4 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.google.common.base.Function; |
| 16 | +import com.google.common.base.Joiner; |
| 17 | +import com.google.common.collect.ArrayListMultimap; |
| 18 | +import com.google.common.collect.BiMap; |
| 19 | +import com.google.common.collect.ClassToInstanceMap; |
| 20 | +import com.google.common.collect.ContiguousSet; |
| 21 | +import com.google.common.collect.DiscreteDomain; |
| 22 | +import com.google.common.collect.HashBasedTable; |
| 23 | +import com.google.common.collect.HashBiMap; |
| 24 | +import com.google.common.collect.HashMultiset; |
| 25 | +import com.google.common.collect.ImmutableList; |
| 26 | +import com.google.common.collect.ImmutableMap; |
| 27 | +import com.google.common.collect.ImmutableSet; |
| 28 | +import com.google.common.collect.ImmutableSortedMap; |
| 29 | +import com.google.common.collect.Iterables; |
| 30 | +import com.google.common.collect.Lists; |
| 31 | +import com.google.common.collect.Multimap; |
| 32 | +import com.google.common.collect.Multimaps; |
| 33 | +import com.google.common.collect.Multiset; |
| 34 | +import com.google.common.collect.Multisets; |
| 35 | +import com.google.common.collect.MutableClassToInstanceMap; |
| 36 | +import com.google.common.collect.Ordering; |
| 37 | +import com.google.common.collect.Range; |
| 38 | +import com.google.common.collect.RangeSet; |
| 39 | +import com.google.common.collect.Sets; |
| 40 | +import com.google.common.collect.Table; |
| 41 | +import com.google.common.collect.Tables; |
| 42 | +import com.google.common.collect.TreeRangeSet; |
| 43 | + |
| 44 | +public class GuavaCollectionTypesTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + public void whenReverseList_thenReversed() { |
| 48 | + final List<String> names = Lists.newArrayList("John", "Adam", "Jane"); |
| 49 | + names.add("Tom"); |
| 50 | + assertEquals(4, names.size()); |
| 51 | + |
| 52 | + final List<String> reversed = Lists.reverse(names); |
| 53 | + assertThat(reversed, contains("Tom", "Jane", "Adam", "John")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void whenCreateCharacterListFromString_thenCreated() { |
| 58 | + final List<Character> chars = Lists.charactersOf("John"); |
| 59 | + |
| 60 | + assertEquals(4, chars.size()); |
| 61 | + assertThat(chars, contains('J', 'o', 'h', 'n')); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void whenCalculateUnion_thenCorrect() { |
| 66 | + final Set<Character> first = ImmutableSet.of('a', 'b', 'c'); |
| 67 | + final Set<Character> second = ImmutableSet.of('b', 'c', 'd'); |
| 68 | + |
| 69 | + final Set<Character> union = Sets.union(first, second); |
| 70 | + assertThat(union, containsInAnyOrder('a', 'b', 'c', 'd')); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void whenCalculateSetsProduct_thenCorrect() { |
| 75 | + final Set<Character> first = ImmutableSet.of('a', 'b'); |
| 76 | + final Set<Character> second = ImmutableSet.of('c', 'd'); |
| 77 | + final Set<List<Character>> result = Sets.cartesianProduct(ImmutableList.of(first, second)); |
| 78 | + |
| 79 | + final Function<List<Character>, String> func = new Function<List<Character>, String>() { |
| 80 | + @Override |
| 81 | + public final String apply(final List<Character> input) { |
| 82 | + return Joiner.on(" ").join(input); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + final Iterable<String> joined = Iterables.transform(result, func); |
| 87 | + assertThat(joined, containsInAnyOrder("a c", "a d", "b c", "b d")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void whenCreateRangeOfIntegersSet_thenCreated() { |
| 92 | + final int start = 10; |
| 93 | + final int end = 30; |
| 94 | + final ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(start, end), DiscreteDomain.integers()); |
| 95 | + |
| 96 | + assertEquals(21, set.size()); |
| 97 | + assertEquals(10, set.first().intValue()); |
| 98 | + assertEquals(30, set.last().intValue()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void whenCreateRangeSet_thenCreated() { |
| 103 | + final RangeSet<Integer> rangeSet = TreeRangeSet.create(); |
| 104 | + rangeSet.add(Range.closed(1, 10)); |
| 105 | + rangeSet.add(Range.closed(12, 15)); |
| 106 | + |
| 107 | + assertEquals(2, rangeSet.asRanges().size()); |
| 108 | + |
| 109 | + rangeSet.add(Range.closed(10, 12)); |
| 110 | + assertTrue(rangeSet.encloses(Range.closed(1, 15))); |
| 111 | + assertEquals(1, rangeSet.asRanges().size()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void whenInsertDuplicatesInMultiSet_thenInserted() { |
| 116 | + final Multiset<String> names = HashMultiset.create(); |
| 117 | + names.add("John"); |
| 118 | + names.add("Adam", 3); |
| 119 | + names.add("John"); |
| 120 | + |
| 121 | + assertEquals(2, names.count("John")); |
| 122 | + names.remove("John"); |
| 123 | + assertEquals(1, names.count("John")); |
| 124 | + |
| 125 | + assertEquals(3, names.count("Adam")); |
| 126 | + names.remove("Adam", 2); |
| 127 | + assertEquals(1, names.count("Adam")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void whenGetTopUsingMultiSet_thenCorrect() { |
| 132 | + final Multiset<String> names = HashMultiset.create(); |
| 133 | + names.add("John"); |
| 134 | + names.add("Adam", 5); |
| 135 | + names.add("Jane"); |
| 136 | + names.add("Tom", 2); |
| 137 | + |
| 138 | + final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet(); |
| 139 | + final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2); |
| 140 | + assertEquals(2, topTwo.size()); |
| 141 | + assertEquals("Adam", topTwo.get(0)); |
| 142 | + assertEquals("Tom", topTwo.get(1)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void whenCreateMap_thenCreated() { |
| 147 | + final Map<String, Integer> salary = ImmutableMap.<String, Integer> builder().put("John", 1000).put("Jane", 1500).put("Adam", 2000).put("Tom", 2000).build(); |
| 148 | + |
| 149 | + assertEquals(1000, salary.get("John").intValue()); |
| 150 | + assertEquals(2000, salary.get("Tom").intValue()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void whenUseSortedMap_thenKeysAreSorted() { |
| 155 | + final ImmutableSortedMap<String, Integer> salary = new ImmutableSortedMap.Builder<String, Integer>(Ordering.natural()).put("John", 1000).put("Jane", 1500).put("Adam", 2000).put("Tom", 2000).build(); |
| 156 | + |
| 157 | + assertEquals("Adam", salary.firstKey()); |
| 158 | + assertEquals(2000, salary.lastEntry().getValue().intValue()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void whenCreateBiMap_thenCreated() { |
| 163 | + final BiMap<String, Integer> words = HashBiMap.create(); |
| 164 | + words.put("First", 1); |
| 165 | + words.put("Second", 2); |
| 166 | + words.put("Third", 3); |
| 167 | + |
| 168 | + assertEquals(2, words.get("Second").intValue()); |
| 169 | + assertEquals("Third", words.inverse().get(3)); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void whenCreateMultimap_thenCreated() { |
| 174 | + final Multimap<String, String> multimap = ArrayListMultimap.create(); |
| 175 | + multimap.put("fruit", "apple"); |
| 176 | + multimap.put("fruit", "banana"); |
| 177 | + multimap.put("pet", "cat"); |
| 178 | + multimap.put("pet", "dog"); |
| 179 | + |
| 180 | + assertThat(multimap.get("fruit"), containsInAnyOrder("apple", "banana")); |
| 181 | + assertThat(multimap.get("pet"), containsInAnyOrder("cat", "dog")); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void whenGroupListUsingMultimap_thenGrouped() { |
| 186 | + final List<String> names = Lists.newArrayList("John", "Adam", "Tom"); |
| 187 | + final Function<String, Integer> function = new Function<String, Integer>() { |
| 188 | + @Override |
| 189 | + public final Integer apply(final String input) { |
| 190 | + return input.length(); |
| 191 | + } |
| 192 | + }; |
| 193 | + final Multimap<Integer, String> groups = Multimaps.index(names, function); |
| 194 | + |
| 195 | + assertThat(groups.get(3), containsInAnyOrder("Tom")); |
| 196 | + assertThat(groups.get(4), containsInAnyOrder("John", "Adam")); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void whenCreateTable_thenCreated() { |
| 201 | + final Table<String, String, Integer> distance = HashBasedTable.create(); |
| 202 | + distance.put("London", "Paris", 340); |
| 203 | + distance.put("New York", "Los Angeles", 3940); |
| 204 | + distance.put("London", "New York", 5576); |
| 205 | + |
| 206 | + assertEquals(3940, distance.get("New York", "Los Angeles").intValue()); |
| 207 | + assertThat(distance.columnKeySet(), containsInAnyOrder("Paris", "New York", "Los Angeles")); |
| 208 | + assertThat(distance.rowKeySet(), containsInAnyOrder("London", "New York")); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void whenTransposeTable_thenCorrect() { |
| 213 | + final Table<String, String, Integer> distance = HashBasedTable.create(); |
| 214 | + distance.put("London", "Paris", 340); |
| 215 | + distance.put("New York", "Los Angeles", 3940); |
| 216 | + distance.put("London", "New York", 5576); |
| 217 | + |
| 218 | + final Table<String, String, Integer> transposed = Tables.transpose(distance); |
| 219 | + assertThat(transposed.rowKeySet(), containsInAnyOrder("Paris", "New York", "Los Angeles")); |
| 220 | + assertThat(transposed.columnKeySet(), containsInAnyOrder("London", "New York")); |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + public void whenCreateClassToInstanceMap_thenCreated() { |
| 225 | + final ClassToInstanceMap<Number> numbers = MutableClassToInstanceMap.create(); |
| 226 | + numbers.putInstance(Integer.class, 1); |
| 227 | + numbers.putInstance(Double.class, 1.5); |
| 228 | + |
| 229 | + assertEquals(1, numbers.get(Integer.class)); |
| 230 | + assertEquals(1.5, numbers.get(Double.class)); |
| 231 | + } |
| 232 | + |
| 233 | + |
| 234 | +} |
0 commit comments