public void testUnmodifiableArrayListMultimapRandomAccess() { ListMultimap<String, Integer> delegate = ArrayListMultimap.create(); delegate.put("foo", 1); delegate.put("foo", 3); ListMultimap<String, Integer> multimap = Multimaps.unmodifiableListMultimap(delegate); assertTrue(multimap.get("foo") instanceof RandomAccess); assertTrue(multimap.get("bar") instanceof RandomAccess); }
public void testForMapRemoveAll() { Map<String, Integer> map = Maps.newHashMap(); map.put("foo", 1); map.put("bar", 2); map.put("cow", 3); Multimap<String, Integer> multimap = Multimaps.forMap(map); assertEquals(3, multimap.size()); assertEquals(Collections.emptySet(), multimap.removeAll("dog")); assertEquals(3, multimap.size()); assertTrue(multimap.containsKey("bar")); assertEquals(Collections.singleton(2), multimap.removeAll("bar")); assertEquals(2, multimap.size()); assertFalse(multimap.containsKey("bar")); }
public void testInvertFrom() { ImmutableMultimap<Integer, String> empty = ImmutableMultimap.of(); // typical usage example - sad that ArrayListMultimap.create() won't work Multimap<String, Integer> multimap = Multimaps.invertFrom(empty, ArrayListMultimap.<String, Integer>create()); assertTrue(multimap.isEmpty()); ImmutableMultimap<Integer, String> single = new ImmutableMultimap.Builder<Integer, String>().put(1, "one").put(2, "two").build(); // copy into existing multimap assertSame(multimap, Multimaps.invertFrom(single, multimap)); ImmutableMultimap<String, Integer> expected = new ImmutableMultimap.Builder<String, Integer>().put("one", 1).put("two", 2).build(); assertEquals(expected, multimap); }
public void testForMapAsMap() { Map<String, Integer> map = Maps.newHashMap(); map.put("foo", 1); map.put("bar", 2); Map<String, Collection<Integer>> asMap = Multimaps.forMap(map).asMap(); assertEquals(Collections.singleton(1), asMap.get("foo")); assertNull(asMap.get("cow")); assertTrue(asMap.containsKey("foo")); assertFalse(asMap.containsKey("cow")); Set<Entry<String, Collection<Integer>>> entries = asMap.entrySet(); assertFalse(entries.contains((Object) 4.5)); assertFalse(entries.remove((Object) 4.5)); assertFalse(entries.contains(Maps.immutableEntry("foo", Collections.singletonList(1)))); assertFalse(entries.remove(Maps.immutableEntry("foo", Collections.singletonList(1)))); assertFalse(entries.contains(Maps.immutableEntry("foo", Sets.newLinkedHashSet(asList(1, 2))))); assertFalse(entries.remove(Maps.immutableEntry("foo", Sets.newLinkedHashSet(asList(1, 2))))); assertFalse(entries.contains(Maps.immutableEntry("foo", Collections.singleton(2)))); assertFalse(entries.remove(Maps.immutableEntry("foo", Collections.singleton(2)))); assertTrue(map.containsKey("foo")); assertTrue(entries.contains(Maps.immutableEntry("foo", Collections.singleton(1)))); assertTrue(entries.remove(Maps.immutableEntry("foo", Collections.singleton(1)))); assertFalse(map.containsKey("foo")); }
public void testNewMultimapValueCollectionMatchesList() { Supplier<LinkedList<Integer>> factory = new ListSupplier(); Map<Color, Collection<Integer>> map = Maps.newEnumMap(Color.class); Multimap<Color, Integer> multimap = Multimaps.newMultimap(map, factory); assertTrue(multimap.get(Color.BLUE) instanceof List); }
public void testNewMultimapValueCollectionMatchesNavigableSet() { Supplier<TreeSet<Integer>> factory = new SortedSetSupplier(); Map<Color, Collection<Integer>> map = Maps.newEnumMap(Color.class); Multimap<Color, Integer> multimap = Multimaps.newMultimap(map, factory); assertTrue(multimap.get(Color.BLUE) instanceof NavigableSet); }
public void testUnmodifiableMultimapIsView() { Multimap<String, Integer> mod = HashMultimap.create(); Multimap<String, Integer> unmod = Multimaps.unmodifiableMultimap(mod); assertEquals(mod, unmod); mod.put("foo", 1); assertTrue(unmod.containsEntry("foo", 1)); assertEquals(mod, unmod); }
public void testNewListMultimap() { CountingSupplier<LinkedList<Integer>> factory = new ListSupplier(); Map<Color, Collection<Integer>> map = Maps.newTreeMap(); ListMultimap<Color, Integer> multimap = Multimaps.newListMultimap(map, factory); assertEquals(0, factory.count); multimap.putAll(Color.BLUE, asList(3, 1, 4, 1)); assertEquals(1, factory.count); multimap.putAll(Color.RED, asList(2, 7, 1, 8)); assertEquals(2, factory.count); assertEquals("{BLUE=[3, 1, 4, 1], RED=[2, 7, 1, 8]}", multimap.toString()); assertFalse(multimap.get(Color.BLUE) instanceof RandomAccess); assertTrue(multimap.keySet() instanceof SortedSet); assertTrue(multimap.asMap() instanceof SortedMap); }
assertEquals(multimap.hashCode(), multimapView.hashCode()); assertEquals(multimap.size(), multimapView.size()); assertTrue(multimapView.containsKey("foo")); assertTrue(multimapView.containsValue(1)); assertTrue(multimapView.containsEntry("bar", 2)); assertEquals(Collections.singleton(1), multimapView.get("foo")); assertEquals(Collections.singleton(2), multimapView.get("bar")); assertFalse(multimapView.containsKey("foo")); assertFalse(map.containsKey("foo")); assertTrue(map.isEmpty()); assertTrue(multimapView.isEmpty()); multimap.clear(); assertEquals(multimap.toString(), multimapView.toString());
public void testUnmodifiableArrayListMultimapRandomAccess() { ListMultimap<String, Integer> delegate = ArrayListMultimap.create(); delegate.put("foo", 1); delegate.put("foo", 3); ListMultimap<String, Integer> multimap = Multimaps.unmodifiableListMultimap(delegate); assertTrue(multimap.get("foo") instanceof RandomAccess); assertTrue(multimap.get("bar") instanceof RandomAccess); }
public void testForMapRemoveAll() { Map<String, Integer> map = Maps.newHashMap(); map.put("foo", 1); map.put("bar", 2); map.put("cow", 3); Multimap<String, Integer> multimap = Multimaps.forMap(map); assertEquals(3, multimap.size()); assertEquals(Collections.emptySet(), multimap.removeAll("dog")); assertEquals(3, multimap.size()); assertTrue(multimap.containsKey("bar")); assertEquals(Collections.singleton(2), multimap.removeAll("bar")); assertEquals(2, multimap.size()); assertFalse(multimap.containsKey("bar")); }
assertEquals(Collections.singleton(1), asMap.get("foo")); assertNull(asMap.get("cow")); assertTrue(asMap.containsKey("foo")); assertFalse(asMap.containsKey("cow")); assertFalse(entries.remove(Maps.immutableEntry("foo", Collections.singleton(2)))); assertTrue(map.containsKey("foo")); assertTrue(entries.contains(Maps.immutableEntry("foo", Collections.singleton(1)))); assertTrue(entries.remove(Maps.immutableEntry("foo", Collections.singleton(1)))); assertFalse(map.containsKey("foo"));
public void testUnmodifiableMultimapIsView() { Multimap<String, Integer> mod = HashMultimap.create(); Multimap<String, Integer> unmod = Multimaps.unmodifiableMultimap(mod); assertEquals(mod, unmod); mod.put("foo", 1); assertTrue(unmod.containsEntry("foo", 1)); assertEquals(mod, unmod); }
public void testInvertFrom() { ImmutableMultimap<Integer, String> empty = ImmutableMultimap.of(); // typical usage example - sad that ArrayListMultimap.create() won't work Multimap<String, Integer> multimap = Multimaps.invertFrom(empty, ArrayListMultimap.<String, Integer>create()); assertTrue(multimap.isEmpty()); ImmutableMultimap<Integer, String> single = new ImmutableMultimap.Builder<Integer, String>() .put(1, "one") .put(2, "two") .build(); // copy into existing multimap assertSame(multimap, Multimaps.invertFrom(single, multimap)); ImmutableMultimap<String, Integer> expected = new ImmutableMultimap.Builder<String, Integer>() .put("one", 1) .put("two", 2) .build(); assertEquals(expected, multimap); }
public void testNewListMultimap() { CountingSupplier<LinkedList<Integer>> factory = new ListSupplier(); Map<Color, Collection<Integer>> map = Maps.newTreeMap(); ListMultimap<Color, Integer> multimap = Multimaps.newListMultimap(map, factory); assertEquals(0, factory.count); multimap.putAll(Color.BLUE, asList(3, 1, 4, 1)); assertEquals(1, factory.count); multimap.putAll(Color.RED, asList(2, 7, 1, 8)); assertEquals(2, factory.count); assertEquals("{BLUE=[3, 1, 4, 1], RED=[2, 7, 1, 8]}", multimap.toString()); assertFalse(multimap.get(Color.BLUE) instanceof RandomAccess); assertTrue(multimap.keySet() instanceof SortedSet); assertTrue(multimap.asMap() instanceof SortedMap); }
assertEquals(multimap.hashCode(), multimapView.hashCode()); assertEquals(multimap.size(), multimapView.size()); assertTrue(multimapView.containsKey("foo")); assertTrue(multimapView.containsValue(1)); assertTrue(multimapView.containsEntry("bar", 2)); assertEquals(Collections.singleton(1), multimapView.get("foo")); assertEquals(Collections.singleton(2), multimapView.get("bar")); assertFalse(multimapView.containsKey("foo")); assertFalse(map.containsKey("foo")); assertTrue(map.isEmpty()); assertTrue(multimapView.isEmpty()); multimap.clear(); assertEquals(multimap.toString(), multimapView.toString());