@Override public Map<String, Collection<Integer>> create(Object... elements) { ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); // assumes that each set is a singleton or less (as is done for the samples) for (Object elem : elements) { @SuppressWarnings("unchecked") // safe by generator contract Entry<String, Collection<Integer>> entry = (Entry<String, Collection<Integer>>) elem; Integer value = Iterables.getOnlyElement(entry.getValue()); builder.put(entry.getKey(), value); } return builder.build().asMultimap().asMap(); }
@Override protected Map<String, Collection<Integer>> makeEmptyMap() { return ImmutableSetMultimap.<String, Integer>of().asMap(); }
@Override protected Map<String, Collection<Integer>> makePopulatedMap() { Multimap<String, Integer> delegate = HashMultimap.create(); populate(delegate); return ImmutableSetMultimap.copyOf(delegate).asMap(); } }
public void testAsMultimapCaches() { ImmutableMap<String, Integer> map = ImmutableMap.of("one", 1); ImmutableSetMultimap<String, Integer> multimap1 = map.asMultimap(); ImmutableSetMultimap<String, Integer> multimap2 = map.asMultimap(); assertEquals(1, multimap1.asMap().size()); assertSame(multimap1, multimap2); }
public void testBuilderOrderValuesBy() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("b", 3); builder.put("d", 2); builder.put("a", 5); builder.orderValuesBy(Collections.reverseOrder()); builder.put("c", 4); builder.put("a", 2); builder.put("b", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("b", "d", "a", "c").inOrder(); assertThat(multimap.values()).containsExactly(6, 3, 2, 5, 2, 4).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("b")).containsExactly(6, 3).inOrder(); assertTrue(multimap.get("a") instanceof ImmutableSortedSet); assertEquals( Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.get("a")).comparator()); assertTrue(multimap.get("x") instanceof ImmutableSortedSet); assertEquals( Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.get("x")).comparator()); assertTrue(multimap.asMap().get("a") instanceof ImmutableSortedSet); assertEquals( Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.asMap().get("a")).comparator()); }
public void testBuilderOrderKeysAndValuesBy() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("b", 3); builder.put("d", 2); builder.put("a", 5); builder.orderKeysBy(Collections.reverseOrder()); builder.orderValuesBy(Collections.reverseOrder()); builder.put("c", 4); builder.put("a", 2); builder.put("b", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("d", "c", "b", "a").inOrder(); assertThat(multimap.values()).containsExactly(2, 4, 6, 3, 5, 2).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("b")).containsExactly(6, 3).inOrder(); assertTrue(multimap.get("a") instanceof ImmutableSortedSet); assertEquals( Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.get("a")).comparator()); assertTrue(multimap.get("x") instanceof ImmutableSortedSet); assertEquals( Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.get("x")).comparator()); assertTrue(multimap.asMap().get("a") instanceof ImmutableSortedSet); assertEquals( Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.asMap().get("a")).comparator()); }
public void testBuilderOrderKeysBy() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("b", 3); builder.put("d", 2); builder.put("a", 5); builder.orderKeysBy(Collections.reverseOrder()); builder.put("c", 4); builder.put("a", 2); builder.put("b", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("d", "c", "b", "a").inOrder(); assertThat(multimap.values()).containsExactly(2, 4, 3, 6, 5, 2).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("b")).containsExactly(3, 6).inOrder(); assertFalse(multimap.get("a") instanceof ImmutableSortedSet); assertFalse(multimap.get("x") instanceof ImmutableSortedSet); assertFalse(multimap.asMap().get("a") instanceof ImmutableSortedSet); }
public void testBuilderOrderKeysByDuplicates() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("bb", 3); builder.put("d", 2); builder.put("a", 5); builder.orderKeysBy( new Ordering<String>() { @Override public int compare(String left, String right) { return left.length() - right.length(); } }); builder.put("cc", 4); builder.put("a", 2); builder.put("bb", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("d", "a", "bb", "cc").inOrder(); assertThat(multimap.values()).containsExactly(2, 5, 2, 3, 6, 4).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("bb")).containsExactly(3, 6).inOrder(); assertFalse(multimap.get("a") instanceof ImmutableSortedSet); assertFalse(multimap.get("x") instanceof ImmutableSortedSet); assertFalse(multimap.asMap().get("a") instanceof ImmutableSortedSet); }
@Override public Map<String, Collection<Integer>> create(Object... elements) { ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); // assumes that each set is a singleton or less (as is done for the samples) for (Object elem : elements) { @SuppressWarnings("unchecked") // safe by generator contract Entry<String, Collection<Integer>> entry = (Entry<String, Collection<Integer>>) elem; Integer value = Iterables.getOnlyElement(entry.getValue()); builder.put(entry.getKey(), value); } return builder.build().asMultimap().asMap(); }
@Override public void write(JsonWriter out, T value) throws IOException { ImmutableSetMultimap<?, ?> multimap = (ImmutableSetMultimap<?, ?>) value; adapter.write(out, (Map) multimap.asMap()); }
@Override public void write(JsonWriter out, T value) throws IOException { ImmutableSetMultimap<?, ?> multimap = (ImmutableSetMultimap<?, ?>) value; adapter.write(out, (Map) multimap.asMap()); }
private ImmutableSet<BindingNode> bindingNodes(ResolvedBindings resolvedBindings) { ImmutableSet.Builder<BindingNode> bindingNodes = ImmutableSet.builder(); resolvedBindings .allBindings() .asMap() .forEach( (component, bindings) -> { for (Binding binding : bindings) { bindingNodes.add(bindingNode(resolvedBindings, binding, component)); } }); return bindingNodes.build(); }
@Override public ImmutableSet<Element> process( SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) { ImmutableSet.Builder<Element> deferredElements = ImmutableSet.builder(); ImmutableSetMultimap.copyOf(elementsByAnnotation) .inverse() .asMap() .forEach( (element, annotations) -> { try { process(downcaster.apply(element), ImmutableSet.copyOf(annotations)); } catch (TypeNotPresentException e) { deferredElements.add(element); } }); return deferredElements.build(); }
/** * Collate settings for a component into a single map * * @param component the component to consider * @return the (immutable) map of parent class to the settings it declares */ public Map<Class<?>, Collection<Class<? extends Settings>>> compile(Class<?> component) { Builder<Class<?>, Class<? extends Settings>> builder = ImmutableSetMultimap.builder(); addAnnotatedSettings(builder, component); return builder.build().asMap(); }
@Override protected Map<String, Collection<Integer>> makeEmptyMap() { return ImmutableSetMultimap.<String, Integer>of().asMap(); }
@Override protected Map<String, Collection<Integer>> makePopulatedMap() { Multimap<String, Integer> delegate = HashMultimap.create(); populate(delegate); return ImmutableSetMultimap.copyOf(delegate).asMap(); } }
public void testAsMultimapCaches() { ImmutableMap<String, Integer> map = ImmutableMap.of("one", 1); ImmutableSetMultimap<String, Integer> multimap1 = map.asMultimap(); ImmutableSetMultimap<String, Integer> multimap2 = map.asMultimap(); assertEquals(1, multimap1.asMap().size()); assertSame(multimap1, multimap2); }
public void testBuilderOrderKeysAndValuesBy() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("b", 3); builder.put("d", 2); builder.put("a", 5); builder.orderKeysBy(Collections.reverseOrder()); builder.orderValuesBy(Collections.reverseOrder()); builder.put("c", 4); builder.put("a", 2); builder.put("b", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("d", "c", "b", "a").inOrder(); assertThat(multimap.values()).containsExactly(2, 4, 6, 3, 5, 2).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("b")).containsExactly(6, 3).inOrder(); assertTrue(multimap.get("a") instanceof ImmutableSortedSet); assertEquals(Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.get("a")).comparator()); assertTrue(multimap.get("x") instanceof ImmutableSortedSet); assertEquals(Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.get("x")).comparator()); assertTrue(multimap.asMap().get("a") instanceof ImmutableSortedSet); assertEquals(Collections.reverseOrder(), ((ImmutableSortedSet<Integer>) multimap.asMap().get("a")).comparator()); }
public void testBuilderOrderKeysBy() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("b", 3); builder.put("d", 2); builder.put("a", 5); builder.orderKeysBy(Collections.reverseOrder()); builder.put("c", 4); builder.put("a", 2); builder.put("b", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("d", "c", "b", "a").inOrder(); assertThat(multimap.values()).containsExactly(2, 4, 3, 6, 5, 2).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("b")).containsExactly(3, 6).inOrder(); assertFalse(multimap.get("a") instanceof ImmutableSortedSet); assertFalse(multimap.get("x") instanceof ImmutableSortedSet); assertFalse(multimap.asMap().get("a") instanceof ImmutableSortedSet); }
public void testBuilderOrderKeysByDuplicates() { ImmutableSetMultimap.Builder<String, Integer> builder = ImmutableSetMultimap.builder(); builder.put("bb", 3); builder.put("d", 2); builder.put("a", 5); builder.orderKeysBy(new Ordering<String>() { @Override public int compare(String left, String right) { return left.length() - right.length(); } }); builder.put("cc", 4); builder.put("a", 2); builder.put("bb", 6); ImmutableSetMultimap<String, Integer> multimap = builder.build(); assertThat(multimap.keySet()).containsExactly("d", "a", "bb", "cc").inOrder(); assertThat(multimap.values()).containsExactly(2, 5, 2, 3, 6, 4).inOrder(); assertThat(multimap.get("a")).containsExactly(5, 2).inOrder(); assertThat(multimap.get("bb")).containsExactly(3, 6).inOrder(); assertFalse(multimap.get("a") instanceof ImmutableSortedSet); assertFalse(multimap.get("x") instanceof ImmutableSortedSet); assertFalse(multimap.asMap().get("a") instanceof ImmutableSortedSet); }