ListBasedMapCodec implements Codec<Map<K,V>> { public Map<K,V> decode(BitBuffer buffer, Resolver resolver, Builder builder) throws DecodingException { List<? extends Map.Entry<K, V>> entries = listCodec.decode(buffer, resolver, builder); Map<K,V> result = new HashMap(entries.size()); for (Map.Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; } ListBasedMapCodec(Codec<List<Map.Entry<K,V>>> listCodec); Map<K,V> decode(BitBuffer buffer, Resolver resolver, Builder builder); void encode(Map value, BitChannel channel, Resolver resolver); Expression<Integer, Resolver> getSize(); CodecDescriptor getCodecDescriptor(); Class<?>[] getTypes(); Class<?> getType();  }
@Test public void shouldWorkWithNormalList() throws DecodingException { when(listCodec.decode(any(BitBuffer.class), any(Resolver.class), any(Builder.class))) .thenReturn(Arrays.asList(item1, item2)); when(item1.getKey()).thenReturn("foo"); when(item1.getValue()).thenReturn(1); when(item2.getKey()).thenReturn("bar"); when(item2.getValue()).thenReturn(2); ListBasedMapCodec<String,Integer> codec = new ListBasedMapCodec<String, Integer>(listCodec); Map<String, Integer> map = codec.decode(buffer, resolver, builder); assertThat(map.size(), is(2)); assertThat(map, hasEntry("foo", 1)); assertThat(map, hasEntry("bar", 2)); verify(listCodec).decode(buffer, resolver, builder); Mockito.verifyNoMoreInteractions(listCodec); }
@Test public void shouldWorkWithZeroSizeList() throws DecodingException { when(listCodec.decode(any(BitBuffer.class), any(Resolver.class), any(Builder.class))) .thenReturn(Collections.<Map.Entry<String, Integer>>emptyList()); ListBasedMapCodec<String,Integer> codec = new ListBasedMapCodec<String, Integer>(listCodec); Map<String, Integer> map = codec.decode(buffer, resolver, builder); assertThat(map.size(), is(0)); verify(listCodec).decode(buffer, resolver, builder); Mockito.verifyNoMoreInteractions(listCodec); }