bengi - guava map
This commit is contained in:
parent
635a5aa5ea
commit
8525795755
@ -11,91 +11,91 @@ import com.google.common.base.Function;
|
|||||||
|
|
||||||
public class GuavaMapFromSet<K, V> extends AbstractMap<K, V> {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public K getKey() {
|
public K getKey() {
|
||||||
return this.key;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V getValue() {
|
public V getValue() {
|
||||||
V value = GuavaMapFromSet.this.cache.get(this.key);
|
V value = GuavaMapFromSet.this.cache.get(this.key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
value = GuavaMapFromSet.this.function.apply(this.key);
|
value = GuavaMapFromSet.this.function.apply(this.key);
|
||||||
GuavaMapFromSet.this.cache.put(this.key, value);
|
GuavaMapFromSet.this.cache.put(this.key, value);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V setValue( V value) {
|
public V setValue(V value) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MyEntrySet extends AbstractSet<Entry<K, V>> {
|
private class MyEntrySet extends AbstractSet<Entry<K, V>> {
|
||||||
|
|
||||||
public class EntryIterator implements Iterator<Entry<K, V>> {
|
public class EntryIterator implements Iterator<Entry<K, V>> {
|
||||||
private Iterator<K> inner;
|
private Iterator<K> inner;
|
||||||
|
|
||||||
public EntryIterator() {
|
public EntryIterator() {
|
||||||
this.inner = MyEntrySet.this.keys.iterator();
|
this.inner = MyEntrySet.this.keys.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return this.inner.hasNext();
|
return this.inner.hasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map.Entry<K, V> next() {
|
public Map.Entry<K, V> next() {
|
||||||
K key = this.inner.next();
|
K key = this.inner.next();
|
||||||
return new SingleEntry(key);
|
return new SingleEntry(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove() {
|
public void remove() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<K> keys;
|
private Set<K> keys;
|
||||||
|
|
||||||
public MyEntrySet( Set<K> keys) {
|
public MyEntrySet(Set<K> keys) {
|
||||||
this.keys = keys;
|
this.keys = keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Map.Entry<K, V>> iterator() {
|
public Iterator<Map.Entry<K, V>> iterator() {
|
||||||
return new EntryIterator();
|
return new EntryIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return this.keys.size();
|
return this.keys.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private WeakHashMap<K, V> cache;
|
private WeakHashMap<K, V> cache;
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Map.Entry<K, V>> entrySet() {
|
public Set<Map.Entry<K, V>> entrySet() {
|
||||||
return this.entries;
|
return this.entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,54 +13,52 @@ import com.google.common.base.Function;
|
|||||||
|
|
||||||
public class GuavaMapFromSetTests {
|
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
|
||||||
|
public String apply(Integer from) {
|
||||||
|
return Integer.toBinaryString(from);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Set<Integer> set = new TreeSet<>(Arrays.asList(32, 64, 128));
|
||||||
|
Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set, function);
|
||||||
|
assertTrue(map.get(32).equals("100000")
|
||||||
|
&& map.get(64).equals("1000000")
|
||||||
|
&& map.get(128).equals("10000000"));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Test
|
||||||
public String apply(Integer from) {
|
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
|
||||||
return Integer.toBinaryString(from.intValue());
|
Function<String, Integer> function = new Function<String, Integer>() {
|
||||||
}
|
|
||||||
};
|
|
||||||
Set<Integer> set = (Set<Integer>) new TreeSet<Integer>(Arrays.asList(
|
|
||||||
32, 64, 128));
|
|
||||||
Map<Integer, String> map = new GuavaMapFromSet<Integer, String>(set,
|
|
||||||
function);
|
|
||||||
assertTrue(map.get(32).equals("100000")
|
|
||||||
&& map.get(64).equals("1000000")
|
|
||||||
&& map.get(128).equals("10000000"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Override
|
||||||
public void givenIntSet_whenMapsToElementBinaryValue_thenCorrect() {
|
public Integer apply(String from) {
|
||||||
Function<String, Integer> function = new Function<String, Integer>() {
|
return from.length();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Set<String> set = new TreeSet<>(Arrays.asList(
|
||||||
|
"four", "three", "twelve"));
|
||||||
|
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
|
||||||
|
function);
|
||||||
|
assertTrue(map.get("four") == 4 && map.get("three") == 5
|
||||||
|
&& map.get("twelve") == 6);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Test
|
||||||
public Integer apply(String from) {
|
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
|
||||||
return from.length();
|
Function<String, Integer> function = new Function<String, Integer>() {
|
||||||
}
|
|
||||||
};
|
|
||||||
Set<String> set = (Set<String>) new TreeSet<String>(Arrays.asList(
|
|
||||||
"four", "three", "twelve"));
|
|
||||||
Map<String, Integer> map = new GuavaMapFromSet<String, Integer>(set,
|
|
||||||
function);
|
|
||||||
assertTrue(map.get("four") == 4 && map.get("three") == 5
|
|
||||||
&& map.get("twelve") == 6);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void givenSet_whenNewSetElementAddedAndMappedLive_thenCorrect() {
|
|
||||||
Function<String, Integer> function = new Function<String, Integer>() {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer apply(String from) {
|
public Integer apply(String from) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user