UnsafeArrayMap implements Map<K, V> { @Override public V get(Object o) { if (o == null) return null; int i = arrayIndexOfKey(o); return i != -1 ? value(i + 1) : null; } UnsafeArrayMap(Object[] array, int toIndex, long filteredBitSet); static Builder<K, V> newBuilder(); @Override int size(); @Override boolean containsKey(Object o); @Override boolean containsValue(Object o); @Override V get(Object o); @Override Set<K> keySet(); @Override Collection<V> values(); @Override Set<Map.Entry<K, V>> entrySet(); @Override boolean isEmpty(); @Override V put(K key, V value); @Override V remove(Object key); @Override void putAll(Map<? extends K, ? extends V> m); @Override void clear(); @Override String toString();  }
@Test public void noNullValues() { array[0] = "1"; array[1] = "one"; array[2] = "2"; array[3] = "two"; array[4] = "3"; array[5] = "three"; Map<String, String> map = builder.build(array); assertSize(map, 3); assertBaseCase(map); assertThat(map).containsOnly( entry("1", "one"), entry("2", "two"), entry("3", "three") ); assertThat(map).hasToString( "UnsafeArrayMap{1=one,2=two,3=three}" ); assertThat(map.get("1")).isEqualTo("one"); assertThat(map.get("2")).isEqualTo("two"); assertThat(map.get("3")).isEqualTo("three"); }
@Test public void equalValues() { array[0] = "1"; array[1] = "1"; array[2] = "2"; array[3] = "2"; array[4] = "3"; array[5] = "3"; Map<String, String> map = builder.build(array); assertSize(map, 3); assertBaseCase(map); assertThat(map).containsOnly( entry("1", "1"), entry("2", "2"), entry("3", "3") ); assertThat(map).hasToString( "UnsafeArrayMap{1=1,2=2,3=3}" ); assertThat(map.get("1")).isEqualTo("1"); assertThat(map.get("2")).isEqualTo("2"); assertThat(map.get("3")).isEqualTo("3"); }
@Test public void mapKeys() { array[0] = " 1"; array[1] = "one"; array[2] = "2 "; array[3] = "two"; array[4] = " 3"; array[5] = "three"; Map<String, String> map = builder.mapKeys(o -> ((String) o).trim()).build(array); assertSize(map, 3); assertBaseCase(map); assertThat(map).containsOnly( entry("1", "one"), entry("2", "two"), entry("3", "three") ); assertThat(map).hasToString( "UnsafeArrayMap{1=one,2=two,3=three}" ); assertThat(map.get("1")).isEqualTo("one"); assertThat(map.get("2")).isEqualTo("two"); assertThat(map.get("3")).isEqualTo("three"); }
@Test public void someNullValues() { array[0] = "1"; array[1] = "one"; array[2] = "2"; array[3] = "two"; array[4] = "3"; Map<String, String> map = builder.build(array); assertSize(map, 2); assertBaseCase(map); assertThat(map).containsOnly( entry("1", "one"), entry("2", "two") ); assertThat(map).hasToString( "UnsafeArrayMap{1=one,2=two}" ); assertThat(map.get("1")).isEqualTo("one"); assertThat(map.get("2")).isEqualTo("two"); assertThat(map.get("3")).isNull(); }
@Test public void someFiltered() { array[0] = "1"; array[1] = "one"; array[2] = "2"; array[3] = "two"; array[4] = "3"; array[5] = "three"; Map<String, String> map = builder.filterKeys("1", "3").build(array); assertSize(map, 1); assertBaseCase(map); assertThat(map).containsOnly( entry("2", "two") ); assertThat(map).hasToString( "UnsafeArrayMap{2=two}" ); assertThat(map.get("1")).isNull(); assertThat(map.get("2")).isEqualTo("two"); assertThat(map.get("3")).isNull(); }