Skip to content

Commit 8525795

Browse files
bengi - guava map
1 parent 635a5aa commit 8525795

2 files changed

Lines changed: 130 additions & 132 deletions

File tree

‎guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java‎

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,91 +11,91 @@
1111

1212
public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
1313

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-
}
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+
}
100100

101101
}

‎guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java‎

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,52 @@
1313

1414
public class GuavaMapFromSetTests {
1515

16-
@Test
17-
public void givenStringSet_whenMapsToElementLength_thenCorrect() {
18-
Function<Integer, String> function = new Function<Integer, String>() {
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+
}
1930

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-
}
31+
@Test
32+
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
33+
Function<String, Integer> function = new Function<String, Integer>() {
3334

34-
@Test
35-
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
36-
Function<String, Integer> function = new Function<String, Integer>() {
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+
}
3747

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>() {
48+
@Test
49+
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
50+
Function<String, Integer> function = new Function<String, Integer>() {
5351

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-
}
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+
}
6664
}

0 commit comments

Comments
 (0)