Re: Delegation and generics craziness
On Mon, 11 Aug 2008 10:00:23 -0700 (PDT), Sideswipe
<christian.bongiorno@gmail.com> wrote, quoted or indirectly quoted
someone who said :
public class ExceptionOnDuplicateKeyMap<K, V> implements Map<K,V> {
private final Map<? extends K, ? extends V> delegate;
public ExceptionOnDuplicateKeyMap(Map<? extends K, ? extends V>
delegate) {
this.delegate = delegate;
}
public void clear() {
delegate.clear();
}
public boolean containsKey(Object key) {
return delegate.containsKey(key);
}
public boolean containsValue(Object value) {
return delegate.containsValue(value);
}
public Set<Entry<K, V>> entrySet() {
return delegate.entrySet(); // error here
}
public V get(Object key) {
return delegate.get(key);
}
public boolean isEmpty() {
return delegate.isEmpty();
}
public Set<K> keySet() {
return delegate.keySet(); // error here
}
public V put(K key, V value) {
if(delegate.containsKey(key))
throw new IllegalArgumentException();
return delegate.put(key,value); // error here
}
public void putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> entry : m.entrySet())
put(entry.getKey(),entry.getValue());
}
public V remove(Object key) {
return delegate.remove(key);
}
public int size() {
return delegate.size();
}
public Collection<V> values() {
return delegate.values(); // error here
}
}
your first problem is you are missing your imports:
import java.util.Collection;
import java.util.Map;
import java.util.Set;
After you fix that you are left with:
ExceptionOnDuplicateKeyMap.java:33: incompatible types
found : java.util.Set<java.util.Map.Entry<capture#674 of ? extends
K,capture#787 of ? extends V>>
required: java.util.Set<java.util.Map.Entry<K,V>>
return delegate.entrySet(); // error here
^
ExceptionOnDuplicateKeyMap.java:48: incompatible types
found : java.util.Set<capture#774 of ? extends K>
required: java.util.Set<K>
return delegate.keySet(); // error here
^
ExceptionOnDuplicateKeyMap.java:55: put(capture#371 of ? extends
K,capture#681 of ? extends V) in ja
va.util.Map<capture#371 of ? extends K,capture#681 of ? extends V>
cannot be applied to (K,V)
return delegate.put(key,value); // error here
^
ExceptionOnDuplicateKeyMap.java:76: incompatible types
found : java.util.Collection<capture#972 of ? extends V>
required: java.util.Collection<V>
return delegate.values(); // error here
^
The mismatches are fairly obvious. The problem what do you do to fix
them. Have a look at source code for
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient
to
* hold the mappings in the specified <tt>Map</tt>.
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
*/
public HashMap(Map<? extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
putAllForCreate(m);
}
It is similar to what you want to do.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com