bengi - guava map

This commit is contained in:
Slavisa Avramovic 2016-06-09 15:00:56 +02:00
parent 635a5aa5ea
commit 8525795755
2 changed files with 112 additions and 114 deletions

View File

@ -14,7 +14,7 @@ public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
private class SingleEntry implements Entry<K, V> { private class SingleEntry implements Entry<K, V> {
private K key; private K key;
public SingleEntry( K key) { public SingleEntry(K key) {
this.key = key; this.key = key;
} }
@ -34,7 +34,7 @@ public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
} }
@Override @Override
public V setValue( V value) { public V setValue(V value) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
} }
@ -67,7 +67,7 @@ public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
private Set<K> keys; private Set<K> keys;
public MyEntrySet( Set<K> keys) { public MyEntrySet(Set<K> keys) {
this.keys = keys; this.keys = keys;
} }
@ -87,7 +87,7 @@ public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
private Set<Entry<K, V>> entries; private Set<Entry<K, V>> entries;
private Function<? super K, ? extends V> function; private Function<? super K, ? extends V> function;
public GuavaMapFromSet( Set<K> keys, Function<? super K, ? extends V> function) { public GuavaMapFromSet(Set<K> keys, Function<? super K, ? extends V> function) {
this.function = function; this.function = function;
this.cache = new WeakHashMap<K, V>(); this.cache = new WeakHashMap<K, V>();
this.entries = new MyEntrySet(keys); this.entries = new MyEntrySet(keys);

View File

@ -16,16 +16,13 @@ public class GuavaMapFromSetTests {
@Test @Test
public void givenStringSet_whenMapsToElementLength_thenCorrect() { public void givenStringSet_whenMapsToElementLength_thenCorrect() {
Function<Integer, String> function = new Function<Integer, String>() { Function<Integer, String> function = new Function<Integer, String>() {
@Override @Override
public String apply(Integer from) { public String apply(Integer from) {
return Integer.toBinaryString(from.intValue()); return Integer.toBinaryString(from);
} }
}; };
Set<Integer> set = (Set<Integer>) new TreeSet<Integer>(Arrays.asList( Set<Integer> set = new TreeSet<>(Arrays.asList(32, 64, 128));
32, 64, 128)); Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set, function);
Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set,
function);
assertTrue(map.get(32).equals("100000") assertTrue(map.get(32).equals("100000")
&& map.get(64).equals("1000000") && map.get(64).equals("1000000")
&& map.get(128).equals("10000000")); && map.get(128).equals("10000000"));
@ -40,13 +37,14 @@ public class GuavaMapFromSetTests {
return from.length(); return from.length();
} }
}; };
Set<String> set = (Set<String>) new TreeSet<String>(Arrays.asList( Set<String> set = new TreeSet<>(Arrays.asList(
"four", "three", "twelve")); "four", "three", "twelve"));
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set, Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
function); function);
assertTrue(map.get("four") == 4 && map.get("three") == 5 assertTrue(map.get("four") == 4 && map.get("three") == 5
&& map.get("twelve") == 6); && map.get("twelve") == 6);
} }
@Test @Test
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() { public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
Function<String, Integer> function = new Function<String, Integer>() { Function<String, Integer> function = new Function<String, Integer>() {
@ -56,11 +54,11 @@ public class GuavaMapFromSetTests {
return from.length(); return from.length();
} }
}; };
Set<String> set = (Set<String>) new TreeSet<String>(Arrays.asList( Set<String> set = new TreeSet<>(Arrays.asList(
"four", "three", "twelve")); "four", "three", "twelve"));
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set, Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
function); function);
set.add("one"); set.add("one");
assertTrue(map.get("one") == 3 && map.size()==4); assertTrue(map.get("one") == 3 && map.size() == 4);
} }
} }