diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java new file mode 100644 index 0000000000..1d19423f7e --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSet.java @@ -0,0 +1,101 @@ +package org.baeldung.guava; + +import java.util.AbstractMap; +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; + +import com.google.common.base.Function; + +public class GuavaMapFromSet extends AbstractMap { + + private class SingleEntry implements Entry { + private K key; + + public SingleEntry(K key) { + this.key = key; + } + + @Override + public K getKey() { + return this.key; + } + + @Override + public V getValue() { + V value = GuavaMapFromSet.this.cache.get(this.key); + if (value == null) { + value = GuavaMapFromSet.this.function.apply(this.key); + GuavaMapFromSet.this.cache.put(this.key, value); + } + return value; + } + + @Override + public V setValue(V value) { + throw new UnsupportedOperationException(); + } + } + + private class MyEntrySet extends AbstractSet> { + + public class EntryIterator implements Iterator> { + private Iterator inner; + + public EntryIterator() { + this.inner = MyEntrySet.this.keys.iterator(); + } + + @Override + public boolean hasNext() { + return this.inner.hasNext(); + } + + @Override + public Map.Entry next() { + K key = this.inner.next(); + return new SingleEntry(key); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + } + + private Set keys; + + public MyEntrySet(Set keys) { + this.keys = keys; + } + + @Override + public Iterator> iterator() { + return new EntryIterator(); + } + + @Override + public int size() { + return this.keys.size(); + } + + } + + private WeakHashMap cache; + private Set> entries; + private Function function; + + public GuavaMapFromSet(Set keys, Function function) { + this.function = function; + this.cache = new WeakHashMap(); + this.entries = new MyEntrySet(keys); + } + + @Override + public Set> entrySet() { + return this.entries; + } + +} diff --git a/guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java new file mode 100644 index 0000000000..7dc4550c09 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaMapFromSetTests.java @@ -0,0 +1,64 @@ +package org.baeldung.guava; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Test; + +import com.google.common.base.Function; + +public class GuavaMapFromSetTests { + + @Test + public void givenStringSet_whenMapsToElementLength_thenCorrect() { + Function function = new Function() { + @Override + public String apply(Integer from) { + return Integer.toBinaryString(from); + } + }; + Set set = new TreeSet<>(Arrays.asList(32, 64, 128)); + Map map = new GuavaMapFromSet(set, function); + assertTrue(map.get(32).equals("100000") + && map.get(64).equals("1000000") + && map.get(128).equals("10000000")); + } + + @Test + public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() { + Function function = new Function() { + + @Override + public Integer apply(String from) { + return from.length(); + } + }; + Set set = new TreeSet<>(Arrays.asList( + "four", "three", "twelve")); + Map map = new GuavaMapFromSet(set, + function); + assertTrue(map.get("four") == 4 && map.get("three") == 5 + && map.get("twelve") == 6); + } + + @Test + public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() { + Function function = new Function() { + + @Override + public Integer apply(String from) { + return from.length(); + } + }; + Set set = new TreeSet<>(Arrays.asList( + "four", "three", "twelve")); + Map map = new GuavaMapFromSet(set, + function); + set.add("one"); + assertTrue(map.get("one") == 3 && map.size() == 4); + } +}