Skip to content

Commit b9ddf5f

Browse files
Merge pull request eugenp#436 from egimaben/master
Added guava set+function=map tutorial and tests
2 parents 604e79d + 635a5aa commit b9ddf5f

2 files changed

Lines changed: 167 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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
20+
@Override
21+
public String apply(Integer from) {
22+
return Integer.toBinaryString(from.intValue());
23+
}
24+
};
25+
Set<Integer> set = (Set<Integer>) new TreeSet<Integer>(Arrays.asList(
26+
32, 64, 128));
27+
Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set,
28+
function);
29+
assertTrue(map.get(32).equals("100000")
30+
&& map.get(64).equals("1000000")
31+
&& map.get(128).equals("10000000"));
32+
}
33+
34+
@Test
35+
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
36+
Function<String, Integer> function = new Function<String, Integer>() {
37+
38+
@Override
39+
public Integer apply(String from) {
40+
return from.length();
41+
}
42+
};
43+
Set<String> set = (Set<String>) new TreeSet<String>(Arrays.asList(
44+
"four", "three", "twelve"));
45+
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
46+
function);
47+
assertTrue(map.get("four") == 4 && map.get("three") == 5
48+
&& map.get("twelve") == 6);
49+
}
50+
@Test
51+
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
52+
Function<String, Integer> function = new Function<String, Integer>() {
53+
54+
@Override
55+
public Integer apply(String from) {
56+
return from.length();
57+
}
58+
};
59+
Set<String> set = (Set<String>) new TreeSet<String>(Arrays.asList(
60+
"four", "three", "twelve"));
61+
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
62+
function);
63+
set.add("one");
64+
assertTrue(map.get("one") == 3 && map.size()==4);
65+
}
66+
}

0 commit comments

Comments
 (0)