S
Sideswipe
So,
I am trying to create a delegate Map that simply throws an exception
if an attempt to insert a duplicate key occurs. That code is simple.
However, the delegate code is not and I am getting compile errors with
regards to my generics. I make no claims to fully understand generics.
So, I present the work I have with the hopes someone can explain to me
why this won't work.
Christian
http://christan.bongiorno.org
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
}
}
I am trying to create a delegate Map that simply throws an exception
if an attempt to insert a duplicate key occurs. That code is simple.
However, the delegate code is not and I am getting compile errors with
regards to my generics. I make no claims to fully understand generics.
So, I present the work I have with the hopes someone can explain to me
why this won't work.
Christian
http://christan.bongiorno.org
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
}
}