Skip to content

Commit 8d7e9a7

Browse files
Merge branch 'pr/436-bengi-guava-set-function-map'
2 parents 604e79d + 8525795 commit 8d7e9a7

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.baeldung.guava;
2+
3+
import java.util.AbstractMap;
4+
import java.util.AbstractSet;
5+
import java.util.Iterator;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.WeakHashMap;
9+
10+
import com.google.common.base.Function;
11+
12+
public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
13+
14+
private class SingleEntry implements Entry<K, V> {
15+
private K key;
16+
17+
public SingleEntry(K key) {
18+
this.key = key;
19+
}
20+
21+
@Override
22+
public K getKey() {
23+
return this.key;
24+
}
25+
26+
@Override
27+
public V getValue() {
28+
V value = GuavaMapFromSet.this.cache.get(this.key);
29+
if (value == null) {
30+
value = GuavaMapFromSet.this.function.apply(this.key);
31+
GuavaMapFromSet.this.cache.put(this.key, value);
32+
}
33+
return value;
34+
}
35+
36+
@Override
37+
public V setValue(V value) {
38+
throw new UnsupportedOperationException();
39+
}
40+
}
41+
42+
private class MyEntrySet extends AbstractSet<Entry<K, V>> {
43+
44+
public class EntryIterator implements Iterator<Entry<K, V>> {
45+
private Iterator<K> inner;
46+
47+
public EntryIterator() {
48+
this.inner = MyEntrySet.this.keys.iterator();
49+
}
50+
51+
@Override
52+
public boolean hasNext() {
53+
return this.inner.hasNext();
54+
}
55+
56+
@Override
57+
public Map.Entry<K, V> next() {
58+
K key = this.inner.next();
59+
return new SingleEntry(key);
60+
}
61+
62+
@Override
63+
public void remove() {
64+
throw new UnsupportedOperationException();
65+
}
66+
}
67+
68+
private Set<K> keys;
69+
70+
public MyEntrySet(Set<K> keys) {
71+
this.keys = keys;
72+
}
73+
74+
@Override
75+
public Iterator<Map.Entry<K, V>> iterator() {
76+
return new EntryIterator();
77+
}
78+
79+
@Override
80+
public int size() {
81+
return this.keys.size();
82+
}
83+
84+
}
85+
86+
private WeakHashMap<K, V> cache;
87+
private Set<Entry<K, V>> entries;
88+
private Function<? super K, ? extends V> function;
89+
90+
public GuavaMapFromSet(Set<K> keys, Function<? super K, ? extends V> function) {
91+
this.function = function;
92+
this.cache = new WeakHashMap<K, V>();
93+
this.entries = new MyEntrySet(keys);
94+
}
95+
96+
@Override
97+
public Set<Map.Entry<K, V>> entrySet() {
98+
return this.entries;
99+
}
100+
101+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.baeldung.guava;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.Arrays;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.TreeSet;
9+
10+
import org.junit.Test;
11+
12+
import com.google.common.base.Function;
13+
14+
public class GuavaMapFromSetTests {
15+
16+
@Test
17+
public void givenStringSet_whenMapsToElementLength_thenCorrect() {
18+
Function<Integer, String> function = new Function<Integer, String>() {
19+
@Override
20+
public String apply(Integer from) {
21+
return Integer.toBinaryString(from);
22+
}
23+
};
24+
Set<Integer> set = new TreeSet<>(Arrays.asList(32, 64, 128));
25+
Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set, function);
26+
assertTrue(map.get(32).equals("100000")
27+
&& map.get(64).equals("1000000")
28+
&& map.get(128).equals("10000000"));
29+
}
30+
31+
@Test
32+
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
33+
Function<String, Integer> function = new Function<String, Integer>() {
34+
35+
@Override
36+
public Integer apply(String from) {
37+
return from.length();
38+
}
39+
};
40+
Set<String> set = new TreeSet<>(Arrays.asList(
41+
"four", "three", "twelve"));
42+
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
43+
function);
44+
assertTrue(map.get("four") == 4 && map.get("three") == 5
45+
&& map.get("twelve") == 6);
46+
}
47+
48+
@Test
49+
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
50+
Function<String, Integer> function = new Function<String, Integer>() {
51+
52+
@Override
53+
public Integer apply(String from) {
54+
return from.length();
55+
}
56+
};
57+
Set<String> set = new TreeSet<>(Arrays.asList(
58+
"four", "three", "twelve"));
59+
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
60+
function);
61+
set.add("one");
62+
assertTrue(map.get("one") == 3 && map.size() == 4);
63+
}
64+
}

0 commit comments

Comments
 (0)