SetMultiMap extends AbstractMultiMap<K, V> { public Map<K, Set<V>> asMap() { Map<K, Set<V>> map = new HashMap<K, Set<V>>(); for (Entry<K, Collection<V>> entry : data.entrySet()) { map.put(entry.getKey(), (Set<V>) entry.getValue()); } return map; }  Map<K, Set<V>> asMap();  }
@Test public void canPut() { SetMultiMap<Integer, String> s = new SetMultiMap<Integer, String>(); assertThat(s.put(1, "foo"), is(TRUE)); assertThat(s.asMap(), is(buildMap(1, Collections.singleton("foo")))); }
@Test public void canPutTwice() { SetMultiMap<Integer, String> s = new SetMultiMap<Integer, String>(); assertThat(s.put(1, "foo"), is(TRUE)); assertThat(s.put(1, "foo"), is(FALSE)); assertThat(s.asMap(), is(buildMap(1, Collections.singleton("foo")))); }
@Test public void canRemoveExistingValue() { SetMultiMap<Integer, String> s = new SetMultiMap<Integer, String>(); assertThat(s.put(1, "foo"), is(TRUE)); assertThat(s.remove(1, "foo"), is(TRUE)); assertThat(s.asMap(), is(Collections.<Integer, Set<String>> emptyMap())); }
@Test public void canHandleRemovesOfNonExistingValues() { SetMultiMap<Integer, String> s = new SetMultiMap<Integer, String>(); assertThat(s.put(1, "foo"), is(TRUE)); assertThat(s.remove(1, "bar"), is(FALSE)); assertThat(s.asMap(), is(buildMap(1, Collections.singleton("foo")))); }
@Test public void canHandleRemovesOfNonExistingKeys() { SetMultiMap<Integer, String> s = new SetMultiMap<Integer, String>(); assertThat(s.put(1, "foo"), is(TRUE)); assertThat(s.remove(2, "foo"), is(FALSE)); assertThat(s.asMap(), is(buildMap(1, Collections.singleton("foo")))); }